我有以下代碼(更新包括Pexpect的):如何獲取密碼提示
import sys
import subprocess
import pexpect
print "0"
ssh = subprocess.Popen("ssh -A -t [email protected] ssh -A -X [email protected]",
shell = True,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE).communicate()
print "1"
child = pexpect.spawn(ssh)
print "2"
child.expect ('password')
print "3"
child.sendline ('password2')
print "4"
result = ssh.stdout.readlines()
if result == []:
error = ssh.stderr.readlines()
print >>sys.stderr, "ERROR: %s" % error
else:
print result
當我運行它,我看到了零印刷,然後在屏幕上顯示的密碼提示。打印的行永遠不會執行,因此下面的代碼也不會執行。我可以以用戶身份輸入密碼,但會掛起。當我用Ctrl + C殺死它時,在返回到命令提示符之前,第二個登錄橫幅出現第二個密碼提示。有人可以解釋如何捕獲第一密碼提示,以便程序可以發送密碼,而不是用戶?另外,有人能解釋爲什麼我沒有得到結果變量,直到我殺了程序?
我建議改用書面密碼ssh_keys。你的生活會更輕鬆,同時更安全。 – mosh442