Skip to content

Instantly share code, notes, and snippets.

@Kline-
Last active August 30, 2024 13:53
Show Gist options
  • Save Kline-/703a6359a0d26e8babb487379a18a878 to your computer and use it in GitHub Desktop.
Save Kline-/703a6359a0d26e8babb487379a18a878 to your computer and use it in GitHub Desktop.
A wrapper for the wireguard command to display the hostname of endpoints from local DNS.
#!/bin/bash
WG=/usr/bin/wg
WGCONF=/etc/wireguard/wg0.conf # The line prior to AllowedIPs in each [Peer] should be # Name = Xyz
OUTPUT=$(script --flush --quiet /dev/null --command $WG)
if [[ $# -gt 0 ]]; then
$WG $@
exit
fi
if [[ ! -z $OUTPUT ]]; then
while read LINE; do
if [[ $LINE =~ ^.*(peer|interface) ]]; then
echo "$LINE"
elif [[ $LINE =~ ^.*allowed.* ]]; then
IPV4=$(printf "%q" "$LINE" | cut -d' ' -f3 | cut -d/ -f1 | cut -d\\ -f1)
#HOST=$(host "$IPV4" | cut -d' ' -f5 | cut -d. -f1)
HOST=$(grep -B1 "$IPV4" "$WGCONF" | grep 'Name' | cut -d' ' -f4)
echo " $LINE"
echo " friendly name: $HOST"
else
echo " $LINE"
fi
done <<< "$OUTPUT"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment