paramiko:SSH端口转发以获取SQL转储 [英] paramiko: SSH port forwarding to get SQL dump

查看:112
本文介绍了paramiko:SSH端口转发以获取SQL转储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python脚本从以中间主机作为代理的远程主机获取SQL转储,如下所示:

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.

注意:我在如何通过Paramiko隧道(或相似的软件包),但是该解决方案似乎特定于PostgreSQL.

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.

我正在通过paramiko使用SSH建立连接.我知道forward.py是端口转发的paramiko示例,但是我不确定我是否使用正确.这就是我所做的(PX =代理,RMT =远程):

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

我得到这个结果:

*** 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:有没有人举过一个例子,说明如何使用paramiko的forward.py通过代理连接到远程主机?

Q1: Does anyone have an example of how to use paramiko's forward.py to connect to remote host via proxy?

Q2:建立连接后,是否可以以编程方式在远程主机上执行shell命令?

Q2: After connection is established, is it possible to programatically execute shell commands on the remote host?

推荐答案

我最近编写了一个SSHForwarder类,该类可以完成您想要的操作:

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

这篇关于paramiko:SSH端口转发以获取SQL转储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆