NetWatch is a simple bash script that monitors network connectivity to specified hosts and executes scripts when connections are lost.
netwatch.sh HOST SCRIPT
Where:
HOST
: IP address or hostname to monitorSCRIPT
: Command or script to execute when host becomes unavailable
netwatch.sh 1.1.1.1 /usr/local/bin/my_script
netwatch.sh 8.8.8.8 reboot
netwatch.sh 1.1.1.1 "echo hello world"
Make the script executable:
chmod +x netwatch.sh
You can change the time between checks by changing the variable DELAY in the script.
The script checks for the existence of the specified command or script. It also validates that the host is a valid IP address.
- The script pings the specified host every 10 seconds.
- If the host becomes unavailable (3 consecutive failed pings), it executes the specified script.
- When the host becomes available again, it resets the counters and status.