HomeHowTo › Network Monitoring With A Bash Script

Network Monitoring With A Bash Script

To perform some basic network monitoring tasks without installing any software like Nagios, you can just run a bash script on your workstation. I have written a little script that checks my hosts by periodically sending pings and port checks. If you don’t necessarily need realtime monitoring of complex services with a web interface, statistics and email notifications, you might find this useful.

The Script’s Output



The script prints one line for each monitored host. A line contains the host’s description, its IP address or domain name and the crucial ports.

Colors indicate the status:

Green –> IP and/or port check successful
Orange –> IP check skipped (if the firewall doesn’t allow pinging)
Red –> IP and/or port unreachable

As for this example, you can easily tell that there is a problem with the mail server’s webserver and that the access point in room 006 is down.




The Sourcecode




This script was written to run on OS X. Except for the audio notification part on the bottom, it also runs on Ubuntu and it might work on other distributions as well.

1. Defining Hosts

Hosts and ports are stored in an array. Each line must have the following structure:

host=("${host[@]}" "" "Mail" "25" "80" "110" "143" "443" "--")

The first entry is always¬†“${host[@]}” to include everything from before. After that comes the IP address or domain name and then the description, followed by none, one or multiple ports. Finally, to mark the end of the record, comes a “–“.

2. Ping Exceptions

If a ping fails, the script will skip the port checks for that host. Some hosts, however, might always be unpingable due to firewall restrictions. If you still want to perform port checks on these hosts, you can add their IP addresses or domain to the “noping” variable. You can use anything as a delimiter as the script will only check if the host is anywhere within that string.


3. UDP Port Checks

By default the script will check ports using TCPv4. If you have some ports that need to be checked via UDP, you can add these to the “udp” variable. Again, anything can be used as a delimiter.


4. Ping & Netcat Tweaking

Depending on your environment and network speed you might need to tweak the ping and netcat parameters.

5. Audio Notification

It might be useful to play a sound when an error was detected. On OS X you can use the “afplay” command to play AIFF files and the “say” command for text-to-speech.

6. Pause Between Checks

The “sleep” command controls the pause between checks. I have set this to 10 minutes.

2 Comments.[ Leave a comment ]

  1. Hi and thanks for shareing this nice script.
    But i cant get the portcheck, nc -command, to work. I use Ubuntu 14.10.
    If use this string nc -4 -w 3 -z -v hotspot.stefnet.se 443 i get Connection to hotspot.stefnet.se 443 port [tcp/https] succeeded! but without the -v i dont get anything. If i add the -v in your script it prints the answer on the screen, but it shouldt echo anything i think… Any sudgestions?
    Regards Stefan

  2. Hi Stefan, thanks for the feedback. I can confirm the behaviour that you described. nc -z doesn’t seem to output reliably when not using -v concurrently. The -v, however, wasn’t compatible with my script — I don’t know why. Therefore I have re-written the port check part (and the ping part as well). I am no longer using that weird grep construction to look for a success message, but $? which contains the error code of the previously executed command. The ping part contained another stumbling block. I was using the -t parameter to add a 2 second timeout. This works fine on OS X, but on Ubuntu -t is for TTL, so -W has to be used instead. Another change: domain names can be used instead of IP addresses now.

Leave a Comment