Linux / UNIX: Ping a Block of Hosts (netblock)

Linux / UNIX: Ping a Block of Hosts (netblock)

To ping a block of hosts (also known as a netblock) on a Linux or Unix system, you can use a combination of the ping command and the for loop.

Here is an example of how to ping a block of hosts on a Linux or Unix system:

for i in {1..254}; do ping -c1 192.168.1.$i > /dev/null; if [ $? -eq 0 ]; then echo "Host 192.168.1.$i is up"; fi; done
Source:ww‮al.w‬utturi.com

This command will ping each host in the 192.168.1.1 to 192.168.1.254 range and display a message if the host is reachable.

The for loop iterates over the range of IP addresses, and the ping command is used to send a single ICMP echo request to each host. The -c1 option specifies that only one packet should be sent, and the > /dev/null redirection sends the output of the ping command to /dev/null, so it is not displayed on the console.

The if statement checks the exit code of the ping command using the $? special variable. If the exit code is 0 (indicating that the ping was successful), the echo command is used to print a message indicating that the host is up.

You can customize this command to fit your specific needs by modifying the IP range and the message displayed when a host is reachable.

Keep in mind that the ping command may not work as expected on all systems, due to network configurations or other factors. If the ping command is not available or does not work as expected, you can use other tools, such as nmap, to scan a block of hosts.

Created Time:2017-10-29 22:08:57  Author:lautturi