Advertisement
Guest User

Untitled

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