Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # http://tomatousb.org/forum/t-454177/wan-dhcp-issues
- # modified by Xruptor
- #run this script only once on router reset, sometimes it runs multiple times for some reason
- chkFile="/tmp/CHECKWAN"
- if [ -f "$chkFile" ]
- then
- logger -p NOTICE -t CHECKWAN "CHECKWAN already initialized, exiting."
- exit
- fi
- #create the tmp file so that we don't run this more than once
- touch $chkFile
- # No of times to check each host before trying next
- TRIES=1
- # How often to check in seconds if OK
- TCHECK=20
- # How long to wait if all hosts fail
- TFAIL=10
- # How many fails before restarting dhcp
- NFAIL=3
- #don't touch this
- notice=1
- # defaults don't touch
- GATEWAY="0.0.0.0"
- UP=0
- HSTS="8.8.8.8 8.8.4.4"
- chk=0
- dn=0
- DNSCHK(){
- cnt=0
- chk=0
- while [ $cnt -lt $TRIES ]; do
- if /bin/ping -c 1 -W 5 -w 5 $@ >/dev/null; then
- chk=1
- break
- else
- let cnt=cnt+1
- fi
- done
- }
- PINGCHK(){
- chk=0
- if /bin/ping -c 1 -W 5 -w 5 8.8.8.8 >/dev/null; then
- chk=1
- fi
- }
- GETGATEWAY() {
- GATEWAY=`nvram get wan_gateway`
- if [ -z "$GATEWAY" ]; then
- GATEWAY="0.0.0.0"
- fi
- if [ $GATEWAY != "0.0.0.0" ]; then
- HSTS="$GATEWAY 8.8.8.8 8.8.4.4"
- else
- HSTS="8.8.8.8 8.8.4.4"
- fi
- }
- while [ true ]; do
- sleep 15
- if [ $notice -lt 2 ]; then
- let notice=notice+1
- logger -p NOTICE -t CHECKWAN "CHECKWAN has initialized."
- fi
- dn=0
- GETGATEWAY
- while [ $dn -lt $NFAIL ]; do
- for h in $HSTS; do
- UP=0;
- DNSCHK $h
- if [ $chk -gt 0 ]; then
- UP=1
- break
- else
- logger -p NOTICE -t CHECKWAN "WAN Ping check failed on host $h"
- fi
- done
- if [ $UP -gt 0 ]; then
- dn=0
- sleep $TCHECK
- else
- let dn=dn+1
- logger -p NOTICE -t CHECKWAN "WAN Check Failure # $dn"
- sleep $TFAIL
- fi
- done
- logger -p NOTICE -t CHECKWAN "Releasing DHCP for forced renewal"
- #reset chk for PING check, JUST IN CASE!!
- chk=0
- #reset DHCP by using Kill command first
- #-----------------------------
- killall -SIGUSR1 udhcpc
- #-----------------------------
- logger -p NOTICE -t CHECKWAN "Attemping to renew the DHCP."
- #grab new IP address, try restarting the wan
- service restart_wan
- #sleep 5
- #do a ping check until WAN is back up
- while [ $chk -lt 1 ]; do
- sleep 15
- PINGCHK
- done
- logger -p NOTICE -t CHECKWAN "WAN DHCP has been renewed."
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement