Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- LOGFILE=/home/pi/skrypty/czyjestnet.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 $(ifconfig 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 goasdsogle.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 #Turn Off Relay GPIO 17 PIN
- sleep 10s
- gpio mode 0 in #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.goasdsogle.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 #Turn Off Relay GPIO 17 PIN
- sleep 10s
- gpio mode 0 in #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
- 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""
- 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" mail@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 #Turn Off Relay GPIO 17 PIN
- sleep 5s
- gpio mode 0 in #Turn ON Relay GPIO 17 PIN
- fi
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement