Skip to content

Instantly share code, notes, and snippets.

@artizirk
Created March 18, 2021 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artizirk/0354ac1a9b0238570e660d31ce9899fb to your computer and use it in GitHub Desktop.
Save artizirk/0354ac1a9b0238570e660d31ce9899fb to your computer and use it in GitHub Desktop.
Annotate WireGuard wg command output with comments from the config file
#!/bin/bash
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi
function annotate {
declare -A PEER_COMMENTS
local last_comment='';
while read -r line; do
if [[ "$line" =~ ^#[[:space:]]*(.*) ]]; then
last_comment="${BASH_REMATCH[1]}"
elif [[ "$line" =~ ^PublicKey[[:space:]]*=[[:space:]]*(.*) ]]; then
PEER_COMMENTS[${BASH_REMATCH[1]}]="$last_comment"
fi
done < /etc/wireguard/$1.conf
while IFS='\n' read -r line; do
#if [[ "$line" =~ .*peer.*:.*(.*).* ]]; then
if [[ "$line" =~ .*peer.*:.*33m(.*)=.* ]]; then
key="${BASH_REMATCH[1]}="
echo "# ${PEER_COMMENTS[$key]}"
fi
echo "$line"
done < <(WG_COLOR_MODE=always wg show $1)
}
for iface in $(wg show interfaces); do
annotate $iface
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment