Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2022
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.45 KB | None | 0 0
  1. #!/bin/sh
  2. # http://tomatousb.org/forum/t-454177/wan-dhcp-issues
  3. # modified by Xruptor
  4.  
  5. #run this script only once on router reset, sometimes it runs multiple times for some reason
  6. chkFile="/tmp/CHECKWAN"
  7.  
  8. if [ -f "$chkFile" ]
  9. then
  10.    logger -p NOTICE -t CHECKWAN "CHECKWAN already initialized, exiting."
  11.    exit
  12. fi
  13.  
  14. #create the tmp file so that we don't run this more than once
  15. touch $chkFile
  16.  
  17. # No of times to check each host before trying next
  18. TRIES=1
  19. # How often to check in seconds if OK
  20. TCHECK=20
  21. # How long to wait if all hosts fail
  22. TFAIL=10
  23. # How many fails before restarting dhcp
  24. NFAIL=3
  25.  
  26. #don't touch this
  27. notice=1
  28.  
  29. # defaults don't touch
  30. GATEWAY="0.0.0.0"
  31. UP=0
  32. HSTS="8.8.8.8 8.8.4.4"
  33. chk=0
  34. dn=0
  35.  
  36. DNSCHK(){
  37.     cnt=0
  38.     chk=0
  39.     while [ $cnt -lt $TRIES ]; do
  40.          if /bin/ping -c 1 -W 5 -w 5 $@  >/dev/null; then
  41.             chk=1
  42.             break
  43.         else
  44.             let cnt=cnt+1
  45.         fi
  46.     done
  47. }
  48.  
  49. PINGCHK(){
  50.     chk=0
  51.     if /bin/ping -c 1 -W 5 -w 5 8.8.8.8  >/dev/null; then
  52.        chk=1
  53.     fi
  54. }
  55.  
  56. GETGATEWAY() {
  57.     GATEWAY=`nvram get wan_gateway`
  58.     if [ -z "$GATEWAY" ]; then
  59.         GATEWAY="0.0.0.0"
  60.     fi
  61.     if [ $GATEWAY != "0.0.0.0" ]; then
  62.         HSTS="$GATEWAY 8.8.8.8 8.8.4.4"
  63.     else
  64.     HSTS="8.8.8.8 8.8.4.4"
  65.     fi
  66. }
  67.  
  68. while [ true ]; do
  69.  
  70.     sleep 15
  71.    
  72.     if [ $notice -lt 2 ]; then
  73.             let notice=notice+1
  74.         logger -p NOTICE -t CHECKWAN "CHECKWAN has initialized."        
  75.     fi
  76.    
  77.     dn=0
  78.     GETGATEWAY
  79.    
  80.     while [ $dn -lt $NFAIL ]; do
  81.         for h in $HSTS; do
  82.             UP=0;
  83.             DNSCHK $h
  84.             if [ $chk -gt 0 ]; then
  85.                 UP=1
  86.                 break
  87.             else
  88.                 logger -p NOTICE -t CHECKWAN "WAN Ping check failed on host $h"
  89.             fi
  90.         done
  91.  
  92.         if [ $UP -gt 0 ]; then
  93.             dn=0
  94.             sleep $TCHECK
  95.         else
  96.             let dn=dn+1
  97.             logger -p NOTICE -t CHECKWAN "WAN Check Failure # $dn"
  98.             sleep $TFAIL
  99.         fi
  100.     done
  101.    
  102.     logger -p NOTICE -t CHECKWAN "Releasing DHCP for forced renewal"
  103.    
  104.     #reset chk for PING check, JUST IN CASE!!
  105.     chk=0
  106.    
  107.     #reset DHCP by using Kill command first
  108.     #-----------------------------
  109.     killall -SIGUSR1 udhcpc
  110.     #-----------------------------
  111.    
  112.     logger -p NOTICE -t CHECKWAN "Attemping to renew the DHCP."
  113.  
  114.         #grab new IP address, try restarting the wan
  115.         service restart_wan
  116.     #sleep 5
  117.  
  118.     #do a ping check until WAN is back up
  119.     while [ $chk -lt 1 ]; do
  120.         sleep 15
  121.         PINGCHK
  122.     done
  123.  
  124.     logger -p NOTICE -t CHECKWAN "WAN DHCP has been renewed."
  125.  
  126. done
  127.  
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement