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.