Created
March 18, 2021 15:05
-
-
Save artizirk/0354ac1a9b0238570e660d31ce9899fb to your computer and use it in GitHub Desktop.
Annotate WireGuard wg command output with comments from the config file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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