Search code examples
pythonpython-2.7pexpect

pexpect sometimes hit, sometimes fails and exit on timeout


I'm writing a small automation program to connect through ssh to a remote server, activate a PERL script and interact with the script (that generate menus for the user to choose from). I have a user predefined list of responses, and I've gone through the PERL script and jotted down the menus prompts in order to use it in "pexpect". now, i have:

global MYTIMEOUT
print "starting building"
child.sendline('/root/myscripts/perl/_build_.pl')
child.expect('username',timeout=MYTIMEOUT)
child.sendline(value_dict['myname'])
child.expect('reason',timeout=MYTIMEOUT)
child.sendline(value_dict['myreason'])
child.expect(value_dict["branch"],timeout=MYTIMEOUT)
child.sendline(branch_number)
child.expect('enter revision',timeout=MYTIMEOUT)
child.sendline('\n')
child.expect('choose customers',timeout=MYTIMEOUT)
child.sendline(value_dict['customers'])
child.expect('choose number',timeout=MYTIMEOUT)
child.sendline(value_dict['component_list'])

So the expect with the username,reason,branch and revision all work fine, and when I open the log file of the child, I can see it clearly that they respond to their correct lines. after this there's some code being executed by the PERL script, around 5-6 seconds, in which he outputs to the shell information for the person running it, after which, I get to 'choose customers' and 'choose number' the sendline ignores what I wrote, and for customers sends a [return] value, and when it gets to expect choose number, it doesn't send anything, and eventually times out.

Any way to force the sendline to obey me? or make sure the expect catches as it should catch the pattern?

EDIT: added information


Solution

  • the problem was child.sendline('\n'), the program expected and enter key pressed, but going through the logs closely i saw that in the automation log, i got two newlines, while in the normal output, i had one newline.

    it fucked up with the rest of the flow after that and that's why i got weird nothings and newline.

    changing it to child.sendline('') solved it.

    thanks for anyone who tried.