Shell script for detecting network segment online hosts

Source: Internet
Author: User

Job Four:

Write a script:

Implement Ping Class C network segment, Class B network segment and all hosts in Class A network segment;

The network segment address lets the user input, moreover uses the script to judge the network segment type;

#!/bin/bash #Author: MOS #Script name:funPing.sh #Date & time:2012-10-09/21:53:09 #Version: 1.0.1 #Descri Ption: # #定义C类网段处理函数 Net_c () {#定义统计不在线IP数量的变量 declare-i fail=0 #生成C类网的最后位IP, loop ping for I in ' seq 1 254 ';d o #ping只发送一个, just wait 1 seconds, get all the information and throw it away, just go logical value if PING-C 1-w 1 $ $i &>/dev/null; then echo  
"Online $ $i" Else let fail++ fi do echo "No online PC, number is $Fail"} Net_b () {#截取B类网的网段 bip= ' echo $1|cut-d '. '-f1-2 ' declare-i fail=0 #循环生成B类网第三段 for i in ' seq 0 254 '; Do res= "$Bip. $i" #循环生成B类网第四段 for R in ' SEQ 1 254 ';d o if ping-c 1-w 1 $Res. $r &>/de  
        V/null then echo "Online $Res. $r" Else let fail++ fi Done the echo "No Online PC, number is $Fail"} net_a () {bip= ' echo $1|cut-d '. '-f1 ' Decl Are-i fail=0 #生成A类网的第Two paragraphs, third and fourth paragraphs, and so on for I in ' seq 0 254 ';d o res= ' $Bip. $i "for R in" Seq 0 254 ';d o res1= ' $Res.     
                    $r "for L in ' SEQ 1 254 ';d o if ping-c 1-w 1 $Res 1. $l &>/dev/null; then  
            echo "Online $Res 1. $l" Else let fail++ fi Done does echo "No Online PC, number is $Fail"} read-p "please input subset:" Ip #判断输入IP段 Whether the fall is legal if [[$Ip =~ ^ ([2-9]|[ 1-9][0-9]|1[0-9][0-9]|2[01][0-9]|22[0-3]) \. ([0-9]| [1-9] [0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4]) \. ([0-9]| [1-9] [0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4]) $]];then #合法, remove the first field, which type of net mask= ' echo ' $Ip ' |cut-d '.-f1 #如果是A类网  
    A function, and so on [$Mask-lt 128] && net_a $Ip [$Mask-lt 192-a $Mask-gt 127] && net_b $Ip [$Mask-gt 191] && net_c $Ip Else #如果输入IP错误, the error message is as follows Echo "error! $Ip invalid. "Fi

If you want to use the above code, be cautious ... In particular, Class A and class B nets ... 255*255*255 The resource cost, you ponder .... Well, if it's only for testing, you can output the results to a file, put it in the background, or just test a specific IP, you can change the number of SEQ in the For loop, modify it, is no problem ....

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.