Search code examples
pythonunit-testingpexpectpxssh

How to use 'sync_original_prompt' from pxssh module


>>> from pexpect import pxssh
>>> s=pxssh.pxssh()
>>> s.login('IP','USER','PASSWORD',auto_prompt_reset=True)
True
>>> s.sendline('echo Test');
10
>>> s.prompt()
True
>>> s.after
'[PEXPECT]# '
>>> s.PROMPT='BTEQ -- Enter your SQL request or BTEQ command:'
>>> s.sendline('bteq .logon dbc,dbc')
20
>>> s.prompt()
True
>>> s.after
'BTEQ -- Enter your SQL request or BTEQ command:'
>>> s.sendline('quit;')
6
>>> s.prompt()
False
>>> s.PROMPT='[PEXPECT]# '
>>> s.prompt()
False
>>> s.sync_original_prompt()
True
>>> s.prompt()
False

As per my understanding after s.sync_original_prompt(), PROMPT variable should reset to initial prompt, which is [PEXPECT]# here, and s.prompt() should result in True. But it doesn't. Can anybody please tell me how to reset PROMPT variable to original prompt? and how to use sync_original_prompt attribute?

Thanks.


Solution

    • The initial s.PROMPT (a regexp string) is actually '\\[PEXPECT\\][\\$\\#] ', not [PEXPECT]#. I'd suggest you write like this:

      savePrompt = s.PROMPT
      
      s.PROMPT = 'a-new-prompt'
      s.sendline('bteq ...')
      s.prompt()                 # expect the new prompt
      
      s.PROMPT = savePrompt      # restore the saved prompt
      s.sendline('quit;')
      s.prompt()                 # expect the saved prompt
      
    • sync_original_prompt() should be used like this:

      s.sync_original_prompt() # this make sure you are at the prompt
      s.set_unique_prompt()    # change the shell's prompt to the default PROMPT
      s.sendline('run some command')
      s.prompt()