[英]ssh not recognized as a command when executed from python using subprocess?
這是我的代碼 -
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
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.