2017 the latest enterprise face Test Shell (ii)
Exercise 1: write a shell script that lists the IP of the 192.169.5.0/24 network segment online. (Locate the active IP)
Requirements are as follows:
1. The online IP and the online IP are placed in two files respectively to facilitate later inspection;
2. Do not affect the operation of the current terminal;
3. After the script has finished running, give the message that the script has finished running.
The script reads as follows:
Method One:
#!/bin/bash
[-f/etc/init.d/functions] &&. /etc/init.d/functions| | Exit1
# Verify that the system function file exists and call the system function if it exists, otherwise quit!
Ips= "192.169.5."
For I in $ (seq 254)
Do
Ping-c 2 $ips $i >/dev/null 2>/dev/null
If ["$?" = = "0"]
Then
echo "Echo $ips $i is online" >>/root/ip_online.txt
Else
echo "Echo $ips $i is not online" >>/root/ip_noline.txt
Fi
Done
If ["$ips $i"! = "192.169.5.255"];then
Action "shell script execution is complete! "/bin/true
Fi
Method Two:
#!/bin/bash
[-f/etc/init.d/functions] &&. /etc/init.d/functions| | Exit1
# Verify that the system function file exists and call the system function if it exists, otherwise quit!
For IP in $ (cat/root/ip.txt)
Do
Ping-c 2 $ip >/dev/null 2>/dev/null
If ["$?" = = "0"]
Then
echo "Echo $ip is online" >>/root/ip_online.txt
Else
echo "Echo $ip is not online" >>/root/ip_noline.txt
Fi
Done
If ["$ip"! = "192.169.5.255"];then
Action "shell script execution is complete! "/bin/true
Fi
Description
(1) If the script name ip_online.sh, execute the script without affecting the use of the current terminal, using the SH ip_online.sh & command.
(2) It is not recommended to use method two because editing a/root/ip.txt file is a waste of time.
This article is from the "Hand of the Paladin Control" blog, please make sure to keep this source http://wutengfei.blog.51cto.com/10942117/1961226
2017 the latest enterprise face Test Shell (ii)