使用python sshtunnel进行隧道

时间:2018-09-03 10:03:04

标签: python ssh-tunnel

我正在尝试复制命令

ssh -L8080:127.0.0.1:80 example.com

ssh -R8080:127.0.0.1:80 example.com 

使用python库sshtunnelhttps://github.com/pahaz/sshtunnel/)。

所以我已经弄清楚了

ssh -L8080:127.0.0.1:80 example.com

成为

server = SSHTunnelForwarder(
'example.com',
ssh_username="username",
ssh_password="password",
remote_bind_address=('127.0.0.1', 8080))

但是我仍然无法理解如何复制命令

ssh -R8080:127.0.0.1:80 example.com 

2 个答案:

答案 0 :(得分:0)

为什么不使用子进程并在shell中运行命令?

for example:

import subprocess
p = subprocess.Popen(["YOUR_COMMAND"], shell=True, stdout=subprocess.PIPE)
p.communicate()

这是一个建议,如果仅运行命令或您需要执行其他操作,则还取决于您需要使用它做什么!

答案 1 :(得分:0)

在我看来,您可能混淆了远程端口和本地端口。

尝试类似的东西:

server = SSHTunnelForwarder(
'example.com',
ssh_username="username",
ssh_password="password",
local_bind_address=('', 8080),
remote_bind_address=('127.0.0.1', 80))

如果要与所有接口共享远程80端口。

或者最好使用:

server = SSHTunnelForwarder(
'example.com',
ssh_username="username",
ssh_password="password",
local_bind_address=('127.0.0.1', 8080),
remote_bind_address=('127.0.0.1', 80))

仅共享本地接口。