telnet在python中的ssh隧道

时间:2017-10-10 14:03:33

标签: python python-2.7 ssh telnet ssh-tunnel

服务器('10 .42.102.11')←----→设备('192.168.253.205')

|

SSH(22)

|

localhost(me)

with SSHTunnelForwarder(
        ('10.42.102.11', 22),
        ssh_username="serv_usr",
        ssh_password="serv_pass",
        remote_bind_address=('192.168.253.205', 2323),
) as tunnel:
    telnet = Telnet()
    #telnet.open('127.0.0.1', 2323)
    telnet.open('192.168.253.205', 2323)
    telnet.close()

结果:

 File "/usr/lib/python2.7/telnetlib.py", line 227, in open
    self.sock = socket.create_connection((host, port), timeout)
  File "/usr/lib/python2.7/socket.py", line 571, in create_connection
    raise err
socket.error: [Errno 110] Connection timed out

我找不到我在做什么(手动连接效果很好)

1 个答案:

答案 0 :(得分:2)

转发端口时,通常会连接到转发到远程服务的本地地址。在这里,您尝试连接到无法访问的192.168.253.205(并且仅通过隧道无法访问)。我认为您缺少的是local_bind_address,以便为您提供连接的本地端口。

with SSHTunnelForwarder(
    ('10.42.102.11', 22),
    ssh_username="serv_usr",
    ssh_password="serv_pass",
    remote_bind_address=('192.168.253.205', 2323),
    local_bind_address=('0.0.0.0', 10022)

这应该允许您连接到本地端口10022(或您选择的任何可用端口),它将被转发到远程计算机上的2323:

telnet.open('127.0.0.1', 10022)