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)    

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.