Shell notes on how to check ip addresses occupied by scripts

Source: Internet
Author: User

Modify the script to check which ip addresses are occupied.

#! /Bin/bash

For I in {1 .. 10} // assign the I variable 1-10

Do // what to do

Ping-c1-w1 192.168.7. $ I &>/dev/null // ping 192.168.7

If [$? -Eq 0]; // whether the returned value is 0

Then // If the returned value is 0, do the following output:

Echo station $ I is up!

Else // otherwise

Echo station $ I is down! // Output this step

Fi done

Grant the script executable permission: chmod + x ipadd. sh

Run the script:

[Root @ localhost shellscripts] # ipadd. sh

Station1 is up!

Station2 is down!

Station3 is up!

Station4 is down!

Station5 is down!

Station6 is down!

Station7 is down!

Station8 is down!

Station9 is down!

Station10 is down!

======================================

[Root @ localhost shellscripts] # ping 192.168.7.3

PING 192.168.7.3 (192.168.7.3) 56 (84) bytes of data. 64 bytes from 192.168.7.3:

Icmp_seq = 1 ttl = 64 time = 0.043 MS 64 bytes from 192.168.7.3:

Icmp_seq = 2 ttl = 64 time = 0.040 MS ^ C --- 192.168.7.3

Ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1791 ms rtt min/avg/max/mdev = 0.040/0.041/0.043/0.006 ms

[Root @ localhost shellscripts] # echo $? // The ping command returns 0.

0 ====================================

[Root @ localhost shellscripts] # ping 192.168.7.4

PING 192.168.7.4 (192.168.7.4) 56 (84) bytes of data.

From 192.168.7.3 icmp_seq = 1 Destination Host Unreachable From 192.168.7.3

Icmp_seq = 2 Destination Host Unreachable From 192.168.7.3

Icmp_seq = 3 Destination Host Unreachable ^ C --- 192.168.7.4

Ping statistics --- 5 packets transmitted, 0 received, + 3 errors, 100% packet loss, time 4084 ms pipe 3

[Root @ localhost shellscripts] # echo $? 1 // ping failure. The return value is not 0.

 

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.