forked from mchsk/openwrt-lte-keep-alive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwg-keep-alive.sh
executable file
·44 lines (35 loc) · 1.37 KB
/
wg-keep-alive.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#------------------------------------------------------------------------------------
#---------------root@OpenWrt:~# cat wg-keep-alive.sh--------------------------
#------------------------------------------------------------------------------------
#!/bin/ash
# The main script file. I recommend putting into cron, e.g.:
# */2 * * * * /root/scripts/wg-keep-alive.sh
DIR=$( cd $(dirname $0) ; pwd -P )
LOG_FILE="$DIR/log-wg.txt"
OFFLINE_COUNT=$(cat $LOG_FILE | tail -4 | grep OFFLINE | wc -l)
OFFLINE_COUNT_TRESHOLD=4
SH_WG_TEST="$DIR/wg-test.sh"
SH_RESTART_INTERFACE="$DIR/wg-restart-interface.sh"
LINES_MAX=11000
LINES_MIN=6000
LINES_COUNT=$(wc -l $LOG_FILE | awk '{print $1}')
# if the log files gets huge, strip it, keep last LINES_MIN lines
if [[ "$LINES_COUNT" -ge "$LINES_MAX" ]]; then
echo "$(tail -$LINES_MIN $LOG_FILE)" > $LOG_FILE
fi
# DNS test, it's result defines the ONLINE/OFFLINE state
/bin/ash $SH_WG_TEST
if [ $? -eq 1 ]; then
echo "Ooops, Wireguard is down!"
echo "$(date) Wireguard Tunnel down > RESTARTING INTERFACE" >> $LOG_FILE
if [[ "$OFFLINE_COUNT" -ge "$OFFLINE_COUNT_TRESHOLD" ]]; then
echo ">> Checking wireguard connectivity.." >> $LOG_FILE
$SH_WG_TEST
else
echo ">> Restarting wireguard-interface.." >> $LOG_FILE
$SH_RESTART_INTERFACE
fi
else
echo "Wireguard is okay!"
echo "$(date) Wireguard aktiv" >> $LOG_FILE
fi