Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.97 KB | None | 0 0
  1. #!/bin/sh
  2. # pings a pre-determined IP address and restarts OpenVPN if ping doesn't work
  3.  
  4. # Set outputfiles
  5. OUTPUT_FILE="/pinger/log.txt"
  6. IP_FILE="/pinger/lastip.txt"
  7.  
  8. # Ping settings. think of something when you are dealing with a spotty connection like wifi
  9. PING_IP="8.8.8.8"
  10. PING_AMOUNT="1"
  11.  
  12. # Command to determine IP address. Maybe make this a setting later
  13. IP_COMMAND="wget http://ipinfo.io/ip -qO -"
  14.  
  15. echo "Pinging $PING_IP, trying $PING_AMOUNT times"
  16. ping -c $PING_AMOUNT $PING_IP
  17.  
  18. STATUS=$?
  19.  
  20. if [ "$STATUS" = "0" ] ; then
  21.     echo "Ping was successful, internet still works, saving IP"
  22.     $IP_COMMAND > $IP_FILE
  23. else
  24.     echo "Ping failed, restarting OpenVPN"
  25.     LOG_TIME=$(date)
  26.     LAST_IP=$(cat $IP_FILE)
  27.     echo "last known IP address was $LAST_IP"
  28.     service openvpn restart
  29.     sleep 5
  30.     NEW_IP=$($IP_COMMAND)
  31.     echo "new IP is $NEW_IP"
  32.     echo -e "$LOG_TIME\tRestarted OpenVPN. Old IP: $LAST_IP\t\tNew IP:$NEW_IP" >> $OUTPUT_FILE
  33.     echo $NEW_IP > $IP_FILE
  34. fi
  35.  
  36. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement