在使用pexpect模块使用此代码时,几乎没有什么帮助。
此代码通过登录服务器执行git pull,然后下载最新代码(如果可以升级)或只发送一条消息“已经是最新的”。
代码实际上标识了密码屏幕,但没有标识“已经是最新”的文本
不确定我是否遗漏了任何东西。
代码中的代码段是:
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)
非常感谢任何帮助。
谢谢, -Vijay
答案 0 :(得分:1)
这种逻辑似乎有点不正确。
首先:您期待一组提示
i = p.expect(['password:','Already up-to-date.',pexpect.EOF])
首先发送密码提示正常工作。然后即使你收到提示 - “已经是最新的”。
您已更改pexpect以检查“密码被拒绝”
output = p.expect('Permission denied')
我相信只有一条期望线是活跃的。在提示发生后,您也无法设置期望值。
您必须更改脚本以按顺序检查预期的行
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
基本上,您在任何时候只有一个活动的expect命令处于活动状态。如果你期望一行“xyz”,它会收到“123”。它只会等待“xyz”。