我有一个必须在其中收集环境中多台计算机的重新启动统计信息的要求。我正在将fabric
用于其他任务,因此我也想对fabric
进行此操作。下面是我的代码。
from fabric.api import *
env.hosts = ['testbox']
@task
@parallel
def runner():
run("last -xF|awk '{print $6$8}'|head -10")
execute(runner)
输出
[testbox] Executing task 'runner'
[testbox] run: last -xF|awk '{print $6$8}'|head -10
[testbox] out: 32018
[testbox] out: 32018
[testbox] out: 32018
[testbox] out: 32018
[testbox] out: 32018
[testbox] out: 32018
[testbox] out: 32018
[testbox] out: 32018
[testbox] out: 32018
[testbox] out: 32018
但是,如果我将代码更改为以下代码,将无法再使用
from fabric.api import *
env.hosts = ['testbox']
@task
@parallel
def runner():
run("last -xF|awk '/boot/{print $6$8}'|head -10")
execute(runner)
输出
[testbox] Executing task 'runner'
[testbox] run: last -xF|awk '/boot/{print $6$8}'|head -10
我不确定为什么会这样,我也尝试过转义\
,但即使这样也没有用。注意,以上命令在bash中也可以正常工作。
grep
也发生了同样的事情。
from fabric.api import *
env.hosts = ['testbox']
@task
@parallel
def runner():
run("last -xF|grep boot")
execute(runner)
输出
Fatal error: run() received nonzero return code 1 while executing!
Requested: last -xF|grep boot
Executed: /bin/bash -l -c "last -xF|grep boot"
Aborting.
编辑1
我对其进行了更多调试,发现问题不在于fabric
,而是与ssh
连接本身有关。因此,我正在对其进行更多研究,一旦完成将对其进行更新。