ubuntu 18版本以上挪到下边用heirloom-mailx来发邮件。
apt-get update
apt-get install mailx
apt-get install mail
sudo apt-get install heirloom-mailx
此时还不能发送外部服务器邮件,需要完成以下配置,修改/etc/nail.rc或者/etc/s-nail.rc(Ubuntu)、/etc/mail.rc(centOS)、vi /etc/nail.rc或者/etc/s-nail.rc
在nail.rc文件末尾添加一下两行代码:
set from=zhangqing@***.com
set smtp=smtp.exmail.qq.com
set smtp-auth-user=zhangqing@***.com
set smtp-auth-password="******"
set smtp-auth=login
按照以上步骤设置完毕之后即可使用mail命令发送邮件,发送邮件有以下三种方式:
- 方法一:交互形式发送邮件
- mail + 邮箱地址
- 填写主题
- 填写内容
- ctrl + d 结束输入
- cc代表抄送,回车完成发送
- 方法二:通道形式发送
- echo “邮件内容” | mail -s “主题” 邮箱地址
- 方法三:读取文件内容发送
- mail -s “主题” “邮箱地址” < “path/filename”
Test:ping某个ip不通时发送邮件
#!/bin/bash
yourip=210.*.*.*
pingsh(){
nowdate=`date +%Y%m%d-%H:%M:%S`
if ping -c 1 $yourip >/dev/null 2>&1 ; then
echo " ping $yourip from 192.168.3.87 at $nowdate is good" >> /var/log/pinglog.log
else
echo "ping $yourip from 192.168.3.87 at $nowdate is down!" >> /var/log/pinglog.log
echo "ping $yourip from 192.168.3.87 at $nowdate is down! AND Go to 3.87 start [bash /var/cro.sh]" | mail -s "Bole internet waring!!!" zhangqing@***.com
fi
}
step=2 #间隔的秒数,不能大于60
for (( i = 0; i < 10; i=(i+step) )); do
pingsh
echo $i
sleep $step
#判断如果不能ping通则退出循环,停止该脚本的执行。
if ping -c 1 $yourip >/dev/null 2>&1 ; then
echo " ping $yourip from 192.168.3.87 at $nowdate is good"
else
exit 0
fi
done
exit 0
step为间隔ping时间。i<10,为执行的总秒数。
heirloom-mailx配置:
配置软件源
ubuntu18.04上无法直接安装heirloom-mailx,需要添加软件源
sudo vi /etc/apt/sources.list
写入
deb http://cz.archive.ubuntu.com/ubuntu xenial main universe
安装heirloom-mailx
sudo apt-get update
sudo apt install heirloom-mailx
配置外部SMTP
Ubuntu18.04的配置文件为/etc/s-nail.rc,把下面几行放置在最后
set from="123456789@qq.com"
set smtp="smtps://smtp.qq.com:465"
set smtp-auth-user="123456789@qq.com"
set smtp-auth-password="ahkphxxabcbshabbga"
set smtp-auth=login
注意:第四行password是指的QQ邮箱授权码,不是密码
测试
echo "邮件内容" | s-nail -s "邮件主题" xxx456@126.com
或者
s-nail -s "邮件主题" 123xxx@126.com < result.txt
测试ping不通时发钉钉,恢复通之后发钉钉:
#!/bin/bash yourip=172.18.188.1 yourip2=192.168.0.1 yourip3=192.168.177.1 yourip4=$1 serverip=192.168.0.49 pingsh4(){ nowdate=`date +%Y%m%d-%H:%M:%S` nowdate2=`date +%Y%m%d-%H` if ping -c 1 $yourip4 >/dev/null 2>&1 ; then echo " ping $yourip4 from $serverip at $nowdate is good" >> /var/www/html/ping/log/pinglog-$nowdate2-$yourip4.log else echo "ping $yourip4 from $serverip at $nowdate is down!" >> /var/www/html/ping/log/pinglog-$nowdate2-$yourip4.log sleep 1 if ping -c 1 $yourip4 >/dev/null 2>&1 ; then nowdatedown=`date +%Y%m%d-%H:%M:%S` echo " ping $yourip4 from $serverip at $nowdatedown is good" >> /var/www/html/ping/log/pinglog-$nowdate2-$yourip4.log return else echo "ping $yourip4 from $serverip at $nowdatedown is down!" >> /var/www/html/ping/log/pinglog-$nowdate2-$yourip4.log python3.6 robot.py "ping $yourip4 from $serverip at $nowdatedown is down!(2th in 3s)" return fi fi } ping_count=3 pingok=y pingsh5() { #ping_result0=`ping $yourip4 -w 3 -c $ping_count -i 1` ping_result=`ping $yourip4 -w 3 -c $ping_count -i 1 |tail -2 |head -1` #取成功条数 succ_ping=`echo $ping_result | gawk '{print $4}'` #取失败百分比 loss_ping=`echo $ping_result | gawk '{print $6}'` if [ $succ_ping -eq $ping_count ];then echo "本次检测结果---->$yourip4服务器ping检测正常" #echo "`date "+%Y%m%d %H:%M:%S"`--->$ping_result0" >>/var/www/html/ping/log/pinglog-$nowdate2-$yourip4.log echo "`date "+%Y%m%d %H:%M:%S"`--->$yourip4 server ping is ok,$loss_ping packet loss." >>/var/www/html/ping/log/pinglog-$nowdate2-$yourip4.log if [[ $pingok = 'n' ]];then python3.6 robot.py "V~Server ($yourip4) ping is ok from $serverip~~~$loss_ping packet loss.`date "+%Y%m%d-%H:%M:%S"`" pingok=y fi else echo "本次检测结果---->$yourip4服务器有丢包现象,丢包率为:$loss_ping" #echo "`date "+%Y%m%d %H:%M:%S"`--->$ping_result0" >>/var/www/html/ping/log/pinglog-$nowdate2-$yourip4.log echo "`date "+%Y%m%d %H:%M:%S"`--->$yourip4 server ping is down,$loss_ping packet loss." >>/var/www/html/ping/log/pinglog-$nowdate2-$yourip4.log if [[ $pingok = 'y' ]] && [[$loss_ping = '100%']];then python3.6 robot.py "X~Server ($yourip4) ping is bad from $serverip!!!$loss_ping packet loss.`date "+%Y%m%d-%H:%M:%S"`" pingok=n fi fi } step=2 #间隔的秒数,不能大于60 for (( i = 0; i < 1000000000; i=(i+1) )) do pingsh5 echo $i sleep 3 datetimerm=59 datetimes=50 #判断如果不能ping通则sleep100s。 if [ $(date +%M) -eq "$datetimerm" ] then echo $(date +%M:%S) mv ./log/pinglog-* ./log/oldlog/ #rm ./log/pinglog-*.log sleep 65 fi done exit 0
解析域名写入到log,分析域名解析到的ip地址。
#!/bin/bash #yourip=39.102.117.166 step=2 #间隔的秒数,不能大于60 for (( i = 0; i < 100000000; i=(i+1) )) do str001=$(nslookup d105xpbtjj9cjp.cloudfront.net 172.18.188.3) echo "$str001" >> ./log/nslookuplog172181883.log str003=$(nslookup d105xpbtjj9cjp.cloudfront.net 10.161.0.3) echo "$str003" >> ./log/nslookuplog1016103.log str005=$(nslookup d5kwyqpbdauu5.cloudfront.net 172.18.188.3) echo "$str005" >> ./log/nslookuplog172181883-d5k.log str007=$(nslookup d5kwyqpbdauu5.cloudfront.net 10.161.0.3) echo "$str007" >> ./log/nslookuplog1016103-d5k.log str008=$(nslookup d2rargdj3vlbg6.cloudfront.net 172.18.188.3) echo "$str008" >> ./log/nslookuplog172181883-d2r.log str009=$(nslookup d2rargdj3vlbg6.cloudfront.net 10.161.0.3) echo "$str009" >> ./log/nslookuplog1016103-d2r.log echo $i sleep 10 #tiqu-ip cat ./log/nslookuplog172181883.log |grep -E 'Add|Name' > ./log/nslookuplog172181883ip.log cat ./log/nslookuplog1016103.log |grep -E 'Add|Name' > ./log/nslookuplog1016103ip.log cat ./log/nslookuplog172181883-d5k.log |grep -E 'Add|Name' > ./log/nslookuplog172181883-d5k-ip.log cat ./log/nslookuplog1016103-d5k.log |grep -E 'Add|Name' > ./log/nslookuplog1016103-d5k-ip.log cat ./log/nslookuplog172181883-d2r.log |grep -E 'Add|Name' > ./log/nslookuplog172181883-d2r-ip.log cat ./log/nslookuplog1016103-d2r.log |grep -E 'Add|Name' > ./log/nslookuplog1016103-d2r-ip.log #quchong-ip cat ./log/nslookuplog172181883ip.log |sort |uniq > ./log/nslookuplog172181883ip-uniq.log cat ./log/nslookuplog1016103ip.log |sort |uniq > ./log/nslookuplog1016103ip-uniq.log cat ./log/nslookuplog172181883-d5k-ip.log |sort |uniq > ./log/nslookuplog172181883-d5k-ip-uniq.log cat ./log/nslookuplog1016103-d5k-ip.log |sort |uniq > ./log/nslookuplog1016103-d5k-ip-uniq.log cat ./log/nslookuplog172181883-d2r-ip.log |sort |uniq > ./log/nslookuplog172181883-d2r-ip-uniq.log cat ./log/nslookuplog1016103-d2r-ip.log |sort |uniq > ./log/nslookuplog1016103-d2r-ip-uniq.log #end cat ./log/*-uniq.log |sort |uniq > ./log/end.log done exit 0
获取到Add行的内容并写到log文件。
cat nslookuplog1.log|grep -E 'Add' > nslookuplog1-ip.log
《Ubuntu下使用mail命令发送邮件,ping不通写入文件并截图》有1条评论