Issue with pexpect, python module.

Pexpect fails to do expected behavior if the length of the command is greater than 65.

import pexpect

sshCmd='ssh root@'
prompt='[root@user ~]#'
ConnectToClient.send('echo abcdefghijklmnopqrst111111111111111111111111111111111111111111111111111111111 ')
ConnectToClient.expect(prompt) ---> PASS
command = 'echo abcdefghijklmnopqrst111111111111111111111111111111111111111111111111111111111'
ConnectToClient.expect(command, prompt) 

While expecting ":", the command (.*) prompt ---> FAILS if the length of command is greater than 65.

expect_str = "%s(.*)%s" % (command, self._default_prompt)    
  • OP has asked same question on the pexpect development page over a month ago on GitHub, but no reply. github.com/pexpect/pexpect/issues/225
    – steve
    Aug 5 '15 at 6:32
  • I'm going to preface this answer by saying it's been a long time since I've had to consider such things but my memory is thinking that there is a window size associated with your expect script. If the terminal isn't the same size when you run the script as when you recorded it, there will be issues. I can't seem to find links to support this but I'm 99% sure I found the answer years ago wandering the darkened back alleys of IBM Developerworks.
    – todd_dsm
    Apr 25 at 5:27
  • @steve He added another comment there also
    – user449077
    Apr 25 at 5:33

There are 2 things wrong with your program. The last line is illegal:

ConnectToClient.expect(command, prompt) 

is not valid, as expect takes as 2nd argument a timeout number, not a string.

The other problem is that in the call:


prompt should be a regex (regular expression). You provide a string, which will be compiled into a regex. However, in a regex the character [ is special so you need to escape it. Either set

prompt = r'\[root@user ~\]#'

or instead of using .expect() use .expect_exact() which wants strings, not regexes.


By default pexpect windowsize is set to (24 rows and 80 coloumns) in my case, when length of prompt and command exceeds 80 . I face this issue. By changing the setwinsize(24,80) to setwinsize(24,150), It works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.