-
Notifications
You must be signed in to change notification settings - Fork 7
/
getnetworkmembers
executable file
·162 lines (144 loc) · 8.34 KB
/
getnetworkmembers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/bin/bash
# This script queries a specified Zerotier account and output all member names to a file suitable for /etc/hosts or an alternative hostfile for DNSMASQ.
# For usage and prerequisites, se below or run the script with the --help option.
# CAUTION: curl and lq need to be installed.
# version 0.9
# by Kim Tholstorf ([email protected])
# https://github.com/KimTholstorf/zerotier-scripts/
# (c)2020 Kim Tholstorf, Some rights reserved
function usage {
cat <<EOFUSAGE
This script queries a specified Zerotier account and output all member names to a file suitable for /etc/hosts or an alternative hostfile for DNSMASQ.
USAGE: $0 --api=<32charalphanum> --output=<path/to/output/file> [ <other options: see below> ]
OPTIONS:
-a=, --api=
(32 digit alphanumeric key) Specifies the ZeroTier API Token (account) to query the API for netorks and members.
Default behavior is to query all the networks the Token owner are member off. This behavior can be changed
to only query a single specific Network ID with the --network argument.
-u=, --url=
(HTTPS) OPTIONAL. URL to a standalone ZeroTier network controller API (Moon).
Default value is https://my.zerotier.com/api as this is the public network controller default configured in every ZeroTier client.
This argument is only for those who run a standalone ZeroTier network controller.
-n=, --network=
(16 digit alphanumeric key) OPTIONAL. Default behavior is to query all available networks.
If a Network ID is specifed only this network queried.
-d=, --tld=
(string) OPTIONAL. Top Level Domain to append the networkname in the outputfile - e.g sdn, zero, net, etc...
Using the output file for DNS resolving will result in the members having a FQDN of member-shortname.networkname.tld
i.e specifying sdn will give the member fileserver in a network named home the FQDN of fileserver.home.sdn.
Default value is ZT resulting in a FQDN of member-shortname.networkname.zt
NOTE: Do not use a leading . only letters - i.e ZT and not .ZT
-o=, --output=
(string, filepath) Path to outputfile e.g /etc/dnsmasq.zerotier.
NOTE: The file will be overwritten at each run
NOTE: Be sure to run ths script with sudo if you are writing to a file outside your oen HOMEDIR.
-s=, --silent=
(TRUE | FALSE) OPTIONAL. Default value is FALSE and output status to to console.
NOTE: --silent can't be combined with --verbose. It's either or.
-v=, --verbose=
(TRUE | FALSE) OPTIONAL. Default value is FALSE and only output status to to console.
NOTE: --verbose can't be combined with --silent. It's either or.
PREREQUISITES:
APPLICATIONS: curl , jq
EOFUSAGE
} #end usage()
#builtin default values. Do not change these.
APIKEY=
NETWORKID=all
OUTPUTFILE=$PWD/getnetworkmembers.output
TLD=zt
APIURL="https://my.zerotier.com/api"
SILENT=false
VERBOSE=false
NETWORKLENGHT=0
args=()
while (( $# > 0 )); do
arg="$1"
arg_key="${arg%%=*}"
arg_data="${arg#*=}"
case $arg_key in
--help|-h) usage; exit 0 ;;
--api|-a) APIKEY=${arg_data} ;;
--url|-u) APIURL=${arg_data} ;;
--network|-n) NETWORKID=${arg_data} ;;
--tld|-d) TLD=${arg_data} ;;
--output|-o) OUTPUTFILE="${arg_data}" ;;
--silent|-s) SILENT="${arg_data}" ;;
--verbose|-v) VERBOSE="${arg_data}" ;;
esac
shift
done
#set -- "${args[@]}"
#Uncomment and change any of these variables below to overule args
#APIKEY="32xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
#NETWORKID="16xxxxxxxxxxxxxx"
#OUTPUTFILE="/etc/dnsmasq.zerotier"
#TLD=zt
#SILENT=false
#VERBOSE=false
#Uncomment and change any of these variables above to overule args
TLD=`echo $TLD | awk '{print tolower($0)}'`
SILENT=`echo $SILENT | awk '{print tolower($0)}'`
VERBOSE=`echo $VERBOSE | awk '{print tolower($0)}'`
#check if arguments are set or dependencies are installed
[[ ! -x "$(command -v jq)" ]] && { echo "Error: jq JSON processor is not installed or in PATH. See https://stedolan.github.io/jq/download/"; exit 1; }
[[ ! -x "$(command -v curl)" ]] && { echo "Error: curl commandline tool is not installed or in PATH"; exit 1; }
[[ -z "$APIKEY" ]] && { echo "Error: API KEY must specified."; usage; exit 1; }
[[ -z "$OUTPUTFILE" ]] && { echo "Error: OUTPUTFILE not specified."; usage; exit 1; }
[[ ! $(echo $APIKEY | awk '{print length}') == 32 ]] && { echo "Syntax Error: API Token can only be a 32-digit alphenumeric value"; exit 1; }
[[ ! $NETWORKID == all && ! $(echo $NETWORKID | awk '{print length}') == 16 ]] && { echo "Syntax Error: Network ID can only be a 16-digit alphenumeric value"; exit 1; }
# end argument check
echo "#Generated at $(date)" > $OUTPUTFILE #create outputfile
if [ $NETWORKID == all ]; then #if/else to either go through all Networks (default) or just the specified Network ID
NETWORKS=`curl -s -H "Authorization: bearer $APIKEY" $APIURL/network`
NETWORKLENGHT=`echo $NETWORKS | jq '. | length'`
echo "#Found $NETWORKLENGHT networks" >> $OUTPUTFILE
NETWORKLENGHT=$(($NETWORKLENGHT - 1))
for n in $( seq 0 $NETWORKLENGHT )
do
#Loooing through the array of networks in the NETWORKS data.
NETWORKID=`echo $NETWORKS | jq -r '.['$n'].id'`
DOMAIN=`echo $NETWORKS | jq -r '.['$n'].config.name'`.$TLD #networkname.tld
JSON=`curl -s -H "Authorization: bearer $APIKEY" $APIURL/network/$NETWORKID/member`
ARRAYLENGHT=`echo $JSON | jq '. | length'`
echo "#NetworkID $NETWORKID have $ARRAYLENGHT members" >> $OUTPUTFILE
ARRAYLENGHT=$(($ARRAYLENGHT - 1))
for i in $( seq 0 $ARRAYLENGHT )
do
#Loooing through the array of names in the JSON data. At each loop a line will be appended to the outputfile.
#If member shortname is blank use member ID as name
TEST=`echo $JSON | jq -r '.[0].name'`
if [ -z $TEST ]; then
echo $JSON | jq -r '.['$i'].config.ipAssignments[] + "#####" + .['$i'].config.address + "." + "'$DOMAIN'" | ascii_downcase' | sed 's/ /-/g' | sed 's/#/ /g' >> $OUTPUTFILE
else
echo $JSON | jq -r '.['$i'].config.ipAssignments[] + "#####" + .['$i'].name + "." + "'$DOMAIN'" | ascii_downcase' | sed 's/ /-/g' | sed 's/#/ /g' >> $OUTPUTFILE
fi
done #end ARRYLENGHT members
NETWORKID=all #reset NETOWRKID variable to later dictate output
done #end NETWORKLENGHT networks
else
DOMAIN=`curl -s -H "Authorization: bearer $APIKEY" $APIURL/network/$NETWORKID | jq -r '.config.name'`.$TLD #networkname.tld
JSON=`curl -s -H "Authorization: bearer $APIKEY" $APIURL/network/$NETWORKID/member`
ARRAYLENGHT=`echo $JSON | jq '. | length'`
echo "#NOTE: --network argument was specified. Only $NETWORKID will be written to this file" >> $OUTPUTFILE
echo "#NetworkID is $NETWORKID and have $ARRAYLENGHT members" >> $OUTPUTFILE
ARRAYLENGHT=$(($ARRAYLENGHT - 1))
for i in $( seq 0 $ARRAYLENGHT )
do
#Loooing through the array of names in the JSON result. At each loop a line will be appended to the outputfile.
#If member shortname is blank use member ID as name
TEST=`echo $JSON | jq -r '.[0].name'`
if [ -z $TEST ]; then
echo $JSON | jq -r '.['$i'].config.ipAssignments[] + "#####" + .['$i'].config.address + "." + "'$DOMAIN'" | ascii_downcase' | sed 's/ /-/g' | sed 's/#/ /g' >> $OUTPUTFILE
else
echo $JSON | jq -r '.['$i'].config.ipAssignments[] + "#####" + .['$i'].name + "." + "'$DOMAIN'" | ascii_downcase' | sed 's/ /-/g' | sed 's/#/ /g' >> $OUTPUTFILE
fi
done #end arraylenght network id
fi
# dictate output at end of script (normal (default), silent, verbose or a combination)
[[ $SILENT == true && $VERBOSE == true ]] && { cat $OUTPUTFILE; exit 0; }
[[ $SILENT == true ]] && { exit 0; }
[[ "$NETWORKID" != "all" ]] && { echo "NOTE: --network argument was specified. Only $NETWORKID was written to $OUTPUTFILE"; }
echo "Found $(($NETWORKLENGHT + 1)) network(s)"
echo "Output created in $OUTPUTFILE"
[[ $VERBOSE == true ]] && { cat $OUTPUTFILE; exit 0; }