Need little assistance with pexpect interaction

≯℡__Kan透↙ 提交于 2019-12-11 09:32:27

问题


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 available) or just sends out a messages saying "Already up-to-date."

The code actually idenfies the password screen but does not identify the text "already up-to-date"

not sure if I'm missing anything here.

A snippet from the code is:

        p = pexpect.spawn('git pull',cwd = comp_dir,maxread = 10000, timeout = 100)
        i = p.expect(['password:','Already up-to-date.',pexpect.EOF])
        if i == 0:
            output_lines = p.before
            output_line_list = output_lines.split('\r\n')
            for line in output_line_list: print line
            count = 0
            p.sendline(pwd)
            while count < 3:  **# The server in case of unsuccessful login asks for password thrice so this check...  (not sure if there is a better way of achieving this)**
                try:
                    output = p.expect('Permission denied')
                    count+=1
                    p.sendline(pwd)
                    p.logfile = sys.stdout
                except:
                    print 'Successful Login !!!! ------'
                    p.expect('Already up-to-date',timeout=None)
                    count = 3
        if i == 1:
            output_lines = p.before
            output_line_list = output_lines.split('\r\n')
            for line in output_line_list: print line
            p.expect(pexpect.EOF)

Any help is greatly appreciated.

Thanks, -Vijay


回答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])
if i == 0:
   .... send password
   i = p.expect(['password:','Already up-to-date.','Permission denied', pexpect.EOF])
if i == 2:
   .... send password
   i = p.expect(['password:','Already up-to-date.','Permission denied', pexpect.EOF])
if i == 1:
   .... print "already updated"
   i = p.expect(['password:','Already up-to-date.','Permission denied', pexpect.EOF])
if i == 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".



来源:https://stackoverflow.com/questions/11296426/need-little-assistance-with-pexpect-interaction

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!