Ubuntu下使用mail命令发送邮件,ping不通写入文件并截图

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=39.*.*.*

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.128.201 at $nowdate is good" >> /var/log/pinglog.log
else

echo "ping $yourip from 192.168.128.201 at $nowdate is down!" >> /var/log/pinglog.log

echo "ping $yourip from 192.168.128.201 at $nowdate is down! AND Go to 3.87 start [bash /var/cro.sh]" | s-nail -s "Bole internet waring!!!" zhangqing@bolegames.com

gnome-screenshot -f ~/Desktop/$nowdate.png
fi
}

step=2 #间隔的秒数,不能大于60
for (( i = 0; i < 1000000000; i=(i+1) ))
do
        pingsh
        echo $i
        sleep 2
        #判断如果不能ping通则退出循环,停止该脚本的执行。
        if  ping -c 1 $yourip  >/dev/null 2>&1 ; then
                echo " ping $yourip from 192.168.128.201 at $nowdate is good"
        #else
        #       exit 0
        fi

done
exit 0

 

解析域名写入到log,分析域名解析到的ip地址。

#!/bin/bash
#yourip=39.102.117.166


step=2 #间隔的秒数,不能大于60
for (( i = 0; i < 1000000000; i=(i+1) ))
do

        str001=$(nslookup g.alicdn.com 172.18.188.1)
      	echo "$str001"  >> /var/log/nslookuplog1.log
        
	str003=$(nslookup g.alicdn.com 172.18.188.3)
	echo "$str003"  >> /var/log/nslookuplog3.log


	echo $i
        sleep 30 

done
exit 0

获取到Add行的内容并写到log文件。

cat nslookuplog1.log|grep -E 'Add' > nslookuplog1-ip.log

 

《Ubuntu下使用mail命令发送邮件,ping不通写入文件并截图》有1条评论

发表评论

2,158 次浏览