Advertisement
gpz1100

Untitled

Apr 10th, 2025
377
0
2 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.33 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. #set -x
  4. #trap read debug
  5.  
  6. TIMEOUT=60
  7. TIMEOUT_DHCP=120
  8. INTERFACE=ens32
  9. LOG=/root/netcheck.log
  10. SCRIPT_PATH=/root
  11. WIREGUARD_IP="10.7.1.1"
  12. INTERNET_IP1="8.8.8.8"
  13. INTERNET_IP2="4.2.2.2"
  14. CHECK_INTERVAL=5        #how often to perform checks in seconds
  15. #NET_INT=`ip route | awk '/default/ { print $5 }'`    #default gateway interface
  16. NET_INT=ens32
  17. WG_INT=wg0
  18.  
  19. # exit if another instance of script is already running
  20.         MY_SCRIPT_NAME=`basename "$0"`
  21.         if pidof -o %PPID  -x $MY_SCRIPT_NAME  > /dev/null; then
  22.         echo "`date +%Y-%m-%d" "%H:%M:%S` `hostname | cut -d'.' -f1` netcheck.sh: Watchdog script already running, PID:`pidof -o %PPID  -x $MY_SCRIPT_NAME`,  exiting." >> $LOG 2>&1
  23.         exit 1
  24.         fi
  25.  
  26. check_ip () {
  27.         if ping -c 1 -W 1 $1 > /dev/null 2>&1
  28.         then return
  29.         else
  30.         return
  31.         fi
  32.  
  33. }
  34.  
  35. # 1 check wireguard connectivity
  36. # 2 if good, exit, if not, check internet connection
  37. # 3 if internet connection good, check wireguard service
  38.  
  39. while true; do
  40.  
  41.         if ! check_ip $WIREGUARD_IP
  42.                 then
  43.                 if check_ip $INTERNET_IP1 || check_ip $INTERNET_IP2 # if internet up then check if wireguard is responsive
  44.                 then
  45.                         if ! wg | grep handshake  #restart wg if not active
  46.                         then
  47.                                 systemctl restart wg-quick@wg0
  48.                                 echo "`date +%Y-%m-%d" "%H:%M:%S` `hostname | cut -d'.' -f1` netcheck.sh: Wireguard not connected, restarting" >> $LOG 2>&1
  49.                         fi
  50.                 else
  51.                         systemctl stop wg-quick@wg0
  52.                         dhclient -r -lf /var/lib/dhcp/dhclient.leases.$NET_INT -pf /var/run/dhclient.$NET_INT.pid $NET_INT
  53.                         dhclient -v -lf /var/lib/dhcp/dhclient.leases.$NET_INT -pf /var/run/dhclient.$NET_INT.pid $NET_INT
  54.                         echo $?
  55.                         echo "`date +%Y-%m-%d" "%H:%M:%S` `hostname | cut -d'.' -f1` netcheck.sh: DHCP renewed" >> $LOG 2>&1
  56.                         sleep 5
  57.                         systemctl start wg-quick@$WG_INT
  58.                         echo "`date +%Y-%m-%d" "%H:%M:%S` `hostname | cut -d'.' -f1` netcheck.sh: Wireguard restarted" >> $LOG 2>&1
  59.                 fi
  60.  
  61.         fi
  62. sleep $CHECK_INTERVAL
  63. done
  64.  
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement