0

Issue with pexpect, python module.

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

#!/usr/bin/python
import pexpect

sshCmd='ssh root@15.186.22.105'
prompt='[root@user ~]#'
ConnectToClient=pexpect.spawn(sshCmd)
ConnectToClient.expect('password')
ConnectToClient.send('abcd')
ConnectToClient.expect(prompt)
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)    
0

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:

ConnectToClient.expect(prompt)

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.

0

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.