pexpect連接ssh實際失敗,但是回傳成功
連接情況:
wangyy@kali:~/dsa/1024$ ssh [email protected] -i /home/wangyy/dsa/1024/86f91e7f1f68b392e33488c5f3a33136-13382 -o PasswordAuthentication=no
ssh: connect to host 192.168.187.129 port 22: Connection refused
wangyy@kali:~/dsa/1024$
代碼:
try:
perm_denied = 'Permission denied'
ssh_newkey = 'Are you sure you want to continue'
conn_closed = 'Connection closed by remote host'
conn_refused = 'ssh: connect to host 192.168.187.129 port 22: Connection refused'
opt = ' -o PasswordAuthentication=no'
connStr = 'ssh '+ user + '@' + host + ' -i ' + keyfile + opt
child = pexpect.spawn(connStr)
ret = child.expect([pexpect.TIMEOUT,perm_denied,ssh_newkey,conn_closed,conn_refused,'$','#', ])
if ret == 2:
print('Adding host to knownhosts')
child.sendline('yes')
connect(user, host, keyfile,False)
elif ret == 3:
print('connection closed by remote host')
Fails += 1
elif ret == 4:
print('connection refused by remote host')
elif ret > 4:
print(child.before)
print("Success "+str(keyfile))
Stop = True
運行,每次都會走到ret>4, debug ret的值為$.然后就判定為登錄成功了。
但是實際上登錄是失敗的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/242053.html
上一篇:python 現在去哪找外包啊
下一篇:go無法編譯
