Port forwarding with Paramiko

Please find some code using paramiko-1.7.7.1, pycrypto-2.6 and the forward.py script from which I did remove code from the line 115 to the end (to avoid options parsing).

import paramiko, sys
from forward import forward_tunnel

remote_host = "target_host"
remote_port = 8000
local_port  = 8000
ssh_host    = "my_ssh_host"
ssh_port    = 22

user     = "login"
password = "s3cr3t"

transport = paramiko.Transport((ssh_host, ssh_port))

# Command for paramiko-1.7.7.1
transport.connect(hostkey  = None,
                  username = user,
                  password = password,
                  pkey     = None)

try:
    forward_tunnel(local_port, remote_host, remote_port, transport)
except KeyboardInterrupt:
    print 'Port forwarding stopped.'
    sys.exit(0)

I’ve tested it successfully from a Windows station, using a ssh server under Red Hat and pointing to a 3rd server. (I’m using Python 2.7.2)

Hope it helps,

Leave a Comment