bash Grepping 并且仅在找到某些内容时才发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14656380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:25:46  来源:igfitidea点击:

Grepping and only sending e-mail if something found

linuxbashcrongrep

提问by Amanada Smith

#!/bin/bash
( /usr/src/chkrootkit-$VERSION/chkrootkit ) | # Binary
grep 'INFECTED|Vulnerable' | # Only get found issues
/bin/mail -s 'CHROOTKIT Weekly Run ($SERVERNAME)' $EMAIL # Send EMail

This still sends e-mails even if nothing is found.

即使什么也没找到,这仍然会发送电子邮件。

What would be a way to only send if something is grepped?

只有在某些东西被 grepped 时才发送的方法是什么?

回答by F. Hauri

This maybe...

这也许...

Simply use -Eswitch in mail command:

只需-E在邮件命令中使用switch:

man -Pcol\ -b mail | grep empty
     -E      Don't send messages with an empty body.


#!/bin/bash
( /usr/src/chkrootkit-$VERSION/chkrootkit ) | # Binary
grep 'INFECTED|Vulnerable' | # Only get found issues
/bin/mail -E -s 'CHROOTKIT Weekly Run ($SERVERNAME)' $EMAIL # Send EMail

or place your check in a crontabfor automatic processing, for ex once a day:

或将您的支票crontab存入自动处理,例如每天一次:

@daily  ( /usr/src/chkrootkit-$VERSION/chkrootkit ) | grep 'INFECTED|Vulnerable'

Cron will send a mail if command output something.

如果命令输出某些内容,Cron 将发送邮件。

But, after re-reading this

但是,在重读这篇文章后

If there is no need to forwardany part of the mail in the alert, there is no need to use the pipe|.

如果不需要转发警报中邮件的任何部分,则无需使用管道|

So you could use conditionin this way:

所以你可以这样使用条件

#!/bin/bash
( /usr/src/chkrootkit-$VERSION/chkrootkit ) | # Binary
    grep -q 'INFECTED|Vulnerable' &&
    /bin/mail -s 'CHROOTKIT Weekly Run ($SERVERNAME)' $EMAIL

The -qswitch to grepensure to stay quiet.

-q开关grep确保保持安静。

回答by Jonathan Le

For GNU Mailutils, you can do something like this with -E'set nonullbody':

对于 GNU Mailutils,您可以使用-E'set nonullbody' 执行以下操作

grep whatever wherever | mailx -E'set nonullbody' -s EMAIL_SUBJECT [email protected]

See http://mailutils.org/manual/html_section/mail.html: nullbody

请参阅http://mailutils.org/manual/html_section/mail.html:nullbody

Type: Boolean
Default: True

Controls whether mail accepts messages with an empty body. The default value, true, means such messages are sent, and a warning (traditionally saying ‘Null message body; hope that's ok') is displayed. The text of the warning can be set using nullbodymsg variable (see below).

If nullbody is unset, mail will silently ignore such messages. This can be useful in ‘crontab' files, to avoid sending mails when nothing important happens. For example, the ‘crontab' entry below will send mail only if the utility some-prog outputs something on its standard output or error:

类型:布尔
默认值:True

控制邮件是否接受正文为空的邮件。默认值 true 表示发送此类消息,并显示警告(传统上说“空消息正文;希望没问题”)。警告的文本​​可以使用 nullbodymsg 变量设置(见下文)。

如果未设置 nullbody,邮件将默默地忽略此类消息。这在 'crontab' 文件中很有用,以避免在没有重要事情发生时发送邮件。例如,仅当实用程序 some-prog 在其标准输出或错误上输出某些内容时,下面的“crontab”条目才会发送邮件:

Just put the above in cron for some schedule:

只需将上面的内容放在 cron 中以获得一些时间表:

*/5 * * * * some-prog 2>&1 | /bin/mail -E'set nonullbody' -s 'Periodic synchronization'

回答by John Brodie

#!/bin/bash
( /usr/src/chkrootkit-$VERSION/chkrootkit ) | # Binary
grep 'INFECTED|Vulnerable' # Only get found issues
if [ $? -eq 0 ]
    /bin/mail -s 'CHROOTKIT Weekly Run ($SERVERNAME)' $EMAIL # Send EMail
fi

grep returns a non-zero exit code if it finds a match, 0 if it does not. We just need to check the last return value (which is grep's return value), and conditionally send the mail based on that.

如果找到匹配项,grep 将返回非零退出代码,否则返回 0。我们只需要检查最后一个返回值(即grep的返回值),并根据它有条件地发送邮件。