-
Notifications
You must be signed in to change notification settings - Fork 32
/
functions.bash
549 lines (340 loc) · 11.6 KB
/
functions.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
# Various functions for the program
# ZeroTier network address
ztAddress='http://127.0.0.1:9993/controller/network'
# Token file
ztToken='/var/lib/zerotier-one/authtoken.secret'
# Temp File
tmpfile='tmp/znetwork.tmp'
ztnetFile='tmp/ztcurrent.txt'
peerTempFile='tmp/networks.tmp'
tmpPeerFile='tmp/ztnetwork-peerfile.tmp'
# ZT Directory
ztDir='/var/lib/zerotier-one'
# local.conf file
localConfig=''${ztDir}'/local.conf'
localConfigTemplate='templates/local.conf.template'
bkLocalConfig='tmp/local.conf.tmp'
# Create temp directory
if [[ ! -d "tmp" ]]; then
mkdir tmp
fi
function ip_chk() {
if [[ ${1} =~ ^(e|E)$ ]]; then
menu
fi
# If set to default then the gateway is null so it will set to the default route.
if [[ ${1} == "default" ]]; then
if [[ "$(ipcalc ${1})" =~ "INVALID" ]]; then
echo "Invalid network/host entered."
# Take the user back to the function.
${2}
fi
fi
}
function ipnet_chk() {
if [[ ${1} =~ ^(e|E)$ ]]; then
${2}
fi
if [[ "$(ipcalc ${1})" =~ "INVALID" ]]; then
echo "Invalid network/host entered."
# Take the user back to the function.
${2}
fi
}
function allDone() {
read -p "${1}. Press Enter to finish"
${2}
}
function del_temp() {
if [[ -f ${tmpRouteFile} ]]; then
rm -f ${tmpRouteFile}
fi
}
# Function to check if json object created properly
function chk_jq() {
if [[ $? -ne 0 ]]; then
allDone "Error with jq query creation." ${2}
fi
}
# Test an IP address for validity:
# https://www.linuxjournal.com/content/validating-ip-address-bash-script
# Usage:
# valid_ip IP_ADDRESS
# if [[ $? -eq 0 ]]; then echo good; else echo bad; fi
# OR
# if valid_ip IP_ADDRESS; then echo good; else echo bad; fi
#
function valid_ip()
{
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
function get_mask() {
: '
# Ipcalc sample
Address: 192.168.0.0 11000000.10101000.00000 000.00000000
Netmask: 255.255.248.0 = 21 11111111.11111111.11111 000.00000000
Wildcard: 0.0.7.255 00000000.00000000.00000 111.11111111
=>
Network: 192.168.0.0/21 11000000.10101000.00000 000.00000000
HostMin: 192.168.0.1 11000000.10101000.00000 000.00000001
HostMax: 192.168.7.254 11000000.10101000.00000 111.11111110
Broadcast: 192.168.7.255 11000000.10101000.00000 111.11111111
Hosts/Net: 2046 Class C, Private Internet
echo $((RANDOM%192 + 192)).$((RANDOM%256)).$((RANDOM%256)).$((RANDOM%256))
560 echo ${P[RANDOM%3]}
echo $((RANDOM%254))
'
# ipcalc 192.168.0.0/21 -n
if ! [[ "${autogen}" =~ ^(y|Y)$ ]]; then
# Get a netmask
read -p "Provide the netmask or cidr notation. If you are unsure, just hit Enter and it will use /24 (if you don't plan to have more than 254 peers on this network): " themask
else
themask="/24"
fi
# Remove slash if provided.
netmask=$(echo ${themask} | sed 's/\///g')
if [[ "$(ipcalc ${1}/${netmask})" =~ "INVALID MASK" ]]; then
echo "Invalid mask."
read -p "Press ENTER when done."
get_mask
else
# ipcalc does all the work.
get_net=$(ipcalc ${1}/${netmask})
# Starting IP in DHCP Pool
min=$(echo ${get_net} | egrep -o "HostMin: [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" | cut -d: -f2 | sed 's/[[:space:]]//g' )
# End IP in DHCP Pool
max=$(echo ${get_net} | egrep -o "HostMax: [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" | cut -d: -f2 | sed 's/[[:space:]]//g' )
# Network and CIDR notation
network=$(echo ${get_net} | egrep -o "Network: [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/[0-9]{1,2}" | cut -d: -f2 | sed 's/[[:space:]]//g' )
# Verify the settings
echo "Start IP: $min"
echo "End IP: $max"
echo "Network: $network"
read -p "Are the above settings okay? [Y|n]" netok
if [[ "${netok}" == "" || "${netok}" =~ ^(y|Y)$ ]] ; then
cnet=$(echo ${2} | sed 's/\"//g')
# Construct IP Assignment for ZT
json=$(jq -n --arg Start "${min}" --arg End "${max}" '{ ipAssignmentPools:[{ipRangeStart: $Start,ipRangeEnd: $End}] }')
assignIP=$(curl -s -X POST \
-H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" \
-d "$json" "${ztAddress}/${cnet}" | jq -r '(.ipAssignmentPools[0].ipRangeEnd)')
route='""'
# Construct Route for LAN
json=$(jq -n --arg target "${network}" --arg route "${route}" '{ routes:[{target: $target, via:$route}] }')
# Set LAN ROUTE
lanRoute=$(curl -s -X POST -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" -d "$json" "${ztAddress}/${cnet}" | jq -r '(.routes[0].target)')
# Turn on DHCP for auto assigning IPs
json=$(jq -n --arg dhcpOn "true" '{ v4AssignMode: { zt : $dhcpOn } }')
# Set LAN ROUTE
autoIP=$(curl -s -X POST -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" -d "$json" "${ztAddress}/${cnet}" |jq -r '(.v4AssignMode.zt)')
# Check if network returns the same settings provided.
if [[ ("${assignIP}" == "${max}" && "${lanRoute}" == "${network}" && "${autoIP}" == "true") ]]; then
allDone "The network settings were enabled. Peers can now join and will be assigned an IP address." "mainMenu"
else
allDone "Error adding the network settings" "mainMenu"
fi
else
mainMenu
fi
fi
}
function genIP() {
read -p "Would you like to autogenerate an IP range? [Y|n] or Enter to return to main menu: " autogen
if [[ "${autogen}" == "" || "${autogen}" =~ ^(y|Y)$ ]]; then
# Private ranges for the ZT Network
a=(10 192.168 172.16)
# Select one above
oct1=${a[RANDOM%3]}
# $((RANDOM%254)) selects a number 0 - 254
if [[ "${oct1}" == 10 ]]; then
ipnet="10.$((RANDOM%254)).$((RANDOM%254)).1"
elif [[ "${oct1}" == "172.16" ]]; then
ipnet="172.16.$((RANDOM%254)).1"
else
ipnet="192.168.$((RANDOM%254)).1"
fi
else
# Manual entry for a network
function user_ip() {
echo ""
read -p "Enter the first usable IP for the network: " theip
if ! [[ "${theip}" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
user_ip
fi
}
user_ip
if [[ "$(ipcalc ${theip})" =~ "INVALID" ]]; then
echo "Invalid IP."
read -p "Press ENTER when done."
user_ip
else
ipnet=${theip}
fi
fi
}
function allNets() {
rm -f ${tmpfile}
del_temp
echo " Network___Description___RangeStart___RangeEnd" > ${tmpfile}
# Get all Networks
for i in $(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}" | egrep -o '[a-f0-9]{16}'); do
# Get the network's name
desc=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/$i" |jq '.name' | sed 's/"//g')
ipAssign=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/$i" |jq -r '.ipAssignmentPools[].ipRangeStart,.ipAssignmentPools[].ipRangeEnd' | paste -sd, - | sed 's/,/___/g')
echo " ${i}___${desc}___${ipAssign}" >> ${tmpfile}
done
# Dynamic menu from:
# https://gist.github.com/nhoag/c202b3dd346668d6d8c1
cat ${tmpfile} | column -t -s "___" |head -1
ENTITIES=$(cat ${tmpfile} | grep -v " Network___" | 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 network: '
read -r opt
if [[ ${opt} =~ ^(b|B)$ ]]; then
# This is due to the changed to the flow directory
# and having to back out of that directory
if [[ "${1}" =~ ":flows" ]]; then
cd ..
theMenu=$(echo ${1} | cut -d: -f1)
${theMenu}
fi
${1}
fi
checkNum ${opt}
if [[ $(seq 1 $SELECTION) =~ $opt ]]; then
# Get the selection value
net=$(sed -n "${opt}p" <<< "$ENTITIES")
fi
# Write selected network to a file
echo "${net}" | awk ' { print $1 } ' > ${ztnetFile}
}
function currNet(){
theNet=$(cat ${ztnetFile})
}
# Check that the number is valid entered
function checkNum() {
if ! [[ ${1} =~ ^([0-9]{1,2})$ || ${1} =~ ^(b|B)$ ]]; then
allDone "A numeric selection is required" "mainMenu"
fi
}
# Create the network
function createNet() {
read -p "Please enter a description for the network: " netDesc
genIP
# Create the network
CONTROLLER_ID=$(zerotier-cli info | cut -d' ' -f 3)
newNet=$(curl -s -X POST \
-H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" \
-d '{"name": "'"${netDesc}"'"}' \
"${ztAddress}/${CONTROLLER_ID}______" | jq '(.nwid)')
if [[ "${newNet}" =~ ^\"[0-9a-f]{16}\" ]]; then
echo "Network created. ID: ${newNet}."
read -p "Press ENTER when done."
get_mask ${ipnet} ${newNet}
else
allDone "Network was not created" mainMenu
fi
}
# Updates the network's IP Assignment
function updateNetIP() {
allNets
theNet=$(echo "${net}" | awk ' { print $1 } ')
genIP
if [[ "${ipnet}" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
get_mask ${ipnet} ${theNet}
else
allDone "IP was not created" "mainMenu"
fi
}
# Update the network's description
function updateDesc() {
allNets
theNet=$(echo "${net}" | awk ' { print $1 } ')
read -p "Please enter the updated description for the network: " netDesc
json=$(jq -n --arg desc "${netDesc}" '{ name: $desc }')
chk_jq
# Update the network description
updateNet=$(curl -s -X POST \
-H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" \
-d "$json" \
"${ztAddress}/${theNet}" | jq -r '(.name)')
if [[ "${updateNet}" == "${netDesc}" ]]; then
allDone "${updateNet} description has been updated" mainMenu
else
allDone "${updateNet} description was not updated" mainMenu
fi
}
# List interfaces
function listIf() {
ENTITIES=$(ip addr |egrep '^[0-9]:{1,2}' | cut -d: -f2 | sed 's/ //g')
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 interface to Block or press B to go Back: '
read -r opt
if [[ ${opt} =~ ^(b|B)$ ]]; then
manageIF
fi
checkNum ${opt}
if [[ $(seq 1 $SELECTION) =~ $opt ]]; then
# Get the selection value
intF=$(sed -n "${opt}p" <<< "$ENTITIES")
fi
}
# List current interfaces
function listLocalConf() {
if [[ "${1}" == "interfacePrefixBlacklist" ]]; then
ENTITIES=$(grep interfacePrefixBlacklist ${localConfig} | egrep -o '\[.*\]' | sed -e 's/\[//g;s/\]//g;s/[",]/ /g' |tr ' ' '\n' |grep '.')
msgLocalConf='interface'
elif [[ "${1}" == "allowManagementFrom" ]]; then
ENTITIES=$(grep allowManagementFrom ${localConfig} | egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/[0-9]{1,2}')
msgLocalConf='IP or Netowrk'
else
advZT
fi
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 '${msgLocalConf}': '
read -r opt
# Check if B to go back or numeric
if [[ ${opt} =~ ^(b|B)$ ]]; then
advZT
fi
if ! [[ ${opt} =~ [0-9] ]]; then
advZT
fi
checkNum ${opt}
if [[ $(seq 1 $SELECTION) =~ $opt ]]; then
# Get the selection value
currSetting=$(sed -n "${opt}p" <<< "$ENTITIES")
fi
}