Hi Washake,
Here's a sample I just whipped up from the example I found here:
<http://pexpect.svn.sourceforge.net/viewvc/pexpect/trunk/pexpect/examples/ssh_tunnel.py?revision=498&view=markup>
Note that I had to tweak the 'tunnel_command' line a bit as it was
missing the conversion type. This sets up a tunnel from a PostgreSQL
server I have to my workstation. I then use the tunnel on my
workstation to connect to the database.
##### On the PostgreSQL server machine ('a_server') #####
# Setup a reverse forward
[EMAIL PROTECTED]:~$ python
Python 2.5.1 (r251:54863, Oct 5 2007, 13:36:32)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pexpect
>>> import getpass
>>> import time
>>> tunnel_command = """ssh -R5432:localhost:5432 %(user)[EMAIL
>>> PROTECTED](host)s"""
>>> host = raw_input('Hostname: ')
Hostname: 192.168.1.1
>>> user = raw_input('Username: ')
Username: username
>>> X = getpass.getpass('Password: ')
Password:
>>> ssh_tunnel = pexpect.spawn(tunnel_command % globals())
>>> ssh_tunnel.expect('password:')
>>> time.sleep(0.1)
>>> ssh_tunnel.sendline(X)
>>> ssh_tunnel.expect (pexpect.EOF)
>>>
# Now the reverse forward is set up and I can connect to the PostgreSQL
# instance running on 'a_server'
##### On the client machine: 192.168.1.1 #####
[EMAIL PROTECTED]:~$ python
Python 2.5.1 (r251:54863, Oct 5 2007, 13:36:32)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2 as Database
>>> conn_parts = {
... 'user':'dbuser',
... 'dbname': 'mydatabase',
... 'password':'',
... 'host':'127.0.0.1',
... 'port':'5432',
... }
>>> cs = ' '.join(["%s=%s" % (k, v) for k, v in conn_parts.items() if v])
>>> print cs
port=5432 host=127.0.0.1 user=dbuser dbname=mydatabase
>>> conn = Database.connect(cs)
>>> cur = conn.cursor()
>>> sql = 'select count(*) from mytable'
>>> cur.execute(sql)
>>> records = cur.fetchall()
>>> conn.close()
>>>
>>> print records
[(557L,)]
>>>
washakie wrote:
> Thanks everyone these seem like promising directions to go... Eric, any
> chance you could share your 'similar' code? The problem it seems with
> paramiko is that it has little 'newbie' documentation, and I'm not clear how
> to set up the reverse port forwarding on it. Thus I may need to use the
> pexpect solution.
>
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor