-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvpn-connected
More file actions
executable file
·52 lines (47 loc) · 1.03 KB
/
vpn-connected
File metadata and controls
executable file
·52 lines (47 loc) · 1.03 KB
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
45
46
47
48
49
50
51
52
#!/bin/bash
set -o pipefail
function wireguard_connected () {
# $(($(date +%s) - $(sudo wg show wg0 latest-handshakes | awk '{ print $2; }')))
now_="$(date +%s)"
latest="$(sudo wg show wg0 latest-handshakes 2>/dev/null | awk '{ print $2; }')"
if [[ -z "$latest" ]]; then
false
return
fi
# 5 minute timeout
if [[ "(($now_ - $latest))" -gt 300 ]]; then
false
return
fi
true
return
}
function openvpn_connected () {
while read -r line
do
if [[ "$line" == "0.0.0.0/1"*"dev tun"* ]]; then
true
return
fi
done < <(ip route)
false
return
}
function vpn_connected () {
wireguard_connected $@
}
RED='\033[0;31m'
GREEN='\033[0;32m'
NORMAL='\033[0m'
if [ "$1" != "-q" ]; then
echo -n "VPN is "
if ! vpn_connected; then
echo -ne "${RED}"
echo -n "not "
else
echo -ne "${GREEN}"
fi
echo -e "connected${NORMAL}"
fi
# Exit status of script allows this to be used elsewhere
vpn_connected