summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornoah <noah@656d521f-e311-0410-88e0-e7920216d269>2004-03-02 01:51:10 +0000
committernoah <noah@656d521f-e311-0410-88e0-e7920216d269>2004-03-02 01:51:10 +0000
commit0602e873b88c649fb9bf016c5833b4be1bb415ba (patch)
treebbfa8c712204248971ccbc9caf99d8bfde953ede
parent3f0ca36375a04b52bf396402ada8836d9c6854e0 (diff)
downloadpexpect-0602e873b88c649fb9bf016c5833b4be1bb415ba.tar.gz
expect() and expect_exact() originally would only preserve the 'before' and
'after' only if the exception was in the pattern list. Otherwise the exception would cause 'before' and 'after' to be lost. I saw no reason not to preserve 'before' and 'after'. git-svn-id: http://pexpect.svn.sourceforge.net/svnroot/pexpect/trunk@214 656d521f-e311-0410-88e0-e7920216d269
-rw-r--r--pexpect/examples/ssh_tunnel.py28
-rw-r--r--pexpect/pexpect.py24
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