有時候,監控一個系統須要在系統出現警告時經過shell發送郵件來通知相關的負責人。本文講講如何shell發送郵件。
一種是普通郵件,經過附件描述詳細報告;一種是高級郵件(哈哈),原本就是告警,還不趕快看,還要慢慢下載附件打開看?因此將附件內容以HTML直接展現到正文處,並經過樣式標記警告。html
1.安裝sendEmail工具
下載、解壓、配置環境變量shell
]# wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz ]# tar -zxf sendEmail-v1.56.tar.gz -C SendEmail/ #配置環境變量這裏不同,不在profile文件裏配置,而是拷貝到系統環境變量已有的路徑中 ]# cd ~/myApp/SendEmail/ ]# cp sendEmail /usr/local/bin/
2.開啓郵箱的SMTP
本文使用QQ郵件做爲發送者
設置 -> 帳戶 -> 下圖中開啓bash
3.發送郵件
QQ郵箱對SMTP有以下說明:
app
#!/bin/bash RECIVER="Wish0123@163.com" SENDER="964478654@qq.com" SENDER_USERNAME=964478654 #密碼填寫SMTP第三方客戶端受權碼 SENDER_PASSWD=*********** ATTACHMENT_PATH=/root/workspace/sh/attach_test.txt #以下圖描述,做爲郵件發送方應填這個hostname STMP_HOST=smtp.qq.com MESSAGEL_SUBJECT="Have Fun" MESSAGE_BODY="報警啊..." sendEmail -f $SENDER -t $RECIVER -a $ATTACHMENT_PATH \ -s $STMP_HOST -xu $SENDER_USERNAME -xp $SENDER_PASSWD \ -u $MESSAGEL_SUBJECT -m $MESSAGE_BODY \ -o message-charset=utf-8
發送後報錯了:工具
******************************************************************* Using the default of SSL_verify_mode of SSL_VERIFY_NONE for client is deprecated! Please set SSL_verify_mode to SSL_VERIFY_PEER possibly with SSL_ca_file|SSL_ca_path for verification. If you really don't want to verify the certificate and keep the connection open to Man-In-The-Middle attacks please set SSL_verify_mode explicitly to SSL_VERIFY_NONE in your application. ******************************************************************* at /usr/local/bin/sendEmail line 1906. invalid SSL_version specified at /usr/share/perl5/vendor_perl/IO/Socket/SSL.pm line 444.
意思是SSL版本不對,緣由是在cent7中Perl的版本5.16,而應該使用更低版本。或者不用SSL,也就須要在腳本最後一行的sendEmail
命令添加一個參數-o tls=no
spa
sendEmail -f $SENDER -t $RECIVER -a $ATTACHMENT_PATH \ -s $STMP_HOST -xu $SENDER_USERNAME -xp $SENDER_PASSWD \ -u $MESSAGEL_SUBJECT -m $MESSAGE_BODY \ -o message-charset=utf-8 -o tls=no
發送成功了!
我擦,竟然被當成詐騙釣魚郵件。。.net
4.HTML版
郵箱直接展現html須要兩個條件:
一是把要展現的數據使用html標籤封裝起來,就能夠隨意增長樣式了;
二是在sendEmail
增長參數-o message-content-type=html
這裏是根據系統內存狀況發送系統郵件,當集羣中機器的使用內存超過總內存80%的機器信息標紅。code
[root@NN1 sh]# cat memory_info ip consum free total 192.168.1.10 100 100 200 192.168.1.11 1 239 240 192.168.1.12 350 50 400 192.168.1.13 40 24 64 192.168.1.14 77 33 110 192.168.1.15 150 250 400
編寫腳本htm
#!/bin/bash -x RECIVER="Wish0123@163.com" SENDER="964478654@qq.com" SENDER_USERNAME=964478654 #密碼填寫SMTP第三方客戶端受權碼 SENDER_PASSWD=xuxjxankheuubcjd STMP_HOST=smtp.qq.com MESSAGEL_SUBJECT="Have Fun" HTML_PATH=html_path echo "<table>">$HTML_PATH echo "<thead><th>ip</th><th>consum</th><th>free</th><th>total</th></thead>">>$HTML_PATH echo "<tbody>">>$HTML_PATH create_tr() { i=2 lines=$(cat memory_info | wc -l) while [ $i -le $lines ] do j=1 ip=$(awk 'NR==i {print $j}' i=$i j=$j memory_info) let "j++" m=$(awk 'NR==i {print $j}' i=$i j=$j memory_info) let "j++" f=$(awk 'NR==i {print $j}' i=$i j=$j memory_info) let "j++" t=$(awk 'NR==i {print $j}' i=$i j=$j memory_info) tr="<tr>" #shell不能直接做小數運算 #更簡單的運算寫法是$[$m + 5] if [ $(expr $m \* 5) -gt $(expr $t \* 4) ]; then tr="<tr style=\"background-color:red\">" fi echo $tr>>$HTML_PATH echo "<td>"$ip"</td>">>$HTML_PATH echo "<td>"$m"</td>">>$HTML_PATH echo "<td>"$f"</td>">>$HTML_PATH echo "<td>"$t"</td>">>$HTML_PATH echo "</tr>">>$HTML_PATH let "i++" done } create_tr echo "</tbody>">>$HTML_PATH echo "</table>">>$HTML_PATH MESSAGE_BODY=$(cat $HTML_PATH) sendEmail -f $SENDER -t $RECIVER -a $ATTACHMENT_PATH \ -s $STMP_HOST -xu $SENDER_USERNAME -xp $SENDER_PASSWD \ -u $MESSAGEL_SUBJECT -m $MESSAGE_BODY \ -o tls=no message-charset=utf-8 message-content-type=html
發送成功!
blog
好玩吧~
參考文章:
https://blog.csdn.net/leshami...
https://my.oschina.net/u/4005...
https://blog.csdn.net/wz94732...