Shell script used to check IP address usage

Source: Internet
Author: User

Copy codeThe Code is as follows :#! /Bin/bash
# Main ---
Network = 192.168.1
Ping_count = 3
IP = 1
:> IP_use
:> IP_idle
:> Ping_action
Echo "'date" + % Y % m % d % H: % M: % S "'-----> run the script ......"
While [$ ip-lt 255]
Do
Host = $ network. $ IP
Echo "--------> start to check if $ host server communication is normal. ping times $ ping_count ."
Ping $ host-c $ ping_count>. ping_tmp
Sleep 1
Cat. ping_tmp> ping_action
Echo "--------> the server $ host detection has been completed ."
Sum_ping = 'Tail-2. ping_tmp | head-1 | awk-F, '{print $2}' | cut-c 2-2'
Loss_ping = 'Tail-2. ping_tmp | head-1 | awk-F, '{print $4}' | cut-c 2-5'
If [$ sum_ping-eq $ ping_count]; then
Echo "--> $ host IP is in use"
Echo "--> $ host IP is in use"> IP_use
Else
Echo "$ host IP is currently idle: $ loss_ping"
Echo "$ host IP is currently idle"> IP_idle
Fi
IP = $ (IP + 1 ))
Done
Echo "'date" + % Y % m % d % H: % M: % S "'-----> the script has been run ......"

To Ping all IP addresses in a CIDR Block and check whether the network connection status is normal, you can use many methods. The following describes two methods:

Script 1Copy codeThe Code is as follows :#! /Bin/sh
# Ping all IP addresses of a CIDR Block
#2012/02/05
Ip = 1 # modifying the Initial Value
While [$ ip! = "254]; do
# Yes: The no host does not exist or is abnormal.
Ping 192.168.0. $ ip-c 2 | grep-q "ttl =" & echo "192.168.0. $ ip yes" | echo "192.168.0. $ ip no"
Ip = 'expr "$ ip" "+" "1 "'
Done

Output result:
192.168.0.1 yes
192.168.0.2 no
192.168.0.3 no
192.168.0.4 no
192.168.0.5 yes
192.168.0.6 no
192.168.0.7 yes
...

Script 2Copy codeThe Code is as follows :#! /Bin/sh
# Ping all IP addresses of a CIDR Block
#2012/02/05
Ip = "192.168.0 ."
For I in 'seq 1 254'
Do
Ping-c 2 $ ip $ I | grep-q 'ttl = '& echo "$ ip $ I yes" | echo "$ ip $ I no"
# Yes: The no host does not exist or is abnormal.
Done

Output result:
192.168.0.1 yes
192.168.0.2 no
192.168.0.3 no
192.168.0.4 no
192.168.0.5 yes
192.168.0.6 no
192.168.0.7 yes
...

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.