使用 Paramiko 进行端口转发 [英] Port forwarding with Paramiko

查看:79
本文介绍了使用 Paramiko 进行端口转发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Paramiko 从 Python 应用程序进行一些端口转发.我可以很好地设置 SSH 连接,但我对如何使用 paramiko.Transport 有点困惑.我已经找到 这个文件,但我不知道里面发生了什么.从查看 paramiko.Transport 文档,似乎是使用 open_channel 函数的一行,但我不知道如何实现它.我正在尝试复制一个简单的 ssh -L 8000:localhost:8000.

I'm trying to do some port forwarding from a python app using Paramiko. I can set up the SSH connection just fine, but I'm a bit stumped as to how to use paramiko.Transport. I've already found this file, but I can't work out what's going on in it. From looking at the paramiko.Transport docs, it seems that a single line using the open_channel function, but I can't work out how to implement that. I'm trying to replicate a simple ssh -L 8000:localhost:8000.

有人可以帮我吗?

推荐答案

请使用 paramiko-1.7.7.1pycrypto-2.6 找到一些代码="https://github.com/paramiko/paramiko/blob/master/demos/forward.py" rel="noreferrer">forward.py 脚本,我确实从中删除了第 115 行的代码到结束(以避免选项解析).

Please find some code using paramiko-1.7.7.1, pycrypto-2.6 and the forward.py script from which I did remove code from the line 115 to the end (to avoid options parsing).

import paramiko, sys
from forward import forward_tunnel

remote_host = "target_host"
remote_port = 8000
local_port  = 8000
ssh_host    = "my_ssh_host"
ssh_port    = 22

user     = "login"
password = "s3cr3t"

transport = paramiko.Transport((ssh_host, ssh_port))

# Command for paramiko-1.7.7.1
transport.connect(hostkey  = None,
                  username = user,
                  password = password,
                  pkey     = None)

try:
    forward_tunnel(local_port, remote_host, remote_port, transport)
except KeyboardInterrupt:
    print 'Port forwarding stopped.'
    sys.exit(0)

我已经在 Windows 工作站上成功测试了它,使用 Red Hat 下的 ssh 服务器并指向第三台服务器.(我使用的是 Python 2.7.2)

I've tested it successfully from a Windows station, using a ssh server under Red Hat and pointing to a 3rd server. (I'm using Python 2.7.2)

希望能帮到你,

这篇关于使用 Paramiko 进行端口转发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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