-
Notifications
You must be signed in to change notification settings - Fork 0
/
wg-report
executable file
·32 lines (29 loc) · 999 Bytes
/
wg-report
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
#!/bin/bash
### v0.1 - Report Wireguard New connections
# Vars
# Client list contains a chosen client name and its public key
_CLIENTLIST="/etc/wireguard/client-list"
_LATESTCONN=$(wg show all latest-handshakes)
_LOGFILE="/etc/wireguard/lastconn.log"
[ -f "$_LOGFILE" ] || touch $_LOGFILE
[ -f "$_CLIENTLIST" ] || { echo "No client File. Exit." && exit 1; }
# Main
while read _INTERFACE _PUBKEY _LASTCONN; do
_LASTTEN=$(date +%s "--date=-10 min")
_USER=$(grep "$_PUBKEY" "$_CLIENTLIST" |awk '{print $1}')
if [ "$_LASTCONN" -ge "$_LASTTEN" ]; then
if grep -q "$_PUBKEY" "$_LOGFILE"; then
continue
else
_REMOTEIP=$(wg show all endpoints |grep $_PUBKEY |awk -F'\t|:' '{print $2}')
telegram-send -c alarm " Wireguard login
# user: $_USER # ip: $_REMOTEIP
# timestamp: $(date '+%D %T')"
echo "$_USER $_PUBKEY $_REMOTEIP" >> "$_LOGFILE"
fi
else
if grep -q "$_PUBKEY" "$_LOGFILE"; then
sed -i "/$_USER/d" $_LOGFILE
fi
fi
done <<< $_LATESTCONN