Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- LOGFILE=/home/pi/bin/log/network-monitor.log
- maxPloss=10
- if ifconfig eth0 | grep -q "inet addr:" ;
- then
- #echo "$(date "+%m %d %Y %T") : Local Ethernet OK" >> $LOGFILE
- echo "Local Ethernet OK. Local IP $(ifconfgi eth0 | grep -q "inet addr:")"
- echo "Pinging google.com to check DNS..."
- # Initialize to a value that would force a restart
- # (just in case ping gives an error and ploss doesn't get set)
- if ping -c1 google.com 2>&1 | grep unknown ; then
- echo "Unknown host when ping google.com. Restart router..."
- echo "Unknown host when ping google.com. Restart router..." >> $LOGFILE
- gpio mode 0 out
- gpio write 0 0 #Turn Off Relay GPIO 17 PIN
- sleep 10s
- gpio write 0 1 #Turn ON Relay GPIO 17 PIN
- #echo "$(date "+%m %d %Y %T") : Waitimg 1m and restart eth0 interface ifdown ifup..." >> $LOGFILE
- #sleep 1m
- #ifdown eth0
- #ifup eth0
- else
- ploss=10
- echo "Set ploss to 10."
- echo "Send 10 Ping to google.com to check package loss..."
- # now ping google for 10 seconds and count packet loss
- ploss=$(ping -q -w10 www.google.com | grep -o "[0-9]*%" | tr -d %) > /dev/null 2>&1
- echo ""$ploss"% pocket loss after 10 ping google.com"
- if [ "$ploss" -gt "$maxPloss" ]; then
- echo "$(date "+%m %d %Y %T") : Packet loss from google.com ($ploss%) exceeded $maxPloss, restarting Router GPIO17..." >> $LOGFILE
- #Restart Router...
- gpio mode 0 out
- gpio write 0 0 #Turn Off Relay GPIO 17 PIN
- sleep 10s
- gpio write 0 1 #Turn ON Relay GPIO 17 PIN
- #echo "$(date "+%m %d %Y %T") : Waitimg 1m and restart eth0 interface ifdown ifup..." >> $LOGFILE
- #sleep 1m
- #ifdown eth0
- #ifup eth0
- else
- echo "$(date "+%m %d %Y %T") : System Online. Google.com package loss ($ploss%)" >> $LOGFILE
- ip=$(wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//')
- echo "IP from DDNS.ORG "$ip""
- ping=$(sudo /usr/local/bin/noip2 -S 2>&1 | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}")
- echo "IP from last noip2 -S "$ping""
- if [ ${ip/:''} == ${ping/:''} ]
- then
- echo "IP "$ip" is updated in NOIP"
- #echo "$(date "+%m %d %Y %T") : External IP and NOIP are the same - "$ip"" >> $LOGFILE
- else
- echo "External IP: "$ip"" | echo "Last noip2 IP: "$ping"" | echo "Restarting noip2..."
- echo "$(date "+%m %d %Y %T") : External IP "$ip" - NOIP2 "$ping". Restarting noip2... " >> $LOGFILE
- service noip restart
- ping2=$(sudo /usr/local/bin/noip2 -S 2>&1 | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}")
- echo "$(date "+%m %d %Y %T") : NOIP2 after restart: "$ping2"" >> $LOGFILE
- echo "Zmienil sie numer IP Raspberry PI na "$ip"" | mail -s "Raspberry PI - Zmiana IP na "$ip"" solotgz@gmail.com
- fi
- fi
- fi
- else
- echo "$(date "+%m %d %Y %T") : Ethernet connection down! Attempting reconnection." >> $LOGFILE
- ifdown eth0
- ifup --force eth0
- sleep 5s
- if ifconfig eth0 | grep -q "inet addr:" ; then
- STATE=$(ifconfig eth0 | grep "inet addr:")
- echo "$(date "+%m %d %Y %T") : Network connection reset. Current state is" $STATE >> $LOGFILE
- echo "$(date "+%m %d %Y %T") : Restarting OpenVPN..." >> $LOGFILE
- service openvpn restart
- echo "Wykryto zawieszenie sie polaczenia lokalnego. Zrestartowanie eth0 oraz OpenVPN" | mail -s "Zrestartowanoa eth0" solotgz@gmail.com
- else
- echo "$(date "+%m %d %Y %T") : Failed to reset ethernet connection. Going to restart Router GPIO17..." >> $LOGFILE
- #Restart Router...
- gpio mode 0 out
- gpio write 0 0 #Turn Off Relay GPIO 17 PIN
- sleep 5s
- gpio write 0 1 #Turn ON Relay GPIO 17 PIN
- fi
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement