简体   繁体   English

Python paramiko/sshtunnel 代码在 linux 下工作正常,但在 Windows 下失败

[英]Python paramiko/sshtunnel code works fine under linux but fails under Windows

I have successfully gotten the following python paramiko/sshtunnel code to work properly under linux to tunnel to a port on a remote machine via an SSH tunnel.我已成功获得以下 python paramiko/sshtunnel 代码在 linux 下正常工作,通过 SSH 隧道隧道到远程机器上的端口。 However, running the exact, same python code under Windows 10 fails.但是,在 Windows 10 下运行完全相同的 python 代码会失败。 This is Python 3.9.5 in both cases.在这两种情况下,这都是 Python 3.9.5。

First, the code itself...首先,代码本身...

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.

And this is the error that keeps printing...这是不断打印的错误......

... retrying 0:0.0.0:53906 because of [WinError 10049] The requested address is not valid in its context ...由于 [WinError 10049] 正在重试 0:0.0.0:53906 请求的地址在其上下文中无效

The "53906" port is different each time I run this, of course.当然,每次我运行它时,“53906”端口都是不同的。

Also, under Windows 10, I can go to Power Shell while this python program is running, and I can run the following, in which case I indeed get connected to the port and see the remote data... Also, under Windows 10, I can go to Power Shell while this python program is running, and I can run the following, in which case I indeed get connected to the port and see the remote data...

telnet localhost 53906

This seems to imply that there is something about the way that python is trying to connect to that socket which is causing the error.这似乎暗示 python 尝试连接到导致错误的套接字的方式存在某些问题。

Can anyone see what I might need to change in my python code to get this to work properly under Windows 10?谁能看到我可能需要更改我的 python 代码以使其在 Windows 10 下正常工作?

Thank you very much.非常感谢。

I figured out the problem and fixed it.我发现了问题并修复了它。

These two lines return connecthost = '0.0.0.0' and a random value for connectport :这两行返回connecthost = '0.0.0.0' 和connectport的随机值:

        connecthost = remote.local_bind_host
        connectport = remote.local_bind_port

However, if I force connecthost to be '127.0.0.1' in the client.connect call, my code works fine.但是,如果我在client.connect调用中强制connecthost为“127.0.0.1”,我的代码可以正常工作。

I'm guessing that Windows 10 must handle '0.0.0.0' differently from how linux handles it.我猜 Windows 10 处理“0.0.0.0”的方式必须不同于 linux 的处理方式。 If so, then perhaps sshtunnel.open_tunnel under Windows should be changed to return '127.0.0.1' instead of '0.0.0.0'.如果是这样,那么sshtunnel.open_tunnel下的 sshtunnel.open_tunnel 可能应该更改为返回 '127.0.0.1' 而不是 '0.0.0.0'。

Anyway, this is working now.无论如何,这现在正在工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM