通過shell編程來測試單個主機或網段主機存活

來源:互聯網
上載者:User

題目:編寫shell指令碼,使用ping判斷指定IP或者IP端內的主機存活。

只接受規定的IP格式輸入和IP段輸入(例如:192.168.2.88 或 192.168.2.)注意,寫IP段的時候後面有個點...指令碼簡陋...看官將就一下吧;上代碼:

#!/bin/bashread -p "Please input ip:(192.168.0.22) or (192.168.0.)" IpTest=`echo $Ip|grep "\<[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\$"`Test1=`echo $Ip|grep "\<[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.\$"`#echo "a IP is: $Test"#echo "IP.is  $Test1"if [[ -z $Test && -z $Test1 ]];then        echo "input error!"        exit 1elif [[ -n $Test ]];then        for i in `seq 1 4`        do                Cut=`echo $Test|cut -d"." -f$i`                if [ $i -eq 1 ];then                        if [ $Cut -gt 223 -o $Cut -lt 1 ];then                                echo "input $Test ---->> $Cut invalid"                                exit 2                        fi                 else                        if [ $Cut -gt 254 ];then                                echo "input $Test ---->> $Cut invalid"                                exit 2                        fi                 fi         done                    if ping -c 2 -W 1 $Test &> /dev/null ;then                        echo "Have $Test"                else                        echo "No $Test"                fielse        for i in `seq 1 3`        do                Cut=`echo $Test1|cut -d"." -f$i`                if [ $i -eq 1 ];then                        if [ $Cut -gt 223 -o $Cut -lt 1 ];then                                echo "input $Test1 ---->> $Cut invalid"                                exit 2                        fi                else                        if [ $Cut -gt 254 ];then                                echo "input $Test1 ---->> $Cut invalid"                                exit 2                        fi                fi        done                declare -i Num                declare -i Fail                Num=0                Fail=0                for i in `seq 1 10`                    do                        if ping -c 1 -W 1 ${Test1}$i &> /dev/null ;then                                echo "Have ${Test1}$i"                                Num=$((Num+1))                        else                                Fail=$((Fail+1))                                echo "No ${Test1}$i"                fi        done        echo "Host online have $Num ."        echo "Host no online have $Fail ."

最後一個for迴圈,我嫌多,唯寫了10個值,有興趣的哥們,自己改 seq 1 10改成1 254即可...還有關於a類b類c類網,由於本人網路方面知識的欠缺,所以判斷的值,估計挺不正確的,列位就諒解一下吧....

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.