Shell script for detecting network segment IP usage _linux shell

Source: Internet
Author: User
Tags ping network
Copy Code code 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 "'-----> Script starts executing ..."
While [$IP-LT 255]
Todo
host= $network. $IP
echo "--------> start to detect $host server communication is normal, ping times $ping_count."
Ping $host-C $ping _count >.ping_tmp
Sleep 1
Cat. Ping_tmp >>ping_action
echo "--------> Server $host detection is complete."
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 already in use"
echo "--> $host IP is already in use" >>ip_use
Else
echo "$host IP is currently idle: $loss _ping"
echo "$host IP currently idle" >>ip_idle
Fi
ip=$ ((ip+1))
Done
echo "' Date" +%y%m%d%h:%m:%s "'-----> Script completed ..."


To ping a network segment of all the IP, and detect the network connection status is normal, many methods can be implemented, the following brief description of two, as follows:

Script 1
Copy Code code as follows:

#!/bin/sh
# Ping network segment all IP
# 2012/02/05
Ip=1 #通过修改初值
While [$ip!= "254]; Todo
#yes正常, no host does not exist or is not normal
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 results:
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 2
Copy Code code as follows:

#!/bin/sh
# Ping network segment all IP
# 2012/02/05
Ip= "192.168.0."
For i in ' SEQ 1 254 '
Todo
Ping-c 2 $ip $i | Grep-q ' ttl= ' && echo "$ip $i yes" | | echo "$ip $i No"
#yes正常, no host does not exist or is not normal
Done

Output results:
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.