Listing all available machines on a network
When we monitor a large network, we need to check the availability of all machines. A machine may not be available for two reasons: it is not powered on, or because of a problem in the network. We can write a shell script to determine and report which machines are available on the network.
Getting ready
In this recipe, we demonstrate two methods. The first method uses ping and the second method uses fping
. The fping
command is easier for scripts and has more features than the ping command. It may not be part of your Linux distribution, but can be installed with your package manager.
How to do it...
The next example script will find the visible machines on the network using the ping command:
#!/bin/bash #Filename: ping.sh # Change base address 192.168.0 according to your network. for ip in 192.168.0.{1..255} ; do ping $ip -c 2 &> /dev/null ; if [ $? -eq 0 ]; then echo $ip is alive fi done
The output resembles this:
$ ./ping.sh 192...