paramiko: SSH port forwarding to get SQL dump

和自甴很熟 提交于 2019-11-28 04:33:50

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/5852481/paramiko-ssh-port-forwarding-to-get-sql-dump

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!