#!/bin/sh

#Skrypt do sprawdzania polaczenia sieciowego
# $1 delay time
# $2 ping count
# $3 host
# $4 action reboot|wan

#DELAY="240"
#HOST="192.168.109.1"
#COUNT="5"

UPTIME=$(awk '{printf "%d", $1}' /proc/uptime)
[ $UPTIME -le $1 ] && exit 0

n=0
while [ $n -lt $2 ]
do
#  sleep 20
  if ping -c1 -W2 $3 >/dev/null
  then
echo "SIEC OK"
#sprawdzam
    exit 0
  fi
  n=$((n+1))
  sleep 1
done

echo "BRAK NETA"

	case "$4" in
		"reboot")
			logger -t $0 "Reboot"
			reboot
			;;
		"wan")
			logger -t $0 "WAN Restart"
			(ifdown wan; sleep 5; ifup wan) &
			;;
		*)
			logger -t $0 "Run custom script"
			($4) &
			;;
	esac
echo "END"
