0

ExecShellResult我在 cfengine v2.2.1a 中编写了以下片段:

control:
    active_interface_mac = ( ExecShellResult(/sbin/ifconfig ${active_interface} | /usr/bin/grep 'ether ' | /usr/bin/cut -f2 -d' ') )
    ...
    time_drift = ( ExecShellResult(/usr/sbin/ntpdate -d pool.ntp.org 2>/dev/null | /usr/bin/grep -o 'offset.*sec' | /usr/bin/cut -f2 -d' ') )
    ...
shellcommands:
    "/bin/echo ${time_drift}" inform=true syslog=true

从命令行运行上述内容时,它显然可以正常工作:

$ ntpdate ...
0.183693

但是,如果在 cfengine 中运行,我会收到语法错误:

$ cfagent -qIK
Executing script /bin/echo /usr/bin/cut...(timeout=0,uid=-1,gid=-1)
cfengine:/bin/echo /usr/: /usr/bin/cut
cfengine: Finished script /bin/echo /usr/bin/cut
cfengine: 
Executing script /bin/echo  option requires an argument -- d usage...(timeout=0,uid=-1,gid=-1)
cfengine:/bin/echo  opti: option requires an argument -- d usage
cfengine: Finished script /bin/echo  option requires an argument -- d usage
cfengine: 
Executing script /bin/echo  cut -b list [-n] [file ...]        cut -c list [file ...]        cut -f list [-s] [-d delim] [file ...]...(timeout=0,uid=-1,gid=-1)
cfengine:/bin/echo  cut : cut -b list [-n] [file ...] cut -c list [file ...] cut -f list [-s] [-d delim] [file ...]
cfengine: Finished script /bin/echo  cut -b list [-n] [file ...]        cut -c list [file ...]        cut -f list [-s] [-d delim] [file ...]

请注意,当我们echoshellcommands:. 到那时,该${time_drift}变量已经被评估,其结果显示我们错误地调用了cut's-d选项,抱怨我们没有传递任何-d显然不正确的东西。

这令人费解,因为${active_interface_mac}使用相同的语法并且可以完美运行。

我尝试将第二个 grep 替换为| tail -1 | sed 's///'、另一个grep -o [0-9]*.[0-9]或我能想到的任何其他内容,包括/usr/bin/cut -f1 -d'${spc}'. 我显然不能使用awk,因为 cfengine 解释$(NF)为括号,它们是 的一部分ExecShellResult,即使在转义时也是如此。

ntpdate我还有哪些其他选项可以从的输出中提取我的实际秒值?

4

1 回答 1

0

我不确定 cfengine 2,我看不出在您的示例中会出现什么问题,但对于 cfengine 3:

bundle agent main
{
  vars:

    "ntpdate_s"
      string => execresult( "/usr/sbin/ntpdate -d pool.ntp.org 2> /dev/null | /usr/bin/awk '/offset .* sec/ {print $10}'", useshell ),
      if => not( isvariable( offset ) );

  reports:
    "Mac address of wlan0 $(sys.hardware_mac[wlan0])";
    "Offset is $(ntpdate_s)";
}

输出:

R: Mac address of wlan0 5c:e0:c5:9f:f3:8f
R: Offset is 0.027672
于 2017-11-14T21:34:44.457 回答