【发布时间】:2011-01-12 15:24:22
【问题描述】:
我正在尝试使用 pexpect 通过 SSH 测试文件是否存在。我已经让大部分代码工作了,但我需要捕获该值,以便我可以断言文件是否存在。我做的代码如下:
def VersionID():
ssh_newkey = 'Are you sure you want to continue connecting'
# my ssh command line
p=pexpect.spawn('ssh service@10.10.0.0')
i=p.expect([ssh_newkey,'password:',pexpect.EOF])
if i==0:
p.sendline('yes')
i=p.expect([ssh_newkey,'password:',pexpect.EOF])
if i==1:
p.sendline("word")
i=p.expect('service@main-:')
p.sendline("cd /opt/ad/bin")
i=p.expect('service@main-:')
p.sendline('[ -f email_tidyup.sh ] && echo "File exists" || echo "File does not exists"')
i=p.expect('File Exists')
i=p.expect('service@main-:')
assert True
elif i==2:
print "I either got key or connection timeout"
assert False
results = p.before # print out the result
VersionID()
感谢您的帮助。
【问题讨论】:
-
@chrissygormley - 我不认为你会考虑使用 ssh-agent 来存储你的密码?如果你这样做了,你可以完全避免 pexpect 而只是这样做: ssh user@host "command"
-
我已经在下面发布了我对这个问题的回答。谢谢
标签: python bash testing ssh pexpect