我正在尝试使用pexpect测试SSH是否存在于SSH上。我已经得到了大部分代码的工作,但我需要捕捉值,以便我可以断言文件是否存在。我所做的代码如下:通过ssh验证文件是否存在
def VersionID():
ssh_newkey = 'Are you sure you want to continue connecting'
# my ssh command line
p=pexpect.spawn('ssh [email protected]')
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('[email protected]:')
p.sendline("cd /opt/ad/bin")
i=p.expect('[email protected]:')
p.sendline('[ -f email_tidyup.sh ] && echo "File exists" || echo "File does not exists"')
i=p.expect('File Exists')
i=p.expect('[email protected]:')
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” –
我已经发布了我的答案在下面的这个问题。谢谢 – chrissygormley