-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzerotier.sh
65 lines (58 loc) · 2.31 KB
/
zerotier.sh
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
#!/usr/bin/env /bin/bash
#set -euo pipefail
###
## GLOBAL VARIABLES
###
ZEROTIER_TOKEN=${ZEROTIER_TOKEN:-''}
API_URL_PREFIX=${API_URL_PREFIX:-'https://my.zerotier.com'}
NETWORK_ID=${NETWORK_ID:-''}
PS3='[Please enter your choice]: '
options=(
"list-networks (ln): List zerotier networks" # 1
"list-members (lm): List zerotier network members" # 2
"update-member (um): Update zerotier member" # 3
"quit: Exit from this menu" # 4
)
function _switch() {
_reply="$1"
_NODE_ID="$2"
_NODE_NAME="$3"
case $_reply in
""|"ln"|"list-networks"|"1")
curl -s -H "Authorization: Bearer ${ZEROTIER_TOKEN}" -H "Content-Type: application/json" "${API_URL_PREFIX}/api/network" | jq -r '.[]| "\(.config.name)***\(.id)"' | column -t -s "***";
;;
""|"lm"|"list-members"|"2")
curl -s -H "Authorization: Bearer ${ZEROTIER_TOKEN}" -H "Content-Type: application/json" "${API_URL_PREFIX}/api/network/${NETWORK_ID}/member" | jq -r '.[]| "\(.name)***\(.config.id)***\(.config.authorized)***\(.config.ipAssignments[0])"' | column -t -s "***" | sort;
;;
""|"um"|"update-member"|"3")
[ -z ${_NODE_ID} ] && echo "Missing node id" && exit 1
[ -z ${_NODE_NAME} ] && echo "Missing node name" && exit 1
curl -s -X POST -H "Authorization: Bearer ${ZEROTIER_TOKEN}" -H "Content-Type: application/json" "${API_URL_PREFIX}/api/network/${NETWORK_ID}/member/${_NODE_ID}" -d '{"config": {"authorized": true}, "name": "'${_NODE_NAME}'"}';
exit
;;
""|"quit"|"4")
echo "Goodbye!"
exit
;;
""|"--help")
echo "Required enviroment variables:"
echo "ZEROTIER_TOKEN - set with your zerotier api token"
echo "NETWORK_ID - set with your network id"
echo "Available commands:"
printf '%s\n' "${options[@]}"
;;
*) echo "invalid option, use --help option for the commands list";;
esac
}
while true
do
# run option directly if specified in argument
[ ! -z $1 ] && _switch $@ # old method: "${options[$cmdopt-1]}"
[ ! -z $1 ] && exit 0
echo "==== ZEROTIER OPTIONS ===="
select opt in "${options[@]}"
do
_switch $REPLY
break
done
done