Linux监控端口发送邮件
1、首先安装 mailx 工具
yum install mailx
2、编辑配置文件
vim /etc/mail.rc #添加如下内容
创建SSL证书
mkdir -p /root/.certs/ echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt certutil -L -d /root/.certs
创建完成后,进入.certs里,进行信任证书
certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qq.crt
返回如下提示:
**Notice: Trust flag u is set automatically if the private key is present.**
#对方收到邮件时显示的发件人
set from=xxx@qq.com
#指定第三方发邮件的smtp服务器地址,如:smtp.qq.com
set smtp=smtps://smtp.qq.com:465
#第三方发邮件的用户名
set smtp-auth-user=xxx@qq.com、
#用户名对应的密码,QQ邮箱可以使用授权码
set smtp-auth-password=xxx
#SMTP的认证方式,默认是login
set smtp-auth=login
set ssl-verify=ignore
#制定的存放QQ邮箱SSL证书的位置
set nss-config-dir=/root/.certs
3、配置完成,测试发送邮件
echo "测试邮件" | mail -s "测试" xxx@qq.com
4、编写Shell脚本
#!/bin/bash
cur_dir=/data/check_shell
ipfile=$cur_dir/ip.txt
logfile=$cur_dir/log.txt
logfile_err=$cur_dir/log_err.txt
date=`date`
echo "****${date}****" >> $logfile
if [ ! -f "$ipfile" ]; then
echo "系统检查到在当前路径下不存在要测试的地址清单,请补充ip.txt要测试的地址清单 EXIT"
exit
fi
if [ ! -f "$logfile" ]; then
touch $cur_dir/log.txt
fi
cat $ipfile | while read line
do
#result=echo -e "\n" | telnet $line 2 > /dev/null | grep Connected | wc -l
result=`timeout 30 telnet $line < /dev/null 2>/dev/null | grep '\^' | wc -l`
if [[ $result -eq 1 ]]; then
echo " $line network is nomral" >> $logfile
else
echo "****${date}****" >> $logfile_err
echo " $line 端口检测失败,请检查" >> $logfile_err
mail -s "$line 端口不在线" -c xxx@163.com xxx@126.com < $logfile_err
fi
done
5、执行定时
$ crontab -e 00 10 * * * /bin/sh /home/app/backup/disk.sh

浙公网安备 33010602011771号