Skip to content

Instantly share code, notes, and snippets.

@tomasinouk
Created October 27, 2016 22:43
Show Gist options
  • Save tomasinouk/d757b98eda9846a4f57e4f287efb4a27 to your computer and use it in GitHub Desktop.
Save tomasinouk/d757b98eda9846a4f57e4f287efb4a27 to your computer and use it in GitHub Desktop.
Script for SmartWorkx, Conel routers to check connection against 2 IP addresses. Good solution, to engage failover after both servers are not reachable.

Here is the start-up script, which in effect does check of 2 IP addresses

#!/bin/sh
#
# This script will be executed when PPP/WAN connection is established.
#
# Script for checking connection by two ping ip addresses
#
# Put checking ip addresses
# change this to IP addresses of your SCADA, etc. servers
PING_IPADDRESS1=8.8.8.8
PING_IPADDRESS2=8.8.4.4

# Put ping period in seconds
PING_INTERVAL=10

# Put max. failed ping count before MWAN restarting
MAX_FAILED_PING=5

PING_TIMEOUT_CNT1=0
PING_TIMEOUT_CNT2=0

while [ 1 ]
do
   DATE=`date +"%Y-%m-%d %H:%M:%S"`
   PING_REPLY_TIME1=`ping -c 1 $PING_IPADDRESS1 | awk '/time=/ { print $7 }' | sed -e 's/time=//' -e 's/\.[0-9]//'`
   if [ "$PING_REPLY_TIME1" -eq "" ]; then
       echo "$DATE Check Connection Script: ping time-out to $PING_IPADDRESS1" >> /var/log/messages
       if [ "$PING_TIMEOUT_CNT1" -le "$MAX_FAILED_PING" ]; then
          PING_TIMEOUT_CNT1=$((PING_TIMEOUT_CNT1+1))
       fi
       echo $PING_TIMEOUT_CNT1
   else
       PING_TIMEOUT_CNT1=0
   fi

   PING_REPLY_TIME2=`ping -c 1 $PING_IPADDRESS2 | awk '/time=/ { print $7 }' | sed -e 's/time=//' -e 's/\.[0-9]//'`
   if ["$PING_REPLY_TIME2" -eq ""]; then
       echo "$DATE Check Connection Script: ping time-out to $PING_IPADDRESS2" >> /var/log/messages
       if [ "$PING_TIMEOUT_CNT2" -le "$MAX_FAILED_PING" ]; then
          PING_TIMEOUT_CNT2=$((PING_TIMEOUT_CNT2+1))
       fi
       echo $PING_TIMEOUT_CNT2
   else
       PING_TIMEOUT_CNT2=0
   fi

   if [ "$PING_TIMEOUT_CNT1" -ge "$MAX_FAILED_PING" ] && [ "$PING_TIMEOUT_CNT2" -ge "$MAX_FAILED_PING" ] ; then
      echo "$DATE Check Connection Script:  Restarting MWAN connection" >> /var/log/messages
      service ppp restart
      exit 0
   fi
   sleep $PING_INTERVAL
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment