wagon/back/lib/wg_user_list

33 lines
1.1 KiB
Plaintext
Raw Normal View History

2022-08-29 01:04:37 -06:00
#!/bin/bash
# FILE: wgapi:back/lib/wg/user/list
# DESCRIPTION: List all devices from all users
# USAGE: list json|tsv
2022-09-06 20:57:41 -06:00
# ERRORS:
# 3: bad usage
# 4: nslookup failed
# 5: Config not found
# 6: wg failed
2022-08-29 01:04:37 -06:00
2022-09-06 20:57:41 -06:00
CONFIG_FILE='/etc/wgapi/config'
[ ${#} -eq 1 ] || exit 3
[ -f "${CONFIG_FILE}" ] || exit 5
source "${CONFIG_FILE}"
2022-08-29 01:04:37 -06:00
#/usr/bin/wg show ${TLD} allowed-ips
/usr/bin/wg show "${TLD}" allowed-ips \
2022-09-08 21:15:30 -06:00
| while IFS=$' ' read -r pubkey ipv4 ipv6; do
2022-08-29 01:04:37 -06:00
ipv4="${ipv4%%/*}"
ipv6="${ipv6%%/*}"
2022-09-08 21:15:30 -06:00
domain="$("${LIB_DIR}/ns_lookup_rdns" "${ipv4}")" || exit 4
username="$(<<<"${domain}" cut -d'.' -f2)"
hostname="$(<<<"${domain}" cut -d'.' -f1)"
2022-08-29 01:04:37 -06:00
if [ "${2}" == 'json' ]; then
#TODO Remove keys with empty ("") values
2022-09-08 21:15:30 -06:00
printf '{"domain":"%s.%s.%s","ipv4":"%s","ipv6":"%s","pubkey":"%s}"},' \
"${hostname}" "${username}" "${TLD}" "${ipv4}" "${ipv6}" "${pubkey}"
2022-08-29 01:04:37 -06:00
elif [ "${2}" == 'tsv' ]; then
2022-09-08 21:15:30 -06:00
printf '%s.%s.%s\t%s\t%s\t%s\n' \
"${hostname}" "${username}" "${TLD}" "${ipv4}" "${ipv6}" "${pubkey}"
2022-08-29 01:04:37 -06:00
fi
done | sed 's/,$//' # Remove trailing comma from json