Bash 使用ms阈值ping脚本,然后保存到文件并发送电子邮件

Bash 使用ms阈值ping脚本,然后保存到文件并发送电子邮件,bash,awk,ping,Bash,Awk,Ping,在google和stackexchange的帮助下,我用bash(我的第一个)编写了一个脚本。 脚本所做的是获取一个阈值,并将其与ping的ms-time结果进行比较。如果毫秒时间值高于阈值,它会将ping结果回显到.log文件(pingscript.log)中 我还希望脚本在ping ms时间高于阈值时发送邮件 我曾试图通过管道将其发送到邮件命令,但没有成功=( 还有人知道怎么解决吗 #!/bin/bash if [ "$#" -ne 1 ]; then echo "You must ent

在google和stackexchange的帮助下,我用bash(我的第一个)编写了一个脚本。 脚本所做的是获取一个阈值,并将其与ping的ms-time结果进行比较。如果毫秒时间值高于阈值,它会将ping结果回显到.log文件(pingscript.log)中

我还希望脚本在ping ms时间高于阈值时发送邮件

我曾试图通过管道将其发送到邮件命令,但没有成功=(

还有人知道怎么解决吗

#!/bin/bash

if [ "$#" -ne 1 ]; then
echo "You must enter 1 command line arguments - The address which you want to ping against"
exit
fi

while true; do
ping -c 1 $1 | awk -v time="$(date +"%Y-%m-%d %H:%M:%S")" -Ftime= 'NF>1{if ($2+0 > 5) print $1 $2 $4 $3 $5 " "time >> "pingscript.log" }'
sleep 2
done
我让它工作了

这就是我最终的结局

#!/bin/bash

if [ "$#" -ne 1 ]; then
    echo "You must enter 1 command line arguments - The address which you want to ping against"
exit
fi

hostname=$(hostname)

while true; do

RESULT=$(ping -c 1 $1 | awk -v time="$(date +"%Y-%m-%d %H:%M:%S")" -Ftime= 'NF>1{if ($2+0 > 1) print $1 $2 $4 $3 $5 " "time }')

if [ "$RESULT" != "" ]; then
echo $RESULT >> pingscript.log
echo $RESULT | mail -s "pingAlert between $hostname -  $1" foo@bar.com
fi

sleep 2
done

这将给foo@bar.com如果ms-time结果高于脚本中的阈值,则发送一封带有ping结果的电子邮件。

从谷歌搜索mailx()和mutt()的使用示例开始。祝你好运!