我试图复制命令
ssh -L8080:127.0.0.1:80 example.com
和
ssh -R8080:127.0.0.1:80 example.com
使用python库sshtunnel
(https://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
为什么不使用子进程并在shell中运行命令?
for example:
import subprocess
p = subprocess.Popen(["YOUR_COMMAND"], shell=True, stdout=subprocess.PIPE)
p.communicate()
这是一个建议,如果只运行命令或者你需要做额外的操作,还要依赖你需要做什么!