diff options
-rw-r--r-- | pexpect/examples/ssh_tunnel.py | 28 | ||||
-rw-r--r-- | pexpect/pexpect.py | 24 |
2 files changed, 31 insertions, 21 deletions
diff --git a/pexpect/examples/ssh_tunnel.py b/pexpect/examples/ssh_tunnel.py index 1147bb7..c6ad4dc 100644 --- a/pexpect/examples/ssh_tunnel.py +++ b/pexpect/examples/ssh_tunnel.py @@ -1,3 +1,10 @@ +#!/usr/bin/env python +"""This starts an SSH tunnel to a given host. +If the SSH process ever dies then this script will detect that and restart it. +I use this under Cygwin to keep open encrypted tunnels to +port 110 (POP3) and port 25 (SMTP). I set my mail client to talk to +localhost and I keep this script running in the background. +""" import pexpect import getpass import time @@ -6,7 +13,7 @@ tunnel_command = 'ssh -C -n -L 25:%(host)s:25 -L 110:%(host)s:110 %(user)s@%(hos nothing_script = """#!/bin/sh while true; do sleep 53; done """ -host = 'spruce.he.net' #'example.com' +host = raw_input('Hostname: ') user = raw_input('Username: ') X = getpass.getpass('Password: ') @@ -14,12 +21,13 @@ def start_tunnel (): ssh_tunnel = pexpect.spawn (tunnel_command % globals()) try: ssh_tunnel.expect ('password:') - except: + except Exception, e: + print str(e) print ssh_tunnel.before print ssh_tunnel.after time.sleep (0.1) ssh_tunnel.sendline (X) - time.sleep (60) + time.sleep (60) # Cygwin is slow to update process status. ssh_tunnel.expect (pexpect.EOF) while 1: @@ -27,15 +35,17 @@ while 1: time.sleep (1) index = ps.expect (['/usr/bin/ssh', pexpect.EOF, pexpect.TIMEOUT]) if index == 2: - print 'TIMEOUT in ps command...' + print 'TIMEOUT in ps command...' print ps.before - print ps.after - print '^^^^^' + print ps.after if index == 1: + print time.asctime(), print 'restarting tunnel' start_tunnel () - time.sleep (1) + time.sleep (60) else: - print 'tunnel OK' - time.sleep (1) + # print 'tunnel OK' + time.sleep (60) + + diff --git a/pexpect/pexpect.py b/pexpect/pexpect.py index c95bbde..82855ae 100644 --- a/pexpect/pexpect.py +++ b/pexpect/pexpect.py @@ -670,18 +670,18 @@ class spawn: incoming = incoming + c except EOF: + self.before = incoming + self.after = EOF if EOF in pattern_list: - self.before = incoming - self.after = EOF - self.buffer = '' + #self.buffer = '' return pattern_list.index(EOF) else: raise except TIMEOUT: + self.before = incoming + self.after = TIMEOUT if TIMEOUT in pattern_list: - self.before = incoming - self.after = TIMEOUT - self.buffer = '' + #self.buffer = '' return pattern_list.index(TIMEOUT) else: raise @@ -733,18 +733,18 @@ class spawn: incoming = incoming + c except EOF: + self.before = incoming + self.after = EOF if EOF in pattern_list: - self.before = incoming - self.after = EOF - self.buffer = '' + #self.buffer = '' return pattern_list.index(EOF) else: raise except TIMEOUT: + self.before = incoming + self.after = TIMEOUT if TIMEOUT in pattern_list: - self.before = incoming - self.after = TIMEOUT - self.buffer = '' + #self.buffer = '' return pattern_list.index(TIMEOUT) else: raise |