Check connection using Nmap / Linux Crontab

Sometimes you need to monitor a connection between two hosts, a single service or a host. This script check a connection using Nmap, and requires the IP and the port of the service.

Use a log file

Using a log file can be useful to generate reports, but also for checking the last status of the connection. If we don’t check last status, the script will notify for every check. We don’t want that; we want to be notified when the connection is unsuccessful and when it’s back.

To write informations on the console and in a log file, we can use tee  command.

How to use it for your own needs

Edit the IP and the port of the service/host you want to monitor and edit the log file. Please run this script with a user with permissions to create/write the log file. If you don’t want to be notified over mail, and only write a log file, you can remove mail  line.

Connection checker bash script

Latest version: GitHub

Set this script as a cron job

To setup a cron job for current user crontab, you can edit personal crontab with crontab -e.  There we will add this script to run every 10 minutes.

To list all your cronjobs for your user, run crontab -l.

Leave a Reply

Your email address will not be published. Required fields are marked *