简体   繁体   中英

Telnet over ssh tunnel in python

server('10.42.102.11') ←————→ device('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()

That results:

 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

I cannot find what I am doing wring here (manually connection works well)

When you forward ports you generally connect to a local address that is forwarded to a remote service. Here you're trying to connect to 192.168.253.205 which is not reachable (and isn't made reachable by the tunnel alone). I think the piece you're missing is the local_bind_address to give you the local port to connect to.

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)

This should allow you to connect to the local port 10022 (or whatever available port you choose) which will be forwarded to 2323 on the remote machine:

telnet.open('127.0.0.1', 10022)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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