Shell programming to test a single host or network segment host to survive

Source: Internet
Author: User
Tags exit

Title: Write a shell script that uses ping to determine whether the host in the specified IP or IP side is alive.

Accept only the specified IP format input and IP segment input (for example: 192.168.2.88 or 192.168.2.) Note that there is a point behind the Write IP segment ... Script Simple ... Reader, please. Code on:

#!/bin/bash read-p "Please input IP: (192.168.0.22) or (192.168.0.)" Ip test= ' 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 $Test 1 "If [[z $Test &&-z $Test 1]];then Echo]
        Input error! "
                Exit 1 elif [[-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----&G
    
                T;> $Cut Invalid "Exit 2 fi fi done If Ping-c 2-W 1 $Test &>/dev/null then echo "Have $Test" Else  echo "No $Test" fi else for I-in ' SEQ 1-3 ' do cut= ' echo $Test 1|cut-d "."
                                -f$i ' If [$i-eq 1];then if [$Cut-gt 223-o $Cut-lt 1];then
                        echo "input $Test 1---->> $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 ' 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."

The last for the loop, I too much, only wrote 10 values, interested in the brothers, their own changes to the 1 10 seq to 1 254 can ... There is a Class B class C network, due to my lack of knowledge of the network, so the value of the judgement, estimated quite incorrect, you will understand it ....

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.