如何使用 python 打开 SSH 隧道?
我正在尝试使用 django 连接到远程 mysql 数据库。
文档规定需要先打开SSH隧道才能连接数据库。
是否有一个 python 库可以在设置某些设置时打开 SSH 隧道?
I am trying to connect to a remote mysql database using django.
The documentation specifies that it is required to open an SSH tunnel first to connect to the database.
Is there a python library that can open an SSH tunnel whenever certain settings are set?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你可以尝试 paramiko 的 转发 功能。有关 paramiko 概述,请参阅 这里。
You could try paramiko's forward functionality. For a paramiko overview, see here.
尝试使用 sshtunnel 包。
这很简单:
披露:我是这个包的作者和维护者。
Try use sshtunnel package.
This is simple:
Disclosure: I'm the author and maintainer of this package.
这是 Python3 的代码片段(但您应该能够毫无困难地将其改造为 Python2)。它在单独的线程中运行 SSH 隧道;然后主线程执行一些操作来通过 SSH 隧道获取网络流量。
在此示例中,ssh 隧道将本地端口 2222 转发到本地主机上的端口 80。主要活动包括运行
,即从端口 2222 获取网页。SshTunnel
类使用 4 个参数进行初始化:本地和远程端口、远程用户和远程主机。它所做的就是通过以下方式启动 SSH:
为了使这项工作正常进行,您需要对远程用户@远程主机进行无密码登录(通过远程服务器上已知的 ~/.ssh/id_rsa.pub)。
这样运行的 ssh 隧道是在一个线程上;主要任务必须在另一项任务中。 ssh 隧道线程被标记为守护进程,以便一旦主活动终止它就会自动停止。
我没有提供完整的 MySQL 连接示例,因为它应该是不言自明的。一旦 SshTunnel 设置了本地 TCP 端口,您就可以通过 MySQL 客户端、curl 或其他方式连接到它。
Here is a code snippet for Python3 (but you should be able to retrofit it into Python2 without difficulty). It runs an SSH tunnel in a separate thread; then the main thread does something to get network traffic over the SSH tunnel.
In this example, the ssh tunnel forwards local port 2222 to port 80 on localhost. The main activity consists of running
ie., fetching a webpage but from port 2222.
The class SshTunnel is initialized with 4 parameters, the local and remote port, the remote user, and the remote host. All it does, is start SSH in the following way:
In order to make this work, you'll need a password-less login for remoteuser@remotehost (via ~/.ssh/id_rsa.pub that's known on the remote server).
The thus running ssh tunnel is on one thread; the main task must be in another one. The ssh tunnel thread is marked as daemon so that it will automatically stop once the main activity terminates.
I didn't put in a full MySQL connectivity example because it should be self-explanatory. Once SshTunnel sets up a local TCP port, you can connect to it - be it via your MySQL client, curl, or whatever.
这里有一个小类,您可以将其放入代码中:
以下是如何使用它,例如使用 MySQL(通常是端口 3306):
Here's a little class that you can drop into your code:
And here's how you could use it, for example with MySQL (port 3306 usually):
这个解决方案对我有用。
在终端/CMD 中运行此命令:
pip install paramiko
然后创建一个 python 文件并在该文件中添加以下代码:
现在运行 python 文件:
python yourfile.py
This solution worked for me.
Run this command in your terminal/CMD:
pip install paramiko
Then create a python file and add this code in that file:
Now run the python file:
python yourfile.py
我是否可以建议尝试像
pyngrok
这样的东西以编程方式为您管理ngrok
隧道?完全公开,我是它的开发者。 SSH 示例此处,以及 Django 示例此处,但它就像安装pyngrok
一样简单:并使用它:
或者公开您的 MySQL 数据库:
或者公开 Django 开发服务器:
Might I suggest trying something like
pyngrok
to programmatically manage anngrok
tunnel for you? Full disclosure, I am the developer of it. SSH example here, and Django example here, but it's as easy as installingpyngrok
:and using it:
Or to expose your MySQL database:
Or to expose to Django dev server: