Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- #set -x
- #trap read debug
- TIMEOUT=60
- TIMEOUT_DHCP=120
- INTERFACE=ens32
- LOG=/root/netcheck.log
- SCRIPT_PATH=/root
- WIREGUARD_IP="10.7.1.1"
- INTERNET_IP1="8.8.8.8"
- INTERNET_IP2="4.2.2.2"
- CHECK_INTERVAL=5 #how often to perform checks in seconds
- #NET_INT=`ip route | awk '/default/ { print $5 }'` #default gateway interface
- NET_INT=ens32
- WG_INT=wg0
- # exit if another instance of script is already running
- MY_SCRIPT_NAME=`basename "$0"`
- if pidof -o %PPID -x $MY_SCRIPT_NAME > /dev/null; then
- 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
- exit 1
- fi
- check_ip () {
- if ping -c 1 -W 1 $1 > /dev/null 2>&1
- then return
- else
- return
- fi
- }
- # 1 check wireguard connectivity
- # 2 if good, exit, if not, check internet connection
- # 3 if internet connection good, check wireguard service
- while true; do
- if ! check_ip $WIREGUARD_IP
- then
- if check_ip $INTERNET_IP1 || check_ip $INTERNET_IP2 # if internet up then check if wireguard is responsive
- then
- if ! wg | grep handshake #restart wg if not active
- then
- systemctl restart wg-quick@wg0
- echo "`date +%Y-%m-%d" "%H:%M:%S` `hostname | cut -d'.' -f1` netcheck.sh: Wireguard not connected, restarting" >> $LOG 2>&1
- fi
- else
- systemctl stop wg-quick@wg0
- dhclient -r -lf /var/lib/dhcp/dhclient.leases.$NET_INT -pf /var/run/dhclient.$NET_INT.pid $NET_INT
- dhclient -v -lf /var/lib/dhcp/dhclient.leases.$NET_INT -pf /var/run/dhclient.$NET_INT.pid $NET_INT
- echo $?
- echo "`date +%Y-%m-%d" "%H:%M:%S` `hostname | cut -d'.' -f1` netcheck.sh: DHCP renewed" >> $LOG 2>&1
- sleep 5
- systemctl start wg-quick@$WG_INT
- echo "`date +%Y-%m-%d" "%H:%M:%S` `hostname | cut -d'.' -f1` netcheck.sh: Wireguard restarted" >> $LOG 2>&1
- fi
- fi
- sleep $CHECK_INTERVAL
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement