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

查看:17
本文介绍了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.

注意:我在 How to connect to a database through a Paramiko Tunnel(或类似的包),但该解决方案似乎特定于 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=proxy,RMT=remote):

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 ...

脚本会卡在最后一行.

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天全站免登陆