Skip to content Skip to sidebar Skip to footer

Need Little Assistance With Pexpect Interaction

Need little assistance with making this code work with pexpect module. This code executes git pull by logging into a server and then downloads the latest code (if upgrade is availa

Solution 1:

This logic seems to be a bit incorrect.

At first: you are expecting from a set of prompts

i = p.expect(['password:','Already up-to-date.',pexpect.EOF])

Which works correctly as password prompt is first to be sent. Then even if you receive the prompt - 'Already up-to-date.'

you have altered pexpect to check for 'password denied'

output = p.expect('Permission denied')

I believe that any point only one of the expect lines are active. Also you can not set expectations after a prompt has occurred.

You have to alter the script to sequentially check on the expected lines

i = p.expect(['password:','Already up-to-date.','Permission denied', pexpect.EOF])
ifi== 0:
   .... send passwordi= p.expect(['password:','Already up-to-date.','Permission denied', pexpect.EOF])
ifi== 2:
   .... send passwordi= p.expect(['password:','Already up-to-date.','Permission denied', pexpect.EOF])
ifi== 1:
   .... print "already updated"
   i = p.expect(['password:','Already up-to-date.','Permission denied', pexpect.EOF])
ifi== 3:
   .... print output

Basically, you have at any point only one active expect command active. if you are expecting a line "xyz" and it receives "123". It will just time out waiting for "xyz".

Post a Comment for "Need Little Assistance With Pexpect Interaction"