如何使用python打开SSH隧道?

问题描述 投票:4回答:4

我正在尝试使用django连接到远程mysql数据库。 该文档指定首先需要打开SSH隧道才能连接到数据库。 是否有一个python库可以在设置某些设置时打开SSH隧道?

python django ssh-tunnel
4个回答
7
投票

你可以试试paramikoforward功能。有关paramiko概述,请参阅here


5
投票

这是Python3的代码片段(但你应该可以毫不费力地将它改装成Python2)。它在一个单独的线程中运行SSH隧道;然后主线程通过SSH隧道获取网络流量。

在此示例中,ssh隧道将本地端口2222转发到localhost上的端口80。主要活动包括跑步

curl http://localhost:2222

即,从端口2222获取网页。

SshTunnel类初始化为4个参数,本地和远程端口,远程用户和远程主机。它只是以下列方式启动SSH:

ssh -N -L localport:remotehost:remoteport remoteuser@remotehost

为了使这项工作,你需要一个无密码登录remoteuser @ remotehost(通过远程服务器上已知的〜/ .ssh / id_rsa.pub)。这样运行的ssh隧道在一个线程上;主要任务必须是另一个。 ssh隧道线程被标记为守护进程,因此一旦主活动终止,它将自动停止。

我没有提供完整的MySQL连接示例,因为它应该是不言自明的。一旦SshTunnel设置了本地TCP端口,您就可以连接到它 - 无论是通过MySQL客户端,卷曲还是其他任何东西。

import subprocess
import time
import threading

class SshTunnel(threading.Thread):
    def __init__(self, localport, remoteport, remoteuser, remotehost):
        threading.Thread.__init__(self)
        self.localport = localport      # Local port to listen to
        self.remoteport = remoteport    # Remote port on remotehost
        self.remoteuser = remoteuser    # Remote user on remotehost
        self.remotehost = remotehost    # What host do we send traffic to
        self.daemon = True              # So that thread will exit when
                                        # main non-daemon thread finishes

    def run(self):
        if subprocess.call([
            'ssh', '-N',
                   '-L', str(self.localport) + ':' + self.remotehost + ':' + str(self.remoteport),
                   self.remoteuser + '@' + self.remotehost ]):
            raise Exception ('ssh tunnel setup failed')


if __name__ == '__main__':
    tunnel = SshTunnel(2222, 80, 'karel', 'localhost')
    tunnel.start()
    time.sleep(1)
    subprocess.call(['curl', 'http://localhost:2222'])

3
投票

尝试使用sshtunnel package

这很简单:

pip install sshtunnel
python -m sshtunnel -U vagrant -P vagrant -L :3306 -R 127.0.0.1:3306 -p 2222 localhost

披露:我是这个软件包的作者和维护者。


2
投票

这是一个可以放入代码的小课程:

import subprocess
import random
import tempfile

class SSHTunnel:

    def __init__(self, host, user, port, key, remote_port):
        self.host = host
        self.user = user
        self.port = port
        self.key = key
        self.remote_port = remote_port
        # Get a temporary file name
        tmpfile = tempfile.NamedTemporaryFile()
        tmpfile.close()
        self.socket = tmpfile.name
        self.local_port = random.randint(10000, 65535)
        self.local_host = '127.0.0.1'
        self.open = False

    def start(self):
        exit_status = subprocess.call(['ssh', '-MfN',
            '-S', self.socket,
            '-i', self.key,
            '-p', self.port,
            '-l', self.user,
            '-L', '{}:{}:{}'.format(self.local_port, self.local_host, self.remote_port),
            '-o', 'ExitOnForwardFailure=True',
            self.host
        ])
        if exit_status != 0:
            raise Exception('SSH tunnel failed with status: {}'.format(exit_status))
        if self.send_control_command('check') != 0:
            raise Exception('SSH tunnel failed to check')
        self.open = True

    def stop(self):
        if self.open:
            if self.send_control_command('exit') != 0:
                raise Exception('SSH tunnel failed to exit')
            self.open = False

    def send_control_command(self, cmd):
        return subprocess.check_call(['ssh', '-S', self.socket, '-O', cmd, '-l', self.user, self.host])

    def __enter__(self):
        self.start()
        return self

    def __exit__(self, type, value, traceback):
        self.stop()

以下是如何使用它,例如使用MySQL(通常是端口3306):

with SSHTunnel('database.server.com', 'you', '22', '/path/to/private_key', '3306') as tunnel:
    print "Connected on port {} at {}".format(tunnel.local_port, tunnel.local_host)
© www.soinside.com 2019 - 2024. All rights reserved.