Summary: This script pings a specified URL/IP and can trigger a system or application restart if the destination is unreachable.
Details: This script was conceived for a friend that was having an issue where is registration with AllStarLink was consistently failing. After some research, we realized that it was more of an AllStar registration issue and was addressed by enabling the HamVoip additional registration method. It sat on my computer for a while, but was brought back after talking to another friend regarding the implementation of a node that would utilize a cellular connection. We brainstormed about how to ensure that the VPN connection was stable as physical access to the repeater site was a bit complicated. I made some changes to the script so that a service reset could be triggered instead of a full system restart. I use this on my supported nodes as it keeps a record of the network connections, but the script could be edited to run any type of command.
How to implement: There are six variables that you can set for this script.
- target: This is the website address or IP that you wish to ping.
- log: Tells the script if you want a log file created.
- log_location: Location of the log file. This defaults to /usr/local
- log_file_name: What you want your log file named. Do not add a .txt to this name.
- restart_script: This sets if you want the script to perform a restart if the connection fails.
- script_type: If the restart parameter is set to yes, you can tell the script what you want to restart. asterisk will perform a asterisk service restart where computer will perform a full system restart.
Automatically running your script: Once you have the script saved to your favorite location (typically either /etc/asterisk/local) you can create a CRON job and have it run hourly on the half hour. This is accomplished by running the command “crontab -e” and adding the entry 30 * * * * /etc/asterisk/local/pingtest.sh. This Cron expression translates to ““At minute 0“.
#!/bin/bash
# SCRIPT NAME: pingtest.sh
# CREATED BY: Justin Grigg
# CREATED ON: 04/21/2020
# SCRIPT PURPOSE: Pings IP/DNS and can trigger a script if unavailable
# -------------------------------------------------------------------------
# set script options below. Only edit between " "
# -------------------------------------------------------------------------
target="register.hamvoip.org" # address you wish to ping
log="on" # if you wish to create a log file ( on / off )
log_location="/usr/local" # standard location is the /usr/local folder. Do not add /
log_file_name="pingresult" # will create a .txt file with this name. Do not add .txt!!
restart_script="off" # if restart will be triggered ( on / off )
script_type="asterisk" # restart type (computer/asterisk)
# -------------------------------------------------------------------------
# Please do not edit below this line
# -------------------------------------------------------------------------
timestamp=$(date)
path=$log_location"/"$log_file_name".txt"
# requests a single ping from target address and looks for a result
pingcount=$( ping -c 1 $target | grep icmp* | wc -l )
# if pingcount return is equal to 0
if [ $pingcount -eq 0 ]
then
# TEST FAILED
# LOG SECTION
# checks to see if the create log flag is set to on
# if enabled, will create a log entry
if [ $log = "on" ]
then
echo "FAIL |" $timestamp "|" $target >>$path
echo "--- Restart Flag set to |" $restart_script >>$path
fi
# RUN SCRIPT SECTION
# checks to see if the run script flag is set to on
# if enabled, will create a log entry and run the script
if [ $restart_script = "on" ]
then
# updating timestamp
restarttime=$(date)
# updating log that restart command was sent
echo "---"$script_type "restart command sent on" $restarttime >>$path
# command that will run if ping failed
if [ $script_type="computer" ]
then
/sbin/shutdown -r now
fi
if [ $script_type="asterisk" ]
then
/usr/local/sbin/astres.sh
fi
fi
else
# TEST PASSED
## checks to see if the create log flag is set to on
# if enabled, will create a log entry
if [ $log = "on" ]
then
echo "PASS |" $timestamp "|" $target >>$path
fi
fi