site nav‎‏‎‎‏‎

categories‎ > ‎code‎ > ‎

ip watchers (watchip and watchip2)

posted 11 Oct 2009 10:10 by Slippy Lane   [ updated 12 Oct 2009 15:43 ]
watchip

#!/bin/bash

#watchip v0.1TEST by slippy
#quick script to periodically ping an IP and report when the IP fails to respond
#requires zenity for popup message
#this version is just a proof-of-concept.
#TODO
#  modify to use hardware interface to power-cycle the router on network drop
#LICENSE
#  Public Domain. Do with it what you will.

cWatch="watch --interval=60 $0 --check"
defIP="192.168.2.1"

if [ $# = 0 ]; then
  echo "No parameters supplied. Running with default IP."
  $cWatch $defIP
else
  if [ "$1" = "--check" ]; then
    echo "Checking $2..."
    result=`ping -c 1 $2 | grep "1 received"`
    if [ -z "$result" ]; then
      echo "Host $2 is unreachable!"
      if [ -n `which zenity` ]; then
        zenity --warning --text="Host $2 is unreachable\!"        
      fi
    else
      echo "$result"
    fi
    echo "Done."
  else
    echo "Running with parameter \"$*\""
    $cWatch $*
  fi
fi

  
watchip2

#!/bin/bash

#watchip v2.0 by slippy
#quick script to periodically ping an IP and power-
#cycle the router when the IP fails to respond.
#written for background-running and easy tweaking

#LICENSE: Public Domain. Do with it what you will.


numWatchInterval=60
strIP="192.168.2.1"
numRelay=1
numSleepInterval=10
numPowerupInterval=30
cmdLog="null"
cmdAlert="echo"
cmdOff="/pub/scripts/switchoff"
cmdOn="/pub/scripts/switchon"

while true; do
  $cmdLog "Checking $strIP"
  strResult=`ping -c 1 $strIP | grep '1 received'`
  if [ -z "$strResult" ]; then
    $cmdAlert "`date` : Host $strIP unreachable. Commencing power cycling."
    $cmdLog "Opening relay $numRelay"
    $cmdOff $numRelay
    $cmdLog "Sleeping for $numSleepInterval seconds."
    sleep $numSleepInterval
    $cmdLog "Closing relay $numRelay."
    $cmdOn $numRelay
    $cmdLog "Waiting for ping response."
    strResult=`ping -c 1 $strIP | grep '1 received'`
    while [ -z "$strResult" ]; do
      $cmdLog "None yet. Sleeping for $numPowerupInterval seconds."
      sleep $numPowerupInterval
      $cmdLog "Checking again..."
      strResult=`ping -c 1 $strIP | grep '1 received'`
    done
    $cmdAlert "`date` : Got ping response. Power-cycling complete."
  fi
  $cmdLog "Sleeping for $numWatchInterval seconds."
  sleep $numWatchInterval
done

 
Č
ċ
ď
watchip
(2k)
Slippy Lane,
11 Oct 2009 12:50
ċ
ď
watchip1.0
(2k)
Slippy Lane,
11 Oct 2009 12:50
ċ
ď
watchip2
(1k)
Slippy Lane,
11 Oct 2009 12:50
ċ
ď
watchip2.1
(2k)
Slippy Lane,
11 Oct 2009 12:50