Created
October 25, 2023 08:45
-
-
Save amurgit/b8382eaabf737cbfbf047fbef7897272 to your computer and use it in GitHub Desktop.
Add client name to "wg show" command
This file contains hidden or 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 | |
# This script adds client name to "wg show" output | |
# Create tmp file | |
tmp_file=$(mktemp tmp-wg-out-XXXXXX.log) | |
# Get colored output from original "wg show" command into file | |
script --return --quiet --log-out "/dev/null" --command "wg show" --echo never >$tmp_file | |
# Array to store ip<->client_name asscociation | |
declare -A clients=() | |
# Traverse all *.conf files and save ip address and client name (from conf file name) | |
for conf in $(ls *.conf); do | |
ip_address="$(awk '/Address/{print $3}' $conf | awk -F "/" '{print $1}')" | |
client_name="$(basename $(echo $conf | cut -d "-" -f 3-) .conf)" | |
clients["$ip_address"]="$client_name" | |
done | |
# Make bold font | |
bold_start="\033[1m" | |
bold_end="\033[0m" | |
# Read wg output and add Client if "allowed ips" string appears | |
while read line_raw; do | |
# Remove color codes | |
line_clean=$(echo $line_raw | sed 's/\x1B\[[0-9;]\{1,\}[A-Za-z]//g') | |
# Get ip address if string has "allowed ips" or "" if does not | |
ip_address=$(echo $line_clean | awk '/allowed ips/{print $3}'|awk -F "/" '{print $1}') | |
# Print original line | |
echo $line_raw | |
# And print client name if ip_address is not empty | |
[ -n "$ip_address" ] && echo -e "${bold_start}Client${bold_end}: ${clients[$ip_address]}" | |
done<$tmp_file | |
# Remove wg output | |
rm $tmp_file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Improves wireguard wg show commands by adding client name to output