简体   繁体   English

使用子进程从python执行时ssh不被识别为命令?

[英]ssh not recognized as a command when executed from python using subprocess?

This is my code - 这是我的代码 -

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)

and this is the error I'm getting- 这是我得到的错误 -

ERROR: [b"'ssh' is not recognized as an internal or external command,\\r\\n", b'operable program or batch file.\\r\\n']. 错误:[b“'ssh'不被识别为内部或外部命令,\\ r \\ n”,b'可操作程序或批处理文件。\\ r \\ n']。

Not sure what I'm doing wrong over here. 不知道我在这里做错了什么。 Also, I haven't mentioned any port. 另外,我还没有提到任何端口。 All I want is to use subprocess and connect to remote server, execute a simple command like ls . 我想要的只是使用子进程并连接到远程服务器,执行像ls这样的简单命令。 Python version is 3.x. Python版本是3.x.

Apparently this happens in python3. 显然这发生在python3中。 Workaround found at this link: 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过python执行时无法识别批处理命令 - Batch command not recognized when executed through python 使用 Python 子进程为后续 ssh 命令保持 SSH 连接处于活动状态 - Keep SSH connection alive for subsequent ssh command using Python subprocess Python subprocess.check_output 从线程执行时挂起 - Python subprocess.check_output hangs when executed from thread 为 Python subprocess.Popen 打印已执行的命令 - Print executed command for Python subprocess.Popen 在python子进程中使用ssh时如何跳过横幅消息 - How to skip banner message when using ssh inside python subprocess python子进程不适用于ssh命令 - python subprocess doesn't work with ssh command 优雅地中止远程 Windows 命令从 Windows SSH 执行 - Gracefully abort remote Windows command executed over SSH from Windows Python Paramiko script when Ctrl+C is pressed 为什么在虚拟环境中使用 subprocess.run() 执行“python -m pip”时会运行错误的 pip? - Why does 'python -m pip' runs the wrong pip when executed using subprocess.run() from a virtual environment? 检索子进程执行的命令 - Retrieving the command executed by subprocess 使用subprocess.call时,有什么方法可以获取执行的完整命令行? - Is there any way to get the full command line that's executed when using subprocess.call?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM