paramiko: SSH port forwarding to get SQL dump
I am trying to use a python script to get an SQL dump from a remote host, with an intermediate host as proxy, like so:
local machine -> proxy -> remote
The proxy needs to be there because the remote host only allows connections through that proxy.
Note: I am aware of a similar question at How to connect to a database through a Paramiko Tunnel (or similar package) but the solution seems to be specific to PostgreSQL.
I am making the connection using SSH via paramiko. I am aware that forward.py is the paramiko example for port forwarding, but I am not sure if I am using it correctly. This is what I did (PX=proxy, RMT=remote):
forward.py --password --host=PX --port=PXport --user=PXusr RMT:RMTport
And I get this result:
*** Unable to open host keys file
*** Warning: no host key for PX
Connecting to ssh host PX:PXport ...
Now forwarding port 4000 to RMT:RMTport ...
The script then gets stuck at the last line.
Q1: Does anyone have an example of how to use paramiko's forward.py to connect to remote host via proxy?
Q2: After connection is est开发者_如何学JAVAablished, is it possible to programatically execute shell commands on the remote host?
I recently wrote an SSHForwarder
class that does what you want:
https://gist.github.com/1399529
To use it you can do something like this:
try:
proxy_host = 'PX'
proxy_user = 'PXusr'
target_port = RMTport
target_host = 'RMT'
f = SSHForwarder(proxy_host, target_port, target_host, proxy_user)
# .... DO SOME STUFF ....
finally:
if f:
f.close()
精彩评论