shell 脚本获取远程服务器信息并发送邮件
1 [root@zenghui expect]# cat ssh_dsa.exp 2 #!/usr/bin/expect 3 set ip [lindex $argv 0] 4 set pass [lindex $argv 1] 5 set port [lindex $argv 2] 6 set user [lindex $argv 3] 7 set id_dsa [lindex $argv 4] 8 9 if { $id_dsa == "1" } {set id "id_dsa_wdzj";set i "-i" 10 spawn ssh $i $id -p $port $user@$ip "df -hP;free -m;uptime" 11 expect { 12 "*(yes/no)?" {send "yes\r"; exp_continue} 13 "*password:" { send "$pass\r"} 14 "*id_dsa_wdzj':" { send "$pass\r"} 15 } 16 } 17 if { $id_dsa == "0" } { 18 spawn ssh -p $port $user@$ip "df -hP;free -m;uptime" 19 expect { 20 "*(yes/no)?" {send "yes\r"; exp_continue} 21 "*password:" { send "$pass\r"} 22 } 23 } 24 25 expect eof
1 [root@zenghui expect]# cat email_ssh.sh 2 #!/bin/bash 3 4 DIR='/root/shell/expect' 5 6 function Ssh(){ 7 clear 8 >$DIR/dev.log 9 >$DIR/dev2.log 10 11 echo '<!--StartFragment--> <table width="880" height="97" style="border-collapse:collapse;width:660.00pt;"> <colgroup><col width="157"> <col width="179"> <col width="186"> <col width="159"> <col width="199"> </colgroup><tbody><tr height="36"> <td class="et2" colspan="5" x:str="" height="36" width="838" style="font-size: 19px; text-align: center; vertical-align: middle; background-color: rgb(0, 0, 0); height: 27pt; width: 628.5pt; font-family: 宋体;"><font color="#ff0000"><b>cpu、内存、磁盘使用情况表1.0</b></font></td> </tr> <tr height="29"> <td class="et4" x:str="" height="29" width="157" style="font-size: 12pt; text-align: center; vertical-align: middle; border: 0.5pt dashed rgb(0, 0, 0); background-color: rgb(0, 128, 0); height: 21.75pt; width: 117.75pt; font-family: 宋体;"><font color="#ffffff">ip</font></td><td class="et4" x:str="" height="29" width="179" style="font-size: 12pt; text-align: center; vertical-align: middle; border: 0.5pt dashed rgb(0, 0, 0); background-color: rgb(0, 128, 0); height: 21.75pt; width: 134.25pt; font-family: 宋体;"><font color="#ffffff">cpu负载大于1.0</f ont></td><td class="et4" x:str="" height="29" width="186" style="font-size: 12pt; text-align: center; vertical-align: middle; border: 0.5pt dashed rgb(0, 0, 0); background-color: rgb(0, 128, 0); height: 21.75pt; width: 139.5pt; font-family: 宋体;"><font color="#ffffff">磁盘大于80%</font></td><td class="et4" x:str="" height="29" width="159" style="font-size: 12pt; text-align: center; vertical-align: middle; border: 0.5pt dashed rgb(0, 0, 0); background-color: rgb(0, 128, 0); height: 21.75pt; width: 119.25pt; font-family: 宋体;"><font color="#ffffff">内存大于90%</font></td><td class="et4" x:str="" height="29" width="199" style="font-size: 12pt; text-align: center; vertical-align: middle; border: 0.5pt dashed rgb(0, 0, 0); background-color: rgb(0, 128, 0); height: 21.75pt; width: 149.25pt; font-family: 宋体;"><font color="#ffffff">时间</font></td> </tr> ' >> $DIR/dev2.log 12 13 for i in `cat $DIR/config.txt` 14 do 15 ip=`echo $i| cut -d"," -f1` 16 passwd=`echo $i| cut -d"," -f2` 17 port=`echo $i| cut -d"," -f3` 18 user=`echo $i| cut -d"," -f4` 19 id=`echo $i| cut -d"," -f5` 20 $DIR/ssh_dsa.exp $ip $passwd $port $user $id > $DIR/ssh.log 2>&1 21 22 echo '<tr height="32">' >> $DIR/dev.log 23 24 25 echo "<td class='et6' x:str='' height='32' width='157' style='font-size: 12pt; text-align: center; vertical-align: middle; border: 0.5pt dashed rgb(0, 0, 0); background-color: rgb(192, 192, 192); height: 24pt; width: 117.75pt; font-family: 宋>体;'>$ip</td>" >> $DIR/dev.log 26 27 #---average--- 28 average=`tail -1 $DIR/ssh.log | awk -v avg=$avg -F"[,:]" '{if ($(NF-2) > avg) print $0}'| awk -F'load average:' '{print $2}'` 29 echo "<td class='et6' x:str='' height='32' width='157' style='font-size: 12pt; text-align: center; vertical-align: middle; border: 0.5pt dashed rgb(0, 0, 0); background-color: rgb(192, 192, 192); height: 24pt; width: 117.75pt; font-family: 宋>体;'>$average</td>" >> $DIR/dev.log 30 #---endaverage--- 31 32 33 34 #---disk--- 35 Df_nr=`awk '{if ($1=="total") print NR}' $DIR/ssh.log` 36 Free_nr=`awk '{if ($1=="Swap:") print NR+1}' $DIR/ssh.log` 37 Disk=`awk -v Df_nr=$Df_nr '{if (NR<Df_nr) print $0}' $DIR/ssh.log | awk -v disk=$disk '{if ($5 > disk && NR >3) print $1,$5,$NF}'` 38 echo "<td class='et6' x:str='' height='32' width='157' style='font-size: 12pt; text-align: center; vertical-align: middle; border: 0.5pt dashed rgb(0, 0, 0); background-color: rgb(192, 192, 192); height: 24pt; width: 117.75pt; font-family: 宋>体;'>$Disk</td>" >> $DIR/dev.log 39 #---enddisk--- 40 41 42 43 #---mem--- 44 Free=`awk -v Free_nr=$Free_nr -v Df_nr=$Df_nr '{if (NR<Free_nr && NR>Df_nr) print $0}' $DIR/ssh.log | awk -v mem=$mem '{if (NR==1) {b=$3/$2*100;a=$0}}END{if (b>mem) print a}'| awk '{print "total:"$2,"used:"$3,"free:"$4}'` 45 46 echo "<td class='et6' x:str='' height='32' width='157' style='font-size: 12pt; text-align: center; vertical-align: middle; border: 0.5pt dashed rgb(0, 0, 0); background-color: rgb(192, 192, 192); height: 24pt; width: 117.75pt; font-family: 宋>体;'>$Free</td>" >> $DIR/dev.log 47 #---endmem--- 48 49 #---time--- 50 Date=`date +%Y-%m-%d" "%H:%M:%S` 51 echo "<td class='et6' x:str='' height='32' width='157' style='font-size: 12pt; text-align: center; vertical-align: middle; border: 0.5pt dashed rgb(0, 0, 0); background-color: rgb(192, 192, 192); height: 24pt; width: 117.75pt; font-family: 宋>体;'>$Date</td>" >> $DIR/dev.log 52 #---endtime--- 53 54 echo '</tr>' >> $DIR/dev.log 55 done 56 } 57 disk="80%" 58 mem=90 59 avg='1' 60 Ssh 61 cat -v $DIR/dev.log | tr -d "^M" >> $DIR/dev2.log 62 echo '</tbody></table> <!--EndFragment-->' >> $DIR/dev2.log 63 cat $DIR/dev2.log| mutt -s 'cpu,mem,disk' -e 'set content_type="text/html"' yunwei@xxx.com
1 [root@zenghui expect]# cat config.txt 2 115.29.x.x,password,22,user,1 3 115.29.x.x,password,22,user,1 4 121.40.x.x,password,22,user,1 5 115.29.x.x,password,2220,user,1 6 192.168.11.82,password,22,root,0 7 121.199.x.x,password,2220,user,1 8 192.168.11.83,password,22,root,0 9 121.41.x.x,password,2220,user,1