我的代码运行 linux 命令向远程 linux 机器发送关机信号。在linux终端上运行命令成功关闭远程机器,但是使用Pythonsubprocess
调用命令会报错。
代码:
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
问题。
我错过了什么?
更新:尚未解决上述问题,但由于某种原因使用subprocess.check_output
而不是check_call
成功执行命令,尽管返回non-zero exit status 255