SHARE
TWEET

ConnectionMonitor.sh

JWLLEWELYN Dec 2nd, 2017 (edited) 304 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. # Autor: John Llewelyn
  4. # FB: fb.com/johnwilliam.llewelyn
  5. # Twitter: twitter.com/JWLLEWELYN
  6. # TLF: +584-1491-011-15
  7. # Its use is free.
  8. # Description: Connection Monitor for ADSL modem.
  9. # Requirements:
  10. # Copy this code or save to /home/administrator/ConnectionMonitor.sh
  11. # It requires the installed packages fping beep and cron
  12. # Comment the blacklist pcspkr snd-pcsp in /etc/modprobe.d/blacklist.conf
  13. # Give execute permissions: chmod +x /home/administrator/ConnectionMonitor.sh
  14. # Add this line in crontab -e with root user
  15. # @reboot sleep 120 && /home/administrator/MonitorDeConexion.sh
  16.  
  17. #################################################################################
  18. # SETTINGS
  19. TEST="8.8.8.8"       # TEST PING
  20. ADAPTER1="enp4s0"    # EXTERNAL ETHERNET ADAPTER
  21.  
  22. # Report
  23. LOGFILE=/home/administrator/Documentos/ReportInternet.log
  24.  
  25. # Messages
  26. MESSAGE1="Restoring Connectivity..."
  27. MESSAGE2="Wait a moment please..."
  28. MESSAGE3="No Internet connectivity."
  29. MESSAGE4="Yes, there is Internet connectivity."
  30. #################################################################################
  31.  
  32. # Time and Date
  33. TODAY=$(date "+%r %d-%m-%Y")
  34.  
  35. # Show IP Public Address
  36. IPv4ExternalAddr1=$(ip addr list $ADAPTER1 |grep "inet " |cut -d' ' -f6|cut -d/ -f1)
  37. IPv6ExternalAddr1=$(ip addr list $ADAPTER1 |grep "inet6 " |cut -d' ' -f6|cut -d/ -f1)
  38.  
  39. # Alarm
  40. alarm() {
  41.     beep -f 1500 -l 200;beep -f 1550 -l 200;beep -f 1500 -l 200;beep -f 1550 -l 200;beep -f 1500 -l 200;beep -f 1550 -l 200;beep -f 1500 -l 200;beep -f 1550$
  42. }
  43.  
  44. # Restoring Connectivity
  45. resolve() {
  46.     clear
  47.     echo "$MESSAGE1"
  48.     sudo ifconfig $ADAPTER1 up;sudo dhclient -r $ADAPTER1;sleep 5;sudo dhclient $ADAPTER1
  49. #   killall -SIGUSR2 udhcpc;sleep 5;killall -SIGUSR1 udhcpc
  50.     echo "$MESSAGE2"
  51.     sleep 120
  52. }
  53.  
  54. # Execution of work
  55. while true; do
  56.     if [[ "$(fping -I $ADAPTER1 $TEST | grep 'unreachable' )" != "" ]]; then
  57.         alarm
  58.         clear
  59.         echo "================================================================================" >> ${LOGFILE}
  60.         echo "$MESSAGE3 - $TODAY"                                                               >> ${LOGFILE}
  61.         echo "$MESSAGE3 - $TODAY"
  62.         echo "================================================================================" >> ${LOGFILE}
  63.         sleep 10
  64.         resolve
  65.     else
  66.         clear
  67.         echo "================================================================================"   >> ${LOGFILE}
  68.         echo "$MESSAGE4 - $TODAY - IPv4 Addr: $IPv4ExternalAddr1 - IPv6 Addr: $IPv6ExternalAddr1" >> ${LOGFILE}
  69.         echo "$MESSAGE4 - $TODAY - IPv4 Addr: $IPv4ExternalAddr1 - IPv6 Addr: $IPv6ExternalAddr1"
  70.         echo "================================================================================"   >> ${LOGFILE}
  71.         sleep 120
  72.     fi
  73. done
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top