forked from HKEOS/Ghostbusters-Testnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peerCleanup.sh
87 lines (72 loc) · 1.75 KB
/
peerCleanup.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
sudo wg show ghostbusters | grep -A 2 "peer: " > peers.temp
if ! which fping > /dev/null; then
echo -e "fping not found! Install? (y/n) \c"
read
if "$REPLY" = "y"; then
sudo apt install fping
fi
fi
cmd="$1"
strict="$2"
if [[ $cmd == "remove" ]]; then
remove=true;
if [[ $strict == "strict" ]]; then
strictmode=true;
else
strictmode=false;
fi
else
remove=false;
fi
if [[ $strictmode == true ]]; then
echo "Any offline hosts will be removed!!";
read -n 1 -s -r -p "Press any key to continue";
echo -e "\n";
else
if [[ $remove == true ]]; then
echo "Offline hosts will be removed! Online hosts with wireguard off will be preserved!";
read -n 1 -s -r -p "Press any key to continue";
echo -e "\n";
fi
fi
while read line; do
if [[ $line == "peer:"* ]]; then
id=$(echo "$line" | cut -f2 -d" ");
fi
if [[ $line == "allowed ips:"* ]]; then
ip=$(echo "$line" | cut -f3 -d" " | cut -f1 -d"/");
fi
if [[ $line == "endpoint:"* ]]; then
endpoint=$(echo "$line" | cut -f2 -d" " | cut -f1 -d":");
fi
if [[ $line == "--" ]]; then
echo "Testing connection for $id on $ip";
fping -c1 -t1000 $ip 2>/dev/null 1>/dev/null
if [ "$?" = 0 ]; then
echo "Host found"
else
echo "WG not found"
if [[ $strictmode == true ]]; then
sudo wg set ghostbusters peer "$id" remove
else
echo "Retrying on $endpoint"
fping -c1 -t1000 $endpoint 2>/dev/null 1>/dev/null
if [ "$?" = 0 ]; then
echo "Public host found, wireguard is down!"
else
echo "Server is offline"
if [[ $remove == true ]]; then
sudo wg set ghostbusters peer "$id" remove
fi
fi
fi
fi
ip="";
id="";
endpoint="";
fi
done < peers.temp
sudo wg-quick save ghostbusters
rm peers.temp
sudo wg show ghostbusters