python: subprocess.Popen(["ssh", 无法连接

python: subprocess.Popen(["ssh", 无法连接

以下是我在 python3 中测试的代码片段:

ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND],
                   shell=False,
                   stdout=subprocess.PIPE,
                   stderr=subprocess.PIPE)

subprocess.Popen(['ssh', "me" + '@' + 123.45.67.891, '-p', "22"]

当我运行它时,它会一遍又一遍地要求输入密码:

Password:
Password:
Password:

我测试了连接,这有效:

ssh [email protected] "uname -a"

注意:尽量避免为简单的事情安装更多的库

编辑:这是完整的代码:

#!/usr/bin/python3
import subprocess
import sys

HOST="123.45.67.891"
# Ports are handled in ~/.ssh/config since we use OpenSSH
COMMAND="uname -a"

ssh = subprocess.run(["ssh", "123.45.67.891", "uname -a"],
                    shell=False,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE,
                    check=False)

result = ssh.stdout.readlines()
if result == []:
 error = ssh.stderr.readlines()
 print >>sys.stderr, "ERROR: %s" % error
else:
 print (result)

EDIT2:当我 cntl-C 时,我得到这个:

Password: Traceback (most recent call last):
File "./sshtest2.py", line 13, in <module>
check=False)
File "/usr/lib/python3.6/subprocess.py", line 405, in run
stdout, stderr = process.communicate(input, timeout=timeout)
File "/usr/lib/python3.6/subprocess.py", line 843, in communicate
stdout, stderr = self._communicate(input, endtime, timeout)
File "/usr/lib/python3.6/subprocess.py", line 1514, in _communicate
ready = selector.select(timeout)
File "/usr/lib/python3.6/selectors.py", line 376, in select
fd_event_list = self._poll.poll(timeout)
KeyboardInterrupt

相关内容