Python Forum
ssh tunnel connection dropped with Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ssh tunnel connection dropped with Python
under linux bash, I build ssh tunnel this way:
ssh -ND 5545 -vvv -p 443 [email protected]

[...] [email protected]'s password: [...] debug1: pledge: network
From there, I know my tunnel is established. If no verbose mode used, 'netstat -tl' command confirms the tunnel has been established. I can perfectly automatize this with bash expect utility (skiping the fingerprint acknowledgement when first time connection)

Now I am trying to do the same thing in python:
import pexpect, sys

def ssh_con(host,user,password):
   child = pexpect.spawn("ssh -ND 5545 -vvv -p 443 %[email protected]%s" % (user,host))
   i = child.expect([pexpect.TIMEOUT, 'password: '])
   if i == 0: #timeout
      print( "ssh connection timeout")
   if i == 1:
      print("ssh connection ok, sending password...")
      i = child.expect([pexpect.TIMEOUT, 'pledge: network'])
      if i==0:
         print("password timeout issue")
   elif i==1:
        print("tunnel sould be up and running now")

def main():       
  host = '<server_ip>'
  user = '<username>'
  password = '<password>'

  child = ssh_con( host, user, password)

if __name__ == '__main__':
All goes fine, until the script reaches the end ('tunnel sould be up and running now'), where it exits / hangs up instead of keeping the tunnel up and running.

Thanx folks !

Possibly Related Threads…
Thread Author Replies Views Last Post
  telnet from ssh tunnel oldfart 3 3,456 Jul-17-2020, 02:28 PM
Last Post: noobami99
  Building SSH tunnel? searching1 0 702 Apr-17-2020, 08:39 PM
Last Post: searching1
  Python server(Django web site)/client(Python app) connection Junior_Pythoneer 5 1,748 Jul-05-2019, 05:41 PM
Last Post: noisefloor
  Python // C # - SOCKET connection is constantly interrupted raspberryPiBRA 0 1,573 Feb-01-2018, 09:53 AM
Last Post: raspberryPiBRA

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020