-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwg-json-zbx
More file actions
65 lines (61 loc) · 2.86 KB
/
Copy pathwg-json-zbx
File metadata and controls
65 lines (61 loc) · 2.86 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
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (C) 2015-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
#
# Modified by alkalim (https://github.com/alkalim) for use by monitoring
# * show peer names from wireguard-install.sh (https://github.com/Nyr/wireguard-install)
# * remove sensitive information (private keys)
# * change peer format for easier processing
#
wgcnf=/etc/wireguard/wg0.conf
exec < <(exec sudo wg show all dump)
peerno=1
printf '{'
while read -r -d $'\t' device; do
if [[ $device != "$last_device" ]]; then
[[ -z $last_device ]] && printf '\n' || printf '%s,\n' "$end"
last_device="$device"
read -r private_key public_key listen_port fwmark
printf '\t"%s": {' "$device"
delim=$'\n'
[[ $public_key == "(none)" ]] || { printf '%s\t\t"publicKey": "%s"' "$delim" "$public_key"; delim=$',\n'; }
[[ $listen_port == "0" ]] || { printf '%s\t\t"listenPort": %u' "$delim" $(( $listen_port )); delim=$',\n'; }
[[ $fwmark == "off" ]] || { printf '%s\t\t"fwmark": %u' "$delim" $(( $fwmark )); delim=$',\n'; }
printf '%s\t\t"peers": [' "$delim"; end=$'\n\t\t]\n\t}'
delim=$'\n'
else
read -r public_key preshared_key endpoint allowed_ips latest_handshake transfer_rx transfer_tx persistent_keepalive
# printf '%s\t\t\t"%s": {' "$delim" "$public_key"
printf '%s\t\t\t{' "$delim"
delim=$'\n'
[[ $public_key == "(none)" ]] || { printf '%s\t\t\t\t"publicKey": "%s"' "$delim" "$public_key"; delim=$',\n'; }
{ printf '%s\t\t\t\t"peerNo": "%s"' "$delim" "$peerno"; delim=$',\n'; peerno=$((peerno+1)); }
[[ $endpoint == "(none)" ]] || { printf '%s\t\t\t\t"endpoint": "%s"' "$delim" "$endpoint"; delim=$',\n'; }
[[ $latest_handshake == "0" ]] || { printf '%s\t\t\t\t"latestHandshake": %u' "$delim" $(( $latest_handshake )); delim=$',\n'; }
[[ $transfer_rx == "0" ]] || { printf '%s\t\t\t\t"transferRx": %u' "$delim" $(( $transfer_rx )); delim=$',\n'; }
[[ $transfer_tx == "0" ]] || { printf '%s\t\t\t\t"transferTx": %u' "$delim" $(( $transfer_tx )); delim=$',\n'; }
[[ $persistent_keepalive == "off" ]] || { printf '%s\t\t\t\t"persistentKeepalive": %u' "$delim" $(( $persistent_keepalive )); delim=$',\n'; }
peer=$(awk '/PublicKey\s*=\s*(.*)$/{key=$3}/END_PEER/{print key,$3}' $wgcnf|grep $public_key|cut -d' ' -f2)
[[ -z $peer ]] || { printf '%s\t\t\t\t"peerName": "%s"' "$delim" "$peer"; delim=$',\n'; }
# add interface name (ifname) so consumers can identify which wg device the peer belongs to
{ printf '%s\t\t\t\t"ifname": "%s"' "$delim" "$device"; delim=$',\n'; }
printf '%s\t\t\t\t"allowedIps": [' "$delim"
delim=$'\n'
if [[ $allowed_ips != "(none)" ]]; then
old_ifs="$IFS"
IFS=,
for ip in $allowed_ips; do
printf '%s\t\t\t\t\t"%s"' "$delim" "$ip"
delim=$',\n'
done
IFS="$old_ifs"
delim=$'\n'
fi
printf '%s\t\t\t\t]' "$delim"
printf '\n\t\t\t}'
delim=$',\n'
fi
done
printf '%s\n' "$end"
printf '}\n'