如何通过Paramiko隧道(或类似包)连接到数据库 [英] How to connect to a database through a Paramiko Tunnel (or similar package)

查看:632
本文介绍了如何通过Paramiko隧道(或类似包)连接到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有严重的问题在paramiko设置一个正确的隧道以启用数据库连接。我已经查看了示例'forward.py',但不知道如何将数据库连接链接到它。

I am having serious issues setting up a proper tunnel in paramiko to enable a database connection. I have reviewed the example 'forward.py', but am not understanding how to then link the database connection to it. Any pointers woudl be much appreciated.

我想我需要以下的东西:

I think I need something the following:

t = paramiko.Transport((hostname, port))
t.connect(username=username, password=password, hostkey=hostkey) 
c = paramiko.Channel(t)

#something about assigning a local port to this connection

connection = psycopg2.connect(connectionstring)
#and do my stuff

connection.close()
c.close()
t.close()


推荐答案

如果你已经使用forward.py脚本创建了你的ssh隧道;您可以使用ssh隧道连接到postgresql,如下所示:

if you have created your ssh tunnel using the forward.py script ; you can use the ssh tunnel to connect to postgresql like this:

conn = psycopg2.connect(database="test", host="localhost", port=<forward_port>)

这篇关于如何通过Paramiko隧道(或类似包)连接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆