To verify the connectivity of all IP addresses within a network segment, multiple approaches can be used. Two methods are presented here: a single-process implementation and a multi-process version.
Single-Process Ping Implementation (Sequential Processing)
#!/bin/bash
read -p "Enter the network portion of the IP address: " network_ip
for host in $(seq 1 254);
do
{
ping ${network_ip}.${host} -c 1 -s 1 2>&1 1>/dev/null && \
echo -e "ping ${network_ip}.${host} is \033[32;49;1mreachable\033[39;49;0m" || \
echo -e "ping ${network_ip}.${host} is \033[31;49;1munreachable\033[39;49;0m"
}
done
echo "Scanning completed."
Multi-Process Ping Implementation (Using & and wait)
#!/bin/bash
read -p "Enter the network portion of the IP address: " network_ip
for host in $(seq 1 254);
do
{
ping ${network_ip}.${host} -c 1 -s 1 2>&1 1>/dev/null && \
echo -e "ping ${network_ip}.${host} is \033[32;49;1mreachable\033[39;49;0m" || \
echo -e "ping ${network_ip}.${host} is \033[31;49;1munreachable\033[39;49;0m"
} &
done
wait
echo "Scanning completed."
Common ping parameters:
-c count: Specifies the number of ping requests to send.-w deadline: Sets a timeout in seconds for the ping operation.-I interface: Defines the network interface to use for sending packets.-t ttl: Sets the Time To Live value for the packet.-s packetsize: Sets the size of the data payload in bytes.-W timeout: Waits for a reply for a specified time in seconds.
Summary: Since shell scripting does not support true multithreading, multi-process exceution is used instead. This is achieved by appending & to commands inside a loop to run them in the background. Using wait ensures that the script waits for all background processes to finish before proceeding.
Source: https://mefj.com.cn/lur3644.html