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
#1
under linux bash, I build ssh tunnel this way:
ssh -ND 5545 -vvv -p 443 user@server

Output:
[...] user@server'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 %s@%s" % (user,host))
   i = child.expect([pexpect.TIMEOUT, 'password: '])
   if i == 0: #timeout
      print( "ssh connection timeout")
      sys.exit(1)
   if i == 1:
      print("ssh connection ok, sending password...")
      child.sendline(password)
      i = child.expect([pexpect.TIMEOUT, 'pledge: network'])
      if i==0:
         print("password timeout issue")
         sys.exit(1)
   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__':
   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 !
Reply


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

Forum Jump:

User Panel Messages

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