bos*_*ari 5 python ssh subprocess python-3.x
这是我的代码 -
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)
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误-
错误: [b"'ssh' 不是内部或外部命令,\r\n",b'可运行的程序或批处理文件。\r\n']。
不知道我在这里做错了什么。另外,我没有提到任何端口。我想要的只是使用子进程并连接到远程服务器,执行一个简单的命令,如ls. Python 版本是 3.x。
小智 2
显然这发生在python3中。在此链接中找到的解决方法: 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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1079 次 |
| 最近记录: |