0

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()
mvrak
  • 501
  • 3
  • 12

2 Answers2

2

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>)
mouad
  • 67,571
  • 18
  • 114
  • 106
  • I know this is right, I am just not using forward.py properly. – mvrak Oct 28 '10 at 01:54
  • Turns out I am not sure why I thought this suited my uses. It is better for me to maintain a tunnel through the external program ssh than to try to set it up in Python. – mvrak Nov 03 '10 at 18:48
1

I had same serious issues getting paramiko to work too but ended up doing it with another library (sshtunnel) that wraps and simplifies paramiko's tunneling.

You can check my answer in another similar question with some sample code to use it.

Community
  • 1
  • 1
jsjc
  • 1,003
  • 2
  • 12
  • 24