Let’s do a simple experiment, to check the Internet connection between the user’s computer and a set of IPs by a shell script.
The basic command tool is Ping which sends packets to network hosts.
It will receive a network response in a short time if the Internet connection is good.

$ ping baidu.com
PING baidu.com (220.181.38.148): 56 data bytes
64 bytes from 220.181.38.148: icmp_seq=0 ttl=52 time=58.793 ms
64 bytes from 220.181.38.148: icmp_seq=1 ttl=52 time=85.885 ms
64 bytes from 220.181.38.148: icmp_seq=2 ttl=52 time=58.432 ms

We can write the following shell script based on Ping.
It tried to connect 255 IPs from 220.181.38.1 to 220.181.38.255.

#! /bin/bash
for ip in 220.181.38.{1..255}; do
    ping ${ip} -c2 &> /dev/null
    if [ $? -eq 0 ]; then
        echo ${ip} is alive
    fi
done

But the whole running process spent a too long time, we need to fix the issue.
Use & to put all ping events in some child shell process, and wait for them in the main process. Programmer can keep watch over all kinds of processes running on this computer by the command top.

#! /bin/bash
for ip in 220.181.38.{1..255}; do
    (
    ping ${ip} -c2 &> /dev/null
    if [ $? -eq 0 ]; then
        echo ${ip} is alive
    fi
    )& # "&" means run in background
done
wait

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments

Content Summary
: Input your strings, the tool can get a brief summary of the content for you.

X
0
Would love your thoughts, please comment.x
()
x