首页 > 代码库 > shell发送网页邮件
shell发送网页邮件
部分IP打码 请谅解~
邮件发送格式 :
echo "cat html"|mail -s "$(echo -e "check node number Content-Type:text/html;charset=utf-8")" -r "grouplist_node_check" mail@mai.com
由mail -s指定发送的内容为html类型。
内容模板 (建议在Dreamweaver、PyCharm等工具编辑好预览OK后使用)
==============================================
<html>
<body>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style> table{border-collapse: collapse;border-spacing: 0;background-color: transparent;} .table {width: 100%; max-width: 100%; margin-bottom: 20px; } .table > tbody > tr > td{padding: 8px; line-height: 1.42857143; vertical-align: top; border-top: 1px solid #dddddd; } .table-condensed > tbody > tr > td { padding: 5px; } .table-bordered { border: 1px solid #dddddd; } .table-bordered > tbody > tr > td { border: 1px solid #dddddd; } .table-bordered > thead > tr > td { border-bottom-width: 2px; } .table-striped > tbody > tr:nth-child(odd) { background-color: #f9f9f9; } table > tbody > tr.warning > td { background-color: #fcf8e3; } .panel { margin-bottom: 20px; background-color: #ffffff; border: 1px solid transparent; border-radius: 4px; -webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.05); box-shadow: 0 1px 1px rgba(0, 0, 0, 0.05); } .panel-body { padding: 15px; } .panel-heading { padding: 10px 15px; border-bottom: 1px solid transparent; border-top-right-radius: 3px; border-top-left-radius: 3px; } .panel-title { margin-top: 0; margin-bottom: 0; font-size: 16px; color: inherit; } .panel-warning > .panel-heading { color: #8a6d3b; background-color: #fcf8e3; border-color: #faebcc; } .panel-danger { border-color: #ebccd1; } .panel-danger > .panel-heading { color: #a94442; background-color: #f2dede; border-color: #ebccd1; } .fenzu{color: #5fc4f6} .num{color: #f65f5f} td{text-align: center;}
</style>
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title">分组信息</h3>
</div>
<div class="panel-body">
<table class="table table-bordered" style="margin: 0 60px; width: 520px; height: 41px;">
<tbody><tr class="warning" style="height: 30px">
<td style="width: 140px"><b style="color: #3c3c3c">分组名称</b></td>
<td style="width: 50px"><b style="color: #3c3c3c">节点个数</b></td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
=================================================
网页邮件顾名思义发送的就是网页格式的邮件,只要在发送时声明类型即可成功发送,shell、python、Foxmail都可发送,另外在测试中发现QQ邮箱可以在线预览编辑html邮件。
网页基本结构 (有些可以省略)
<html>
<head>
<title></title> 是网页的标题
<link/> 引用外部css/js
<meta /> 语言 一般是utf-8/gb2312
</head> 是网页头信息,一般来告诉浏览器引用的css/js样式是从哪里拿,还有语言、html版本等信息
<body>
</body> 是网页的主体结构用来书写网页的主要部分
<script></script> js函数或者引用一个什么东西进来 位置随意。一般是实现页面动态特效和辅助数据的元素
</html> 是网页的大结构,主要是告诉浏览器这个标签对是写的html的相关东西 用来区分
块级元素:容量比较大的标签,一般自带换行的标签都是块面级,且块面之间可以相互嵌套。 <div>、<table>、<p>、<br/>、<ul>
行内元素:只支持一行,不支持嵌套。<a>、<soqn>、<b>
样式:给标签赋予特定的颜色、大小、形状。是样式的作用。
样式有多种方法调用 行内调用是效力最强的 如给一个特定的<a>标签赋予颜色和字体大小的设置需要在行内写style:
<a href="http://www.mamicode.com/#" style="color: read font-size:16px" >HTML基础</a>
如果有比较批量的<a>标签需要改变样式则给标签一个标记利用 class、id、name实现
<a href="http://www.mamicode.com/#" class="baise">这是class样式演示</a> <a href="http://www.mamicode.com/#" id="fense">这是id样式演示</a> <a href="http://www.mamicode.com/#" name="wuse" >这是name演示的演示</a>
.baise{ color: #FFFFFF} #fense{color: #FF1493} 无法建立样式
若要全部<a> 标签生效
a{color: blue}
===================================================
注:网页邮件有很多样式限定,对于div的兼容性比较差,尽量使用table进行嵌套部署。
可以从bootstrap寻找样式提取取来加载进<style></style>中 然后在下文使用class或者id进行调用。
要使邮件报警跟脚本的输出结合起来我是用拼接实现的,将html table前面的部分保存成文件加载进脚本。
脚本输入的东西进行处理成网页table需要的格式跟加载的前半部分拼接而成,最后的结尾再做个拼接成品即可发送。
somkeping邮件美化
#!/bin/bash DATE=`date -d "-1 min" +%Y%m%d-%H%M` active="off" DATE_s=`date -d "-1 min" +%Y%m%d-%H%M` MAILLOG=/usr/local/smokeping/var/alert_logs/alertmail.$DATE ##日志中获取 MAILLIST="@@@@@@@.#@@com" num=$((`cat $MAILLOG |egrep -v "#|$(date +%Y)*|someloss|lossdetect|^$"|wc -l`/4)) >/home/3jia5om/scripts/html_tmp htmla=`echo -e ‘<html><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><body><style>table{border-collapse:collapse;border-spacing:0;background-color:transparent}.table{width:90%;max-width:95%;margin-bottom:20px}.table>tbody>tr>td{padding:6px 4px;line-height:1.42857143;vertical-align:top;border-top:1px solid #dddddd;text-align:center;font-size:11px;width:40px}.table-condensed>tbody>tr>td{padding:5px}.table-bordered{border:1px solid #dddddd}.table-bordered>tbody>tr>td{border:1px solid #dddddd}.table-bordered>thead>tr>td{border-bottom-width:2px}.table-striped>tbody>tr:nth-child(odd){background-color:#f9f9f9}.table>tbody>tr.active>td{background-color:#f5f5f5}.table>tbody>tr.warning>td{background-color:#fcf8e3}.table>tbody>tr.info>td{background-color:#d9edf7}.table>tbody>tr.danger>td{background-color:#f2dede}.table>tbody>tr.success>td{background-color:#dff0d8}.panel{margin-bottom:20px;background-color:#ffffff;border:1px solid transparent;border-radius:4px;-webkit-box-shadow:0 1px 1px rgba(0,0,0,0.05);box-shadow:0 1px 1px rgba(0,0,0,0.05);width:760px}.panel-heading{border-bottom:1px solid transparent;border-top-right-radius:3px;border-top-left-radius:3px}.panel-title{margin-top:0;margin-bottom:0;font-size:16px;color:inherit}.panel-warning{border-color:#faebcc}.panel-warning>.panel-heading{color:#8a6d3b;background-color:#fcf8e3;border-color:#faebcc}.panel-info{border-color:#bce8f1;}.panel-info>.panel-heading{color:#31708f;background-color:#d9edf7;border-color:#bce8f1}.panel-success{border-color:#d6e9c6}.panel-success>.panel-heading{color:#3c763d;background-color:#dff0d8;border-color:#d6e9c6}.panel-danger{border-color:#ebccd1}.panel-danger>.panel-heading{color:#a94442;background-color:#f2dede;border-color:#ebccd1}span{margin-right:20px}ul li{list-style:none;float:left;line-height:34px}.me{margin-left:13%}.gaodu{height:30px}.zuo{text-align:left;margin-left:10px}</style>‘` if [ -f $MAILLOG ];then s_add=(`cat $MAILLOG |grep CDN|awk -F "[][]" ‘{print $1,$2}‘|awk ‘{print $3}‘`) ##切出来自IP d_add=(`cat $MAILLOG |grep CDN|awk -F "[][]" ‘{print $1,$2}‘|awk ‘{print $1}‘`) ##切出来自名称 else exit 0 fi if [ -f $MAILLOG ];then countnum=0 ##循环主体 for i in `seq 1 $num` do on=‘on‘ content=`cat $MAILLOG |egrep -v "#|$(date +%Y)*|someloss|lossdetect|^$"|sed -n $[($i-1)*4+1],$[$i*4+1]p` "***.***.**.***|***.***.**.***|***.***.**.***|***.***.**.***|***.***.**.***|***.***.**.***|***.***.**.***"` head=`echo $content|sed -n 1p` lost=`cat $MAILLOG |egrep -v "#|$(date +%Y)*|someloss|lossdetect|^$"|sed -n $[($i-1)*4+1],$[$i*4+1]p|sed -n 2p|awk -F ":" ‘{print $2}‘|sed ‘s/[[:space:]]//g‘` rtt=`cat $MAILLOG |egrep -v "#|$(date +%Y)*|someloss|lossdetect|^$"|sed -n $[($i-1)*4+1],$[$i*4+1]p|sed -n 3p|awk -F ":" ‘{print $2}‘|sed ‘s/[[:space:]]//g‘` to=` cat $MAILLOG |egrep -v "#|$(date +%Y)*|someloss|lossdetect|^$"|sed -n $[($i-1)*4+1],$[$i*4+1]p|sed -n 4p` add=`echo $head|awk -F "[][]" ‘{print $1,$2}‘|awk ‘{print $3}‘` #echo $add|wc -L if [ `echo $add|wc -L` -lt 7 ];then add="***.***.**.***" fi hostname=`echo $head|awk ‘{print $1}‘|awk -F "." ‘{print $NF}‘` ushote=`echo $head|awk -F "[][]" ‘{print $1,$2}‘|awk -F "." ‘{print $1}‘` count=0 counts=0 for line in `cat /home/3jia5om/scripts/black.somkeping` do count=$[$count+1] if [ $[$count%2] -eq 1 ];then blackname=$line #echo $blackname else blackip=$line fi if [ $count -ge 2 ];then if [[ $hostname == $blackname ]];then blackip=${blackip//,/ } for ip in $blackip do #echo $ip if [[ $add == $ip ]];then active="on" counts=$[$counts+1] #echo "状态激活" echo "`date +%Y‘-‘%m‘-‘%d‘ ‘%H‘:‘%M‘:‘%S` 报警节点:$blackname 来自IP:$ip 被手动屏蔽。" >>/home/3jia5om/scripts/alert.log fi done fi count=0 fi done #echo $active if [[ $active == $on ]];then #echo "1111" active="off" continue fi echo ‘<div class="panel panel-info" style="width: 820px"><div class="panel-heading" style="height: 35px">‘>>/home/3jia5om/scripts/html_tmp echo "<ul class="panel-title"><li>报警节点:</li><li><span>$hostname</span></li><li class="me">所属线路:</li><li><span>$ushote</span></li><li class="me">来自IP:</li><li><span>$add</span></li></ul></div>">>/home/3jia5om/scripts/html_tmp echo ‘<div class="panel-body"><table class="table table-bordered"style="margin:10px 1%"><tbody><tr class="info gaodu">‘>>/home/3jia5om/scripts/html_tmp echo -e "<td>Loss:</td><td>$(echo $lost|awk -F "," ‘{print $1}‘)</td><td>$(echo $lost|awk -F "," ‘{print $2}‘)</td><td>$(echo $lost|awk -F "," ‘{print $4}‘)</td><td>$(echo $lost|awk -F "," ‘{print $5}‘)</td><td>$(echo $lost|awk -F "," ‘{print $6}‘)</td><td>$(echo $lost|awk -F "," ‘{print $7}‘)</td><td>$(echo $lost|awk -F "," ‘{print $8}‘)</td><td>$(echo $lost|awk -F "," ‘{print $9}‘)</td><td>$(echo $lost|awk -F "," ‘{print $10}‘)</td><td>$(echo $lost|awk -F "," ‘{print $11}‘)</td><td>$(echo $lost|awk -F "," ‘{print $12}‘)</td><td>$(echo $lost|awk -F "," ‘{print $13}‘)</td><td>$(echo $lost|awk -F "," ‘{print $14}‘)</td><td>$(echo $lost|awk -F "," ‘{print $15}‘)</td><td>$(echo $lost|awk -F "," ‘{print $16}‘)</td><td>$(echo $lost|awk -F "," ‘{print $17}‘)</td><td>$(echo $lost|awk -F "," ‘{print $18}‘)</td><td>$(echo $lost|awk -F "," ‘{print $19}‘)</td></tr> <tr class="gaodu"><td>Rtt:</td><td>$(echo $rtt|awk -F "," ‘{print $1}‘)</td><td>$(echo $rtt|awk -F "," ‘{print $2}‘)</td><td>$(echo $rtt|awk -F "," ‘{print $4}‘)</td><td>$(echo $rtt|awk -F "," ‘{print $5}‘)</td><td>$(echo $rtt|awk -F "," ‘{print $6}‘)</td><td>$(echo $rtt|awk -F "," ‘{print $7}‘)</td><td>$(echo $rtt|awk -F "," ‘{print $8}‘)</td><td>$(echo $rtt|awk -F "," ‘{print $9}‘)</td><td>$(echo $rtt|awk -F "," ‘{print $10}‘)</td><td>$(echo $rtt|awk -F "," ‘{print $11}‘)</td><td>$(echo $rtt|awk -F "," ‘{print $12}‘)</td><td>$(echo $rtt|awk -F "," ‘{print $13}‘)</td><td>$(echo $rtt|awk -F "," ‘{print $14}‘)</td><td>$(echo $rtt|awk -F "," ‘{print $15}‘)</td><td>$(echo $rtt|awk -F "," ‘{print $16}‘)</td><td>$(echo $rtt|awk -F "," ‘{print $17}‘)</td><td>$(echo $rtt|awk -F "," ‘{print $18}‘)</td><td>$(echo $rtt|awk -F "," ‘{print $19}‘)</td></tr><tr style="height: 30px"><td colspan="2">目标IP:</td><td class="zuo" colspan="18">$to</td></tr></tbody></table></div></div>">>/home/3jia5om/scripts/html_tmp countnum=$(($countnum+1)) done htmlb=`cat /home/3jia5om/scripts/html_tmp` htmlc="</body></html>" htmla=$htmla$htmlb$htmlc if [ $countnum -gt 0 ];then #echo $htmla echo $htmla| mail -s "$(echo -e "[SmokeAlert] is active on $DATE $MAILLIST \nContent-Type:text/html;charset=utf-8")" -r "somkeping" $MAILLIST dsg="`echo $2 $4`" #/usr/bin/curl -d "group=IDCswitch&subject=\"IDC network quality smokeping alert\"&msg=$Msg" http://*.*.*.*/msgserver/sendinfo.jsp >/dev/null 2>&1 #这里是短信接口 fi else echo ok fi |
不报警 分组&节点
20161129-0200 someloss CDN_Chinanet.CDN_BJ.bj163 [from ***.***.***.***] loss: 57%, 27%, 37%, 36%, 37%, 27%, 36%, 34%, 32%, 38%, 29%, 29%, 37%, 27%, 36%, 22%, 20%, 23%, 17% rtt: 139ms, 135ms, 135ms, 135ms, 135ms, 135ms, 135ms, 135ms, 135ms, 135ms, 135ms, 134ms, 135ms, 134ms, 135ms, 134ms, 134ms, 134ms, 117ms ***.***.***.*** CDN_Chinanet.CDN_BJ.bj163 [from ***.***.***.***] loss: 11%, 7%, 7%, 7%, 13%, 5%, 9%, 6%, 7%, 3%, 6%, 6%, 7%, 7%, 7%, 4%, 5%, 2%, 3% rtt: 139ms, 139ms, 139ms, 139ms, 139ms, 139ms, 139ms, 139ms, 139ms, 139ms, 139ms, 139ms, 139ms, 139ms, 139ms, 139ms, 139ms, 139ms, 139ms ***.***.***.*** |
源文件
分组检查邮件报警
#!/bin/bash base_dir=/home/3jia5om/scripts/check_group_number work_dir=/root/powercdn_dns_conf group_dir=$work_dir/group WHITE_GROUP=$base_dir/white_group #特殊分组(白名单) SPECIAL_GROUP=$base_dir/no_use_group #无用分组 error_group=$base_dir/error_list html1=$base_dir/html1 html4="</tbody></table></div></div></body></html>" >$base_dir/html2 >$error_group for i in $group_dir/*.group do GROUP=`echo ${i##*/} | awk -F\. ‘{print $1}‘` count=`egrep -o ‘[0-9]{1,3}(\.[0-9]{1,3}){3}‘ $i |wc -l` if [ $count -lt 5 ] then grep $GROUP $WHITE_GROUP |grep -v special_bj |grep -v special_bj_udp|grep -v download|grep -v meibeian |grep -v g12851373 >> /dev/null if [ $? -eq 0 ] then echo "$GROUP special node is $count " >> $error_group fi elif [ $count -lt 10 ] then grep $GROUP $WHITE_GROUP >> /dev/null || grep $GROUP $SPECIAL_GROUP >> /dev/null if [ $? -ne 0 ] then echo "$GROUP normal node is $count " >> $error_group fi fi done if [ -s $error_group ];then D=`date +"%F %T"` info="$D `sort -t ‘ ‘ -k 3 -k 5 $error_group | column -t`" sendsms() { for call in 18610297949 18613879049 do curl --data "p=$call&c=$info" "http://smss.powercdn.com/sendmsg/sendmsg.html" done cat $base_dir/error_list |while read i ##循环有问题的分组 do echo -e "<tr class="warning" style="height: 30px"><td style="width: 140px"><span class="fenzu">`echo $i|awk ‘{print $1}‘`</span></td> \n <td style="width: 50px"><span class="num">`echo $i|awk ‘{print $5}‘`</span></td></tr>" >>$base_dir/html2 ##输出网页表格结构 done html2=$base_dir/html2 html3=`cat $html1 $html2` echo "$html3 $html4"|mail -s "$(echo -e "check node number\nContent-Type:text/html;charset=utf-8")" -r "grouplist_node_check" yunwei@3jia5.com info@powercdn.com #echo "${info}"|mail -s "check node number" -r "check node" @@@.@@.com } sendsms else exit fi |
本文出自 “12193705” 博客,谢绝转载!
shell发送网页邮件