Přeskočit obsah

Checking Whether IP Addresses Are Alive

TL;DR ✨

It is a familiar situation: there are several machines, and a quick way is needed to see whether any of them have gone offline. Here is a simple BASH script that uses the ping utility for quick diagnostics.

First, create a database containing the IP addresses of the individual machines. There are no delimiters; each IP address is on a separate line. For example:

It is a familiar situation: there are several machines, and a quick way is needed to see whether any of them have gone offline. Here is a simple BASH script that uses the ping utility for quick diagnostics.

First, create a database containing the IP addresses of the individual machines. There are no delimiters; each IP address is on a separate line. For example:

vi list.txt

8.8.8.8
77.75.76.3
77.78.126.235

:wq

Place the list.txt file in a directory, for example:

/home/radek/Dokumenty

Create the script:

vi pingVsude.sh
#!/bin/bash
# Název: pingVsude.sh
date
cat /home/radek/Dokumenty/list.txt |  while read output
do
    ping -c 1 "$output" > /dev/null
    if [ $? -eq 0 ]; then
    echo "IP $output je on"
    else
    echo "IP $output je off"
    fi
done

Change the permissions:

chmod 777 pingVsude.sh

Then Run the Command

 watch ./pingVsude.sh

This starts a continuously repeating check to see whether any IP address has gone offline. The author usually keeps this script running in a terminal on a second screen and glances at it from time to time to make sure everything is okay.

TIP: It is useful to run the script from cron at specific times and monitor whether the machines on the network are alive. The output is also worth saving to a log file. :-)