wagon/back/lib/wg_user_list

34 lines
1.0 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 \
| while read pubkey ipv4 ipv6; do
ipv4="${ipv4%%/*}"
ipv6="${ipv6%%/*}"
2022-09-06 20:57:41 -06:00
domain="$(../../ns/lookup/rdns ${ipv4})"
if [ ${?} -ne 0 ]
then exit 4
fi
username="$(printf ${domain} | cut -d'.' -f2)"
hostname="$(printf ${domain} | cut -d'.' -f1)"
2022-08-29 01:04:37 -06:00
if [ "${2}" == 'json' ]; then
#TODO Remove keys with empty ("") values
printf "{\"hostname\":\"${hostname}.${username}.${TLD}\",\"ipv4\":\"${ipv4}\",\"ipv6\":\"${ipv6}\",\"pubkey\":\"${pubkey}\"},"
elif [ "${2}" == 'tsv' ]; then
printf "${hostname}.${username}.${TLD}\t${ipv4}\t${ipv6}\t${pubkey}\n"
fi
done | sed 's/,$//' # Remove trailing comma from json