【发布时间】:2021-08-29 23:12:58
【问题描述】:
我已成功获得以下 python paramiko/sshtunnel 代码,以便在 linux 下正常工作,通过 SSH 隧道隧道到远程机器上的端口。但是,在 Windows 10 下运行完全相同的 python 代码会失败。在这两种情况下都是 Python 3.9.5。
首先,代码本身...
import sys
import json
import time
import queue
import socket
import paramiko
import sshtunnel
from threading import Thread
remotehost = 'remote-host-blah-blah-blah.net'
remoteport = 9999
pkeyfile = os.path.expanduser('~/.ssh/id_rsa')
def main():
pkey = paramiko.RSAKey.from_private_key_file(pkeyfile)
with sshtunnel.open_tunnel(
(remotehost, 22),
ssh_username='remoteusername',
ssh_pkey=pkey,
compression=True,
remote_bind_address=('0.0.0.0', remoteport)
) as remote:
connecthost = remote.local_bind_host
connectport = remote.local_bind_port
return runit(connecthost, connectport)
return 1
def runit(connecthost, connectport):
client = None
while True:
try:
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((connecthost, connectport))
output(f'\nconnected: {connecthost}:{connectport}')
break
except Exception as e:
time.sleep(1.0)
print(f'!!! retrying {connecthost}:{connectport} because of {e}')
# If we made it here, we are properly connected through
# the tunnel. This works under linux, but we never get
# here under Windows 10. The code which follows is not
# pertinent to this Stackoverflow question, so I have
# left out the remaining code in this program.
这是不断打印的错误...
!!!由于 [WinError 10049] 请求重试 0.0.0.0:53906 地址在其上下文中无效
当然,每次我运行它时,“53906”端口都是不同的。
另外,在 Windows 10 下,我可以在这个 python 程序运行时进入 Power Shell,我可以运行以下命令,在这种情况下,我确实连接到端口并查看远程数据...
telnet localhost 53906
这似乎暗示 python 尝试连接到导致错误的套接字的方式存在某些问题。
任何人都可以看到我可能需要更改我的 python 代码以使其在 Windows 10 下正常工作吗?
非常感谢。
【问题讨论】:
标签: python linux windows paramiko ssh-tunnel