调用远程关闭命令退出状态6时的Python子进程CalledProcessError

时间:2017-08-02 11:58:37

标签: python linux ssh subprocess sshpass

我的代码运行linux命令将关机信号发送到远程linux机器。在linux终端上运行该命令会成功关闭远程计算机,但使用Python subprocess调用该命令会导致错误。

代码:

import subprocess
res = subprocess.check_call("sshpass -pPassword ssh root@192.168.1.2 sudo shutdown now", shell=True)

Python 2.7的结果:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/subprocess.py", line 541, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command 'sshpass -pPassword ssh root@192.168.1.2 sudo shutdown now' returned non-zero exit status 6

Python 3的结果:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/subprocess.py", line 581, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command 'sshpass -pPassword ssh root@192.168.1.2 sudo shutdown now' returned non-zero exit status 6

目标机器保持不变。

在命令行上单独运行sshpass -pPassword ssh root@192.168.1.2 sudo shutdown now成功并返回Connection to 192.168.1.2 closed.

这似乎可能是subprocess + sshpass问题。

我错过了什么?

更新: Haven没有解决上述问题但由于某些原因使用subprocess.check_output而不是check_call成功执行命令,尽管返回non-zero exit status 255

0 个答案:

没有答案