返回通过嵌入在 python 内的 expect 命令中的 ssh 远程执行的 grep 命令的状态
Returning status of grep command executed remotely via ssh embedded in expect command inside python
我有一个下面的 python 脚本,我在其中使用 expect 执行远程 SSH 命令。在这里,即使目标文件包含字符串“error”或不包含,退出代码总是作为成功返回,因为 ssh 连接是唯一的检查。如何获取 grep 命令的状态?
#! /usr/bin/python
import subprocess
def execute(cmd):
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
_output, _error = proc.communicate()
_code = proc.returncode
return _output, _error, _code
host = "localhost"
passwd = "kube"
cmd="/usr/bin/expect -c 'spawn ssh "+host+" \"cat /home/kube/f1 | grep -qi error\"; expect \"password:\" { send \""+passwd+"\r\"} ;interact' "
_output, _error, return_code = execute(cmd)
print cmd + "\n" + _output + "\n" + _error
if (return_code == 0):
print "no error"
else:
print "contains error"
选项 1:
让远程命令为你输出一些指示success/failure的东西。例如:
ssh user@host "/some/command | grep -q some-string && echo MAGIC-SUCCESS || echo MAGIC-FAILURE"
并且在 Python 中您可以获得输出并解析它。
选项 2:
根据man expect
:
wait [args]
[...] wait
normally returns a list of four integers. The first integer is the pid of the process that was waited upon. The second integer is the corresponding spawn id. The third integer is -1
if an operating system error occurred, or 0
otherwise. If the third integer was 0
, the fourth integer is the status returned by the spawned process. If the third integer was -1
, the fourth integer is the value of errno
set by the operating system. [...]
因此您的 Expect 代码可以检查 wait
结果,然后 exit
具有不同的值,并且 Python 代码可以获得退出状态。
对于 Expect 部分,它是这样的:
spawn ...
...
expect eof
set result [wait]
exit [lindex $result 3]
我有一个下面的 python 脚本,我在其中使用 expect 执行远程 SSH 命令。在这里,即使目标文件包含字符串“error”或不包含,退出代码总是作为成功返回,因为 ssh 连接是唯一的检查。如何获取 grep 命令的状态?
#! /usr/bin/python
import subprocess
def execute(cmd):
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
_output, _error = proc.communicate()
_code = proc.returncode
return _output, _error, _code
host = "localhost"
passwd = "kube"
cmd="/usr/bin/expect -c 'spawn ssh "+host+" \"cat /home/kube/f1 | grep -qi error\"; expect \"password:\" { send \""+passwd+"\r\"} ;interact' "
_output, _error, return_code = execute(cmd)
print cmd + "\n" + _output + "\n" + _error
if (return_code == 0):
print "no error"
else:
print "contains error"
选项 1:
让远程命令为你输出一些指示success/failure的东西。例如:
ssh user@host "/some/command | grep -q some-string && echo MAGIC-SUCCESS || echo MAGIC-FAILURE"
并且在 Python 中您可以获得输出并解析它。
选项 2:
根据man expect
:
wait [args]
[...]
wait
normally returns a list of four integers. The first integer is the pid of the process that was waited upon. The second integer is the corresponding spawn id. The third integer is-1
if an operating system error occurred, or0
otherwise. If the third integer was0
, the fourth integer is the status returned by the spawned process. If the third integer was-1
, the fourth integer is the value oferrno
set by the operating system. [...]
因此您的 Expect 代码可以检查 wait
结果,然后 exit
具有不同的值,并且 Python 代码可以获得退出状态。
对于 Expect 部分,它是这样的:
spawn ...
...
expect eof
set result [wait]
exit [lindex $result 3]