Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
660 views
in Technique[技术] by (71.8m points)

proxy - 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 established, is it possible to programatically execute shell commands on the remote host?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...