0

我每小时都有一个由 root 运行的 cron 作业,检查是否存在绊线违规。它仍然每小时给我发一封电子邮件,不管我有没有违规行为。如果存在违规行为,则包括报告。如果没有违规,它会向我发送一封只有主题行的空白电子邮件。

这是脚本:

#!/bin/bash

# Save report
tripwire --check > /tmp/twreport

# Count violations
v=`grep -c 'Total violations found:  0' /tmp/twreport`

# Send report
if [ "$v" -eq 0 ]; then
        mail -s "[tripwire] Report for `uname -n`" user@example.com < /tmp/twreport
fi
4

2 回答 2

0

尝试用双引号括起来并使用完整路径

v="`/bin/grep -c 'Total violations found:  0' /tmp/twreport`"

if [ "$v" == "0" ]; then # or = instead of == based on your shell

如果这些不起作用,请验证搜索词。我在 'found: 0' 上看到 0 前有两个空格

于 2017-05-03T12:53:07.887 回答
0

我建议将代码更改为

if [ -f /tmp/twreport ] # Check file exists
then
 v=$(grep -c '^Total violations found:  0$' /tmp/twreport)
 #Not suggested using legacy backticks
 if [ "$v" -eq 0 ]; then
        mail -s "[tripwire] Report for $(uname -n)" user@example.com < /tmp/twreport
 fi
fi

最后在 cron 中设置路径,然后再放置脚本行。喜欢

# Setting PATH
PATH=/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/path/to/tripwire:/and/so/on
# Now,set up the cron-job for the script
0        11         *              *          0       /path/to/script
于 2017-05-03T03:11:13.453 回答