diff options
author | noah <noah@656d521f-e311-0410-88e0-e7920216d269> | 2010-07-11 01:54:48 +0000 |
---|---|---|
committer | noah <noah@656d521f-e311-0410-88e0-e7920216d269> | 2010-07-11 01:54:48 +0000 |
commit | 1e21b116cd537e40eb0becf0110e34b3b24794dd (patch) | |
tree | 280b684f1fffffc74e8d6b329e0bad2cd2efdb31 /pexpect/pxssh.py | |
parent | de2c4c0e45f0ea8c9a20fddf4c7b52c27bb7f3f5 (diff) | |
download | pexpect-1e21b116cd537e40eb0becf0110e34b3b24794dd.tar.gz |
Added a fix to ANSI.py by Shane Kerr.
Also found a bug in support for Unicode.
I was not able to fix it at the time.
Unicode is still a pain in the ass.
git-svn-id: http://pexpect.svn.sourceforge.net/svnroot/pexpect/trunk@521 656d521f-e311-0410-88e0-e7920216d269
Diffstat (limited to 'pexpect/pxssh.py')
-rw-r--r-- | pexpect/pxssh.py | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/pexpect/pxssh.py b/pexpect/pxssh.py index 6e1fc55..5856b2c 100644 --- a/pexpect/pxssh.py +++ b/pexpect/pxssh.py @@ -30,10 +30,10 @@ class pxssh (spawn): shells. Example that runs a few commands on a remote server and prints the result:: - + import pxssh import getpass - try: + try: s = pxssh.pxssh() hostname = raw_input('hostname: ') username = raw_input('username: ') @@ -71,11 +71,12 @@ class pxssh (spawn): """ def __init__ (self, timeout=30, maxread=2000, searchwindowsize=None, logfile=None, cwd=None, env=None): + spawn.__init__(self, None, timeout=timeout, maxread=maxread, searchwindowsize=searchwindowsize, logfile=logfile, cwd=cwd, env=env) self.name = '<pxssh>' - - #SUBTLE HACK ALERT! Note that the command to set the prompt uses a + + #SUBTLE HACK ALERT! Note that the command that SETS the prompt uses a #slightly different string than the regular expression to match it. This #is because when you set the prompt the command will echo back, but we #don't want to match the echoed command. So if we make the set command @@ -98,7 +99,7 @@ class pxssh (spawn): # Unsetting SSH_ASKPASS on the remote side doesn't disable it! Annoying! #self.SSH_OPTS = "-x -o'RSAAuthentication=no' -o 'PubkeyAuthentication=no'" self.force_password = False - self.auto_prompt_reset = True + self.auto_prompt_reset = True def levenshtein_distance(self, a,b): @@ -175,7 +176,7 @@ class pxssh (spawn): to guess when we have reached the prompt. Then we hope for the best and blindly try to reset the prompt to something more unique. If that fails then login() raises an ExceptionPxssh exception. - + In some situations it is not possible or desirable to reset the original prompt. In this case, set 'auto_prompt_reset' to False to inhibit setting the prompt to the UNIQUE_PROMPT. Remember that pxssh @@ -196,7 +197,7 @@ class pxssh (spawn): i = self.expect(["(?i)are you sure you want to continue connecting", original_prompt, "(?i)(?:password)|(?:passphrase for key)", "(?i)permission denied", "(?i)terminal type", TIMEOUT, "(?i)connection closed by remote host"], timeout=login_timeout) # First phase - if i==0: + if i==0: # New certificate -- always accept it. # This is what you get if SSH does not have the remote host's # public key stored in the 'known_hosts' cache. @@ -214,14 +215,14 @@ class pxssh (spawn): # This is weird. This should not happen twice in a row. self.close() raise ExceptionPxssh ('Weird error. Got "are you sure" prompt twice.') - elif i==1: # can occur if you have a public key pair set to authenticate. + elif i==1: # can occur if you have a public key pair set to authenticate. ### TODO: May NOT be OK if expect() got tricked and matched a false prompt. pass elif i==2: # password prompt again # For incorrect passwords, some ssh servers will # ask for the password again, others return 'denied' right away. # If we get the password prompt again then this means - # we didn't get the password right the first time. + # we didn't get the password right the first time. self.close() raise ExceptionPxssh ('password refused') elif i==3: # permission denied -- password was bad. @@ -241,7 +242,7 @@ class pxssh (spawn): elif i==6: # Connection closed by remote host self.close() raise ExceptionPxssh ('connection closed') - else: # Unexpected + else: # Unexpected self.close() raise ExceptionPxssh ('unexpected login response') if not self.sync_original_prompt(): @@ -267,20 +268,25 @@ class pxssh (spawn): self.expect(EOF) self.close() - def prompt (self, timeout=20): + def prompt (self, timeout=-1): """This matches the shell prompt. This is little more than a short-cut to the expect() method. This returns True if the shell prompt was - matched. This returns False if there was a timeout. Note that if you - called login() with auto_prompt_reset set to False then you should have - manually set the PROMPT attribute to a regex pattern for matching the - prompt. """ + matched. This returns False if a timeout was raised. Note that if you + called login() with auto_prompt_reset set to False then before calling + prompt() you must set the PROMPT attribute to a regex that prompt() + will use for matching the prompt. Calling prompt() will erase the + contents of the 'before' attribute even if no prompt is ever matched. + If timeout is not given or it is set to -1 then self.timeout is used. + """ + if timeout == -1: + timeout = self.timeout i = self.expect([self.PROMPT, TIMEOUT], timeout=timeout) if i==1: return False return True - + def set_unique_prompt (self): """This sets the remote prompt to something more unique than # or $. |