使用子进程连接到远程计算机



我想连接到远程机器,也想在那里执行某些命令。

详:

远程机器:它是一台 Linux 机器。

本地机器:它是Windows 7机器。

我的Python代码在Windows机器上。

import subprocess
import sys
HOST="eseki.rnd.sozi.se"
# Ports are handled in ~/.ssh/config since we use OpenSSH
COMMAND="uname -a"
ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND],
                       shell=False,
                       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

但是,当我执行此代码时,我收到以下错误消息:

Traceback (most recent call last):
  File "C:UsersABCworkspaceHelloWorldPythonsrcSSHTestModule.py", line 16, in <module>
    stderr=subprocess.PIPE)
  File "C:Python27libsubprocess.py", line 710, in __init__
    errread, errwrite)
  File "C:Python27libsubprocess.py", line 958, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

我应该怎么做才能使用 Python 连接到远程计算机?

本地计算机上没有安装 ssh 客户端,或者它不在 PATH 变量中。

无论哪种方式,我都建议您查看专门为ssh设计的库,而不是使用子进程(例如,http://www.paramiko.org/)

最新更新