Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.54 KB | None | 0 0
  1. #!/bin/bash
  2. LOGFILE=/home/pi/skrypty/czyjestnet.log
  3. maxPloss=10
  4.  
  5. if ifconfig eth0 | grep -q "inet addr:" ;
  6. then
  7. #echo "$(date "+%m %d %Y %T") : Local Ethernet OK" >> $LOGFILE
  8. echo "Local Ethernet OK. Local IP $(ifconfig eth0 | grep -q "inet addr:")"
  9. echo "Pinging google.com to check DNS..."
  10. # Initialize to a value that would force a restart
  11. # (just in case ping gives an error and ploss doesn't get set)
  12. if ping -c1 goasdsogle.com 2>&1 | grep unknown ; then
  13. echo "Unknown host when ping google.com. Restart router..."
  14. echo "Unknown host when ping google.com. Restart router..." >> $LOGFILE
  15. gpio mode 0 out #Turn Off Relay GPIO 17 PIN
  16. sleep 10s
  17. gpio mode 0 in #Turn ON Relay GPIO 17 PIN
  18. #echo "$(date "+%m %d %Y %T") : Waitimg 1m and restart eth0 interface ifdown ifup..." >> $LOGFILE
  19. #sleep 1m
  20. #ifdown eth0
  21. #ifup eth0
  22. else
  23. ploss=10
  24. echo "Set ploss to 10."
  25. echo "Send 10 Ping to google.com to check package loss..."
  26. # now ping google for 10 seconds and count packet loss
  27. ploss=$(ping -q -w10 www.goasdsogle.com | grep -o "[0-9]*%" | tr -d %) > /dev/null 2>&1
  28. echo ""$ploss"% pocket loss after 10 ping google.com"
  29. if [ "$ploss" -gt "$maxPloss" ]; then
  30. echo "$(date "+%m %d %Y %T") : Packet loss from google.com ($ploss%) exceeded $maxPloss, restarting Router GPIO17..." >> $LOGFILE
  31. #Restart Router...
  32. gpio mode 0 out #Turn Off Relay GPIO 17 PIN
  33. sleep 10s
  34. gpio mode 0 in #Turn ON Relay GPIO 17 PIN
  35. #echo "$(date "+%m %d %Y %T") : Waitimg 1m and restart eth0 interface ifdown ifup..." >> $LOGFILE
  36. #sleep 1m
  37. #ifdown eth0
  38. #ifup eth0
  39. echo "$(date "+%m %d %Y %T") : System Online. Google.com package loss ($ploss%)" >> $LOGFILE
  40. ip=$(wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//')
  41. echo "IP from DDNS.ORG "$ip""
  42.  
  43. fi
  44. fi
  45. else
  46. echo "$(date "+%m %d %Y %T") : Ethernet connection down! Attempting reconnection." >> $LOGFILE
  47. ifdown eth0
  48. ifup --force eth0
  49. sleep 5s
  50. if ifconfig eth0 | grep -q "inet addr:" ; then
  51. STATE=$(ifconfig eth0 | grep "inet addr:")
  52. echo "$(date "+%m %d %Y %T") : Network connection reset. Current state is" $STATE >> $LOGFILE
  53. echo "$(date "+%m %d %Y %T") : Restarting OpenVPN..." >> $LOGFILE
  54. service openvpn restart
  55. echo "Wykryto zawieszenie sie polaczenia lokalnego. Zrestartowanie eth0 oraz OpenVPN" | mail -s "Zrestartowanoa eth0" mail@gmail.com
  56. else
  57. echo "$(date "+%m %d %Y %T") : Failed to reset ethernet connection. Going to restart Router GPIO17..." >> $LOGFILE
  58. #Restart Router...
  59. gpio mode 0 out #Turn Off Relay GPIO 17 PIN
  60. sleep 5s
  61. gpio mode 0 in #Turn ON Relay GPIO 17 PIN
  62. fi
  63. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement