使用python庫paramiko實現ssh登錄並通過tunnels轉發

由於工作需要,需要寫個端口轉發的工具,google了一下發現大多數都用paramiko庫的比較多,並且github上也有一個腳本的示例,非常好用。
這次寫的腳本如下

#!/usr/bin/env python

"""
Sample script showing how to do local port forwarding over paramiko.
This script connects to the requested SSH server and sets up local port
forwarding (the openssh -L option) from a local port through a tunneled
connection to a destination reachable from the SSH server machine.
"""

import getpass
import os
import socket
import select
import json

try:
    import SocketServer
except ImportError:
    import socketserver as SocketServer

import sys
from optparse import OptionParser

import paramiko

SSH_PORT = 2637
DEFAULT_PORT = 4151

g_verbose = True


class ForwardServer(SocketServer.ThreadingTCPServer):
    daemon_threads = True
    allow_reuse_address = True


class Handler(SocketServer.BaseRequestHandler):

    def handle(self):
        try:
            chan = self.ssh_transport.open_channel(
                "direct-tcpip",
                (self.chain_host, self.chain_port),
                self.request.getpeername(),
            )
        except Exception as e:
            verbose(
                "Incoming request to %s:%d failed: %s"
                % (self.chain_host, self.chain_port, repr(e))
            )
            return
        if chan is None:
            verbose(
                "Incoming request to %s:%d was rejected by the SSH server."
                % (self.chain_host, self.chain_port)
            )
            return

        # verbose(
        #    "Connected!  Tunnel open %r -> %r -> %r"
        #    % (
        #        self.request.getpeername(),
        #        chan.getpeername(),
        #        (self.chain_host, self.chain_port),
        #    )
        # )
        while True:
            r, w, x = select.select([self.request, chan], [], [])
            if self.request in r:
                data = self.request.recv(1024)
                if len(data) == 0:
                    break
                chan.send(data)
            if chan in r:
                data = chan.recv(1024)
                if len(data) == 0:
                    break
                self.request.send(data)

        peername = self.request.getpeername()
        chan.close()
        self.request.close()
        # verbose("Tunnel closed from %r" % (peername,))


def forward_tunnel(local_port, remote_host, remote_port, transport):
    # this is a little convoluted, but lets me configure things for the Handler
    # object.  (SocketServer doesn't give Handlers any way to access the outer
    # server normally.)
    class SubHander(Handler):
        chain_host = remote_host
        chain_port = remote_port
        ssh_transport = transport

    ForwardServer(("", local_port), SubHander).serve_forever()


def verbose(s):
    if g_verbose:
        print(s)


HELP = """\
Set up a forward tunnel across an SSH server, using paramiko. A local port
(given with -p) is forwarded across an SSH session to an address:port from
the SSH server. This is similar to the openssh -L option.
"""


def get_host_port(spec, default_port):
    "parse 'hostname:22' into a host and port, with the port optional"
    args = (spec.split(":", 1) + [default_port])[:2]
    args[1] = int(args[1])
    return args[0], args[1]


def parse_options():
    global g_verbose

    parser = OptionParser(
        usage="usage: python update.py 111111.test",
        version="%prog 1.0",
        description=HELP,
    )
    parser.add_option(
        "-q",
        "--quiet",
        action="store_false",
        dest="verbose",
        default=True,
        help="squelch all informational output",
    )
    parser.add_option(
        "-p",
        "--local-port",
        action="store",
        type="int",
        dest="port",
        default=DEFAULT_PORT,
        help="local port to forward (default: %d)" % DEFAULT_PORT,
    )
    parser.add_option(
        "-u",
        "--user",
        action="store",
        type="string",
        dest="user",
        default='hex',
        help="username for SSH authentication (default: %s)"
             % getpass.getuser(),
    )
    parser.add_option(
        "-K",
        "--key",
        action="store",
        type="string",
        dest="keyfile",
        default=None,
        help="private key file to use for SSH authentication",
    )
    parser.add_option(
        "",
        "--no-key",
        action="store_false",
        dest="look_for_keys",
        default=True,
        help="don't look for or use a private key file",
    )
    parser.add_option(
        "-P",
        "--password",
        action="store",
        dest="readpass",
        default='xxxxx',
        help="read password (for key or password auth) from stdin",
    )
    parser.add_option(
        "-r",
        "--remote",
        action="store",
        type="string",
        dest="remote",
        default='192.168.2.159:1111',
        metavar="host:port",
        help="remote host and port to forward to",
    )
    parser.add_option(
        "-s",
        "--ssh-host",
        action="store",
        type="string",
        dest="ssh_host",
        default='222.22.22.22:1111',
        metavar="host:port",
        help="ssh host and port",
    )
    parser.add_option(
        "-d",
        "--device-type",
        action="store",
        type="string",
        dest="device_type",
        default='device',
        help="group or device",
    )
    parser.add_option(
        "-i",
        "--device-ids",
        action="store",
        type="string",
        dest="device_ids",
        default=None,
        help="deveice ids",
    )
    parser.add_option(
        "-w",
        "--wait-second",
        action="store",
        type="int",
        dest="wait_second",
        default=120,
        help="wait_second",
    )

    options, args = parser.parse_args()

    # if len(args) != 1:
    #    parser.error("Incorrect number of arguments.")
    if options.remote is None:
        parser.error("Remote address required (-r).")

    g_verbose = options.verbose
    server_host, server_port = get_host_port(options.ssh_host, SSH_PORT)
    remote_host, remote_port = get_host_port(options.remote, SSH_PORT)
    return options, (server_host, server_port), (remote_host, remote_port), args, parser


def main():
    options, server, remote, args, parser = parse_options()

    password = options.readpass
    # if options.readpass:
    #    password = getpass.getpass("Enter SSH password: ")

    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.WarningPolicy())

    print('\n\n\n\n')
    verbose("Connecting to ssh host %s:%d ..." % (server[0], server[1]))
    try:
        client.connect(
            server[0],
            server[1],
            username=options.user,
            key_filename=options.keyfile,
            look_for_keys=options.look_for_keys,
            password=password,
        )
    except Exception as e:
        print("*** Failed to connect to %s:%d: %r" % (server[0], server[1], e))
        sys.exit(1)

    verbose(
        "Now forwarding port %d to %s:%d ..."
        % (options.port, remote[0], remote[1])
    )
    print('\n\n\n\n')
    try:
        from threading import Thread
        import time
        import requests
        import json
        tunnel_thread = Thread(target=forward_tunnel, args=(options.port, remote[0],
                                                            remote[1],
                                                            client.get_transport()))
        tunnel_thread.start()
        if len(args) != 2:
            print(args)
            parser.error("Incorrect number of arguments. len=" + str(len(args)))
        topic = args[0]
        hipos_zip = args[1]
        if not hipos_zip:
            parser.error("Need hipos_zip argument.")
        if not topic:
            parser.error("Need topic argument.")
        if topic == '1201.HEX_POS_SERVER_DEVICE':
            print("Online topic:%s %s" % (topic, '*' * 60))
            online_flag = input("It's online topic, please enter 'true' for sure***************:  ")
            if 'true' != online_flag:
                parser.error("Not continue! It's online topic.")
                os._exit(0)
        elif topic == '120101.HEX_POS_SERVER_DEVICE':
            print('Test topic:%s %s' % (topic, '*' * 60))
        else:
            parser.error("Invalid topic argument.")
        device_type = options.device_type
        ids = options.device_ids
        if not ids or not ids.split(','):
            parser.error("Please enter ids!")
        url = "http://localhost:4151/pub?topic=" + topic
        config = ids.split(',')
        update_data = {
            'event':
                'local_api',
            'type':
                'device',
            'group':
                '',
            'id':
                'nothing',
            'payload': {
                'data':
                    [None,
                     'hipos_update.zip',
                     '13953192',
                     'temp'],
                'action':
                    'updateToNewHipos',
                'id':
                    0,
                'serviceName':
                    'version'}
        }
        update_data['type'] = device_type
        update_data['payload']['data'][0] = hipos_zip
        for i in range(1, 61):
            time.sleep(1)
            print('Download update_zip command will be sent in %ss, please wait...' %
                  (61 - i))
        print('\n\n\n\n')
        print('-' * 100)
        for i in config:
            update_data['id'] = i
            print('id=' + str(update_data['id']))
            print('update=' + str(update_data))
            resp = requests.post(url, data=json.dumps(update_data))
            print('response=' + resp.text)
            time.sleep(1)
            print('-' * 100)
        wait_second = options.wait_second

        print('\n\n\n\n')
        print('-' * 100)
        print('Send update data successfully, restart in %s s.' % wait_second)
        print('Please waiting')
        print('-' * 100)
        print('\n\n\n\n')
        for i in range(1, wait_second):
            time.sleep(1)
            print('Restart command will be sent in %ss, please wait...' %
                  (wait_second - i))
        print('-' * 100)
        print('Hipos is coming after restart command sent')
        print('-' * 100)
        print('\n\n\n\n')
        print('-' * 100)
        update_data['payload']['data'] = []
        update_data['payload']['action'] = 'RestartToNewHipos'
        print('update=' + str(update_data))
        for i in config:
            update_data['id'] = i
            print('Restart command was sent successfully! ID=' + str(i))
            print('update=' + str(update_data))
            resp = requests.post(url, data=json.dumps(update_data))
            print('response=' + resp.text)
            time.sleep(1)
        print('-' * 100)
        print('\n\n\n\n')
        print('-' * 100)
        print('Wait for hipos coming')
        print('done!')
        os._exit(0)
    except KeyboardInterrupt:
        print("C-c: Port forwarding stopped.")
        os._exit(0)
    return client


if __name__ == "__main__":
    main()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章