这是我的代码 -
import subprocess
import sys
HOST="xyz3511.uhc.com"
# Ports are handled in ~/.ssh/config since we use OpenSSH
COMMAND="uptime"
ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND],
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
result = ssh.stdout.readlines()
if result == []:
error = ssh.stderr.readlines()
print (sys.stderr, "ERROR: %s" % error)
else:
print (result)
这就是我遇到的错误-
错误:
[b"'ssh' 未被识别为内部或外部命令,\r\n",
b'可运行的程序或批处理文件。\r\n']。
不知道我在这里做错了什么。另外,我没有提到任何端口。我想要的只是使用子进程并连接到远程服务器,执行一个简单的命令,例如ls
。 Python版本是3.x。
显然这发生在python3中。
在此链接中找到的解决方法:https://gist.github.com/bortzmeyer/1284249 https://gist.github.com/bortzmeyer/1284249
system32 = os.path.join(os.environ['SystemRoot'], 'SysNative' if platform.architecture()[0] == '32bit' else 'System32')
ssh_path = os.path.join(system32, 'OpenSSH/ssh.exe')
out1, err1 = Popen([ssh_path, "pi@%s"%self.host, "%s"%cmd],
shell=False,
stdout=PIPE,
stderr=PIPE).communicate()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)