3
0

run 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/bin/sh
  2. # How often to test, seconds
  3. ping_time=67
  4. # "One ping, must have reply in 1 sec"
  5. ping_opts="-c1 -W1 -w1"
  6. # If ping failed, how soon to retry
  7. retry_time=5
  8. # Reinit after this many consecutive ping error
  9. max_fail=5
  10. # Interface whose DHCP data to use
  11. if=${PWD##*/dhcp_}
  12. if=${if%%_pinger}
  13. msg() {
  14. echo "`date '+%Y-%m-%d %H:%M:%S'` $*" >>"$0.log"
  15. }
  16. if test -f "$0.log"; then
  17. tail -999 "$0.log" >"$0.log.new"
  18. mv "$0.log.new" "$0.log"
  19. fi
  20. test -f "../dhcp_$if/env.out" || exec env - sleep "$ping_time"
  21. . "../dhcp_$if/env.out"
  22. test x"$router" != x"" || exec env - sleep "$ping_time"
  23. #msg "Pinging $router"
  24. failcnt=0
  25. while true; do
  26. ping $ping_opts "$router" && exec env - sleep "$ping_time"
  27. failcnt=$((failcnt+1))
  28. msg "Failed to ping $router, fail count:$failcnt"
  29. test $failcnt -ge $max_fail && break
  30. env - sleep "$retry_time"
  31. done
  32. test -d "../dhcp_$if" && {
  33. msg "Restarting dhcp_$if"
  34. svc -t "dhcp_$if"
  35. }
  36. test -d "../supplicant_$if" && {
  37. msg "Restarting supplicant_$if"
  38. svc -t "supplicant_$if"
  39. }
  40. exec env - sleep "$ping_time"