-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathztroutes.bash
296 lines (192 loc) · 6.06 KB
/
ztroutes.bash
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/bin/bash
source "functions.bash"
tmpRouteFile='tmp/ztroutesfile.tmp'
rm -f tmp/ztexistjson.tmp
rm -f tmp/ztexistjson2.tmp
allNets "exit 0"
#theNet=$(echo "${net}" | awk ' { print $1 } ')
del_temp
# Set array to add routes
declare -a targetArray
function get_routes() {
rm -f tmp/ztexistjson.tmp
currNet
# Default index for routes
COUNTER=0
# Get the routes for the network
x=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}" | jq -r '.routes[].target' |wc -l)
# set the number of available routes - 1
num=$(($x-1))
# Begin format for files with routes.
echo '{ "routes": [' >> tmp/ztexistjson.tmp
# Loop through the routes
until [ $COUNTER -gt $num ]
do
t=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/" | jq -r ".routes[$COUNTER].target")
v=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/" | jq -r ".routes[$COUNTER].via")
((COUNTER++))
# Formatting the route
ta='{"target": "'${t}'", "via": "'${v}'"},'
# Add route to array
targetArray+=(${ta})
# Used for printing to the screen
# The column command will replace the three underscores with spaces for
# Clean formatting
echo "${t}___${v}" >> ${tmpRouteFile}
# Write the formatted route to the temp file
echo "${ta}" >> tmp/ztexistjson.tmp
done
}
function allDone() {
read -p "Press Enter when done."
}
function menu() {
clear
echo "########################################################"
echo "${net}"
echo "########################################################"
echo "1. List routes"
echo "2. Add a route"
echo "3. Delete a route"
echo "[E]xit back to main ZT Menu"
read -p "Please select a number value next to the options. " choice
case "${choice}" in
1) # list routes
echo "Destination___Gateway" > ${tmpRouteFile}
get_routes
currNet
clear
echo ""
cat ${tmpRouteFile} | column -t -s"___"
del_temp
currNet
allDone
;;
2) # Add a route
echo "Destination___Gateway" > ${tmpRouteFile}
clear
get_routes
theNet=$(cat ${ztnetFile})
function get_dest() {
read -p "Please enter the destination network or host and netmask or 'E' to exit: " dest
}
get_dest
ip_chk "${dest}" "get_dest"
echo ""
function get_gateway() {
read -p "Please enter the host to be the gateway and netmask or 'E' to exit: " gateway
}
get_gateway
ip_chk "${gateway}" "get_gateway"
if [[ "${gateway}" == "default" ]]; then
gateway=""
fi
# Get existing routes
currRoutes=$(echo ${targetArray[*]} | sed 's/} {/},{/g')
if [[ "{target: ${dest}, via: ${gateway}}" =~ "${currRoutes}" ]]; then
echo "Route already exists."
allDone
currNet
menu
else
# Add new route to the temp file for parsing
echo '{"target": "'${dest}'", "via": "'${gateway}'"} ]}' >> tmp/ztexistjson.tmp
# Put all routes on one line
cat tmp/ztexistjson.tmp | awk 'ORS=/,$/?" ":"\n"' > tmp/ztexistjson2.tmp
# Create the json formatted string containing the existing and new route
json=$(cat tmp/ztexistjson.tmp | jq --argjson curr "$(<tmp/ztexistjson2.tmp)" '.routes += [$curr]')
# rm -f tmp/ztexistjson.tmp
chk_jq
# Add the route
addRoute=$(curl -X POST -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" -d "${json}" "${ztAddress}/${theNet}"| jq '.routes')
fi
if [[ ("${dest}" =~ "${addRoute}" && "${gateway}" =~ "${addRoute}") ]]; then
echo "Route was not added."
rm -f tmp/ztexistjson.tmp
rm -f tmp/ztexistjson2.tmp
allDone
del_temp
else
echo "Route was added."
del_temp
rm -f tmp/ztexistjson.tmp
rm -f tmp/ztexistjson2.tmp
allDone
fi
;;
3) # Delete a route
get_routes
currNet
# Dynamic menu from:
# https://gist.github.com/nhoag/c202b3dd346668d6d8c1
ENTITIES=$(cat ${tmpRouteFile} | column -t -s "___")
SELECTION=1
while read -r line; do
echo "$SELECTION) $line"
((SELECTION++))
done <<< "$ENTITIES"
echo "B) To go back to the main menu"
((SELECTION--))
echo
printf 'Select the number next to the route to delete: '
read -r opt
if [[ ${opt} =~ ^(b|B)$ ]]; then
exit 0
fi
if [[ $(seq 1 $SELECTION) =~ $opt ]]; then
# Get the selection value
ther=$(sed -n "${opt}p" <<< "$ENTITIES")
# Destination
d=$(echo ${ther} | awk ' { print $1 } ' | sed 's/\//\\\//g')
# Gateway
g=$(echo ${ther} | awk ' { print $2 } ' |sed 's/\//\\\//g')
fi
# Prompt to delete route
read -p "Are you sure you want to delete the route => ${ther}? [y|N] " choice
if [[ "${choice}" =~ ^(y|Y)$ ]]; then
# Delete the line containing the route
sed -i "/\"target\": \"${d}.*\"via\": \"$g/d" tmp/ztexistjson.tmp
# Add end of the routes object
echo ']}' >> tmp/ztexistjson.tmp
# Put all routes on one line
cat tmp/ztexistjson.tmp | awk 'ORS=/,$/?" ":"\n"' > tmp/ztexistjson2.tmp
# Remove extraneous comma
sed -i 's/, \]}/\]}/g' tmp/ztexistjson2.tmp
# Create the json formatted string containing the
json=$(cat tmp/ztexistjson2.tmp | jq . )
# rm -f tmp/ztexistjson.tmp
chk_jq
# Delete the route
addRoute=$(curl -X POST -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" -d "${json}" "${ztAddress}/${theNet}"| jq '.routes')
else
echo "Route will not be deleted."
allDone
del_temp
rm -f tmp/ztexistjson.tmp
currNet
menu
fi
if [[ ("${d}" =~ "${addRoute}" && "${g}" =~ "${addRoute}") ]]; then
echo "Route was not deleted."
del_temp
currNet
allDone
else
echo "Route was deleted."
get_routes
currNet
allDone
del_temp
fi
;;
e|E) # Exit
exit 0
;;
*) echo "Invalid Option"
sleep 2
menu
;;
esac
menu
}
menu