Array Bash shell

Source: Internet
Author: User

Http://www.cnblogs.com/chengmo/archive/2010/09/30/1839632.html

#!/bin/The bash# script detects an offline host opportunity to send an email notification that can be run all the time. # Please set mail gatewany IP lastip Four items according to your need not set mail[Email protected]163. com #报警邮件接收地址gateway=192.168.1.253#内网网关ip Test the network is normal outipaddr=202.96.134.134#外网ip Test Whether the extranet is a normal IP="192.168.1."Lastip=(142 About211212 -221222) #需要监控的主机列表 can continue to add 142 About 211 212  - 221 222Unlastip=() #不在线主机列表length=${#lastip [@]} #unlength=${#unlastip [@]}echo"There's a total of $length servers in the detection"PS () {#ping online host list ping $ip $1-C1-W2|grep-q"TTL"#最多ping2次 time 2 seconds 2 seconds measured ping2 ping does not pass 2 seconds automatic stopif[$?-eq0];then # # # echo"Ping $ip Pass"# # #通Else# # # does not pass ping $gateway-C1-W2|grep-q"TTL"#本机ping网关 Test Purpose: To prevent the machine from being out of LANif[$?-eq0];then # # inside Netcom Echo"Ping $ip $ lost"Ping $outipaddr-C2-W2|grep-q"TTL"#本机ping外网if[$?-eq0];then # External Netcom Delete the offline host and notify the email echo"the IP that is currently being removed is: $"# echo"Add the former Unlastip array value to: ${unlastip[@]}"# echo"to add"Unlastip= (${unlastip[@]} $1# Add an offline host to the offline list # echo"Add Success"unset Lastip[i] #从数组中删除离线ip But the value of Echo Lastip[i] is empty you need to do the following: Echo"Current offline host list: ${unlastip[@]}"m=${lastip[@]} lastip=($m) #删除后剩余数组内容重新赋值给数组 echo"Current Online host list: ${lastip[@]}"length=${#lastip [@]} # unlength=${#unlastip [@]} # # fi echo ' Date '|mail-s"$ip $ lost"$Mail # #发邮件提醒断网时间 Echo"******************* Mail sent ************************"            Else# Send not successful echo"sent mail failed,please check"fi #Else# # Intranet not through Echo"can ' t reach the gateway, please check inet" #     Continuefi # #fi ###}pu () {#ping离线主机列表ping $ip $1-C1-W2|grep-q"TTL"#最多ping2次 time 2 seconds 2 seconds measured ping2 ping does not pass 2 seconds automatic stopif[$?-eq0];then # # # # # # # # # # # # # # # # # # # # of IPs here's The Echo"to add"Lastip= (${lastip[@]} $1) echo"Add Success"unset unlastip[j] #从数组中删除原数 echo"Current Online host list: ${lastip[@]}"m=${unlastip[@]} unlastip=($m) #删除后剩余数组内容重新赋值给数组 echo"currently not online host list ${unlastip[@]}"Unlength=${#unlastip [@]} # length=${#lastip [@]} #fi} while:; Do# Dead Loop # Improved loop, Unset${lastip[i]} Remove the ping IP in the arrayif(($length >0) ; then # Online host list non-empty to check the list of whether the host is really online, if not the line PS function will be offline host kicked out of the online list for((i=0;i< $length; i++)); Do#echo ${lastip[i]} PS ${lastip[i]} #exit done fi# before the improved loop, unable to delete the array original number # forIinch${lastip[*]}; Do# loop IP list #ps $i # $i is the $ in PS1#echo"sum:"$sum #done#echo $unlengthsleep1#echo"PS over and PU start"  if(($unlength >0) and then # There is an offline host to start checking whether the offline host is restored online, the PU function will be added to the online host list to restore the online host for((j=0;j< $unlength; j + +)); Dopu ${unlastip[j]} done fi# echo"Pu over and PS start"Echo"Current Online host list: ${lastip[@]}"Echo"currently not online host list ${unlastip[@]}" Done

Array Bash shell

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.