-
Notifications
You must be signed in to change notification settings - Fork 0
/
ntios_wln_netplanfunc.sh
executable file
·462 lines (418 loc) · 18.6 KB
/
ntios_wln_netplanfunc.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
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
#!/bin/bash
#---FUNCTIONS
WLN_Netplan_Handler() {
#Input args
local isbssmode=${1}
local istargetfpath=${2}
local isautoreconnectonboot_istriggered=${3} # {YES | NO}
#Define variables
local intfstates_ctx_fpath="${WLN_EMPTYSTRING}"
local ret="${REJECTED}"
#Set variable(s) based on 'isautoreconnectonboot_istriggered' input value
if [[ ${isautoreconnectonboot_istriggered} == "${NO}" ]]; then
intfstates_ctx_fpath="${WLN_INTFSTATES_CTX_DAT_FPATH}"
else
intfstates_ctx_fpath="${WLN_INTFSTATES_CTX_INIT_DAT_FPATH}"
fi
#----------------------------------------------------------------
#REMARK:
# if 'wln_ip', 'wln_netmask', 'wln_ipv6', 'wln_netmaskv6'
# ...is NOT provided, then the default values
# ...(1.2.0.1, 255.255.255.255, 1:2::1, 128) will be used.
#----------------------------------------------------------------
# #Retrieve data from database
# if [[ "${isbssmode}" == "${WLN_EMPTYSTRING}"]]; then
# isbssmode=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__bssmode")
# fi
#Create directory '/etc/tibbo/netplan/wln' (if not present)
if [[ $(Mkdir "${WLN_ETC_TIBBO_NETPLAN_WLN_DIR}") == false ]]; then
ret="${REJECTED}"
else
#Generate 'wlan.yaml'
case "${isbssmode}" in
"${PL_WLN_BSS_MODE_INFRASTRUCTURE}")
ret=$(Netplan_For_Infrastructure "${intfstates_ctx_fpath}")
;;
"${PL_WLN_BSS_MODE_ACCESSPOINT}")
ret=$(Netplan_For_AccessPoint "${intfstates_ctx_fpath}")
;;
"${PL_WLN_BSS_MODE_ROUTER}")
ret=$(Netplan_For_Router "${intfstates_ctx_fpath}")
;;
esac
fi
#Netplan apply
# ret=$(WLN_Netplan_Apply)
#Output
echo "${ret}"
return 0;
}
Netplan_For_Infrastructure() {
#Input args
local isintfstates_ctx_fpath=${1}
#Define constants
local PHASE_NETPLANFUNC_RETRIEVE_DATA=1
local PHASE_NETPLANFUNC_YAML_CREATE=10
local PHASE_NETPLANFUNC_YAML_REMOVE=20
local PHASE_NETPLANFUNC_YAML_WRITE=30
local PHASE_NETPLANFUNC_EXIT=100
local SCAN_SSID_1_HACK="\\\"\\\n scan_ssid=1\\\n# \\\"hack!"
#Define variables
local intf="${WLN_EMPTYSTRING}"
local dhcp="${WLN_EMPTYSTRING}"
local ip="${WLN_EMPTYSTRING}"
local cidr="${WLN_EMPTYSTRING}"
local gatewayip="${WLN_EMPTYSTRING}"
local dns1="${WLN_EMPTYSTRING}"
local dns2="${WLN_EMPTYSTRING}"
local WLN_intfstates_ctx__dhcpv6="${WLN_EMPTYSTRING}"
local ipv6="${WLN_EMPTYSTRING}"
local cidrv6="${WLN_EMPTYSTRING}"
local gatewayipv6="${WLN_EMPTYSTRING}"
local dns1v6="${WLN_EMPTYSTRING}"
local dns2v6="${WLN_EMPTYSTRING}"
local ssid="${WLN_EMPTYSTRING}"
# local ssid_isvisible="${WLN_EMPTYSTRING}"
local wepmode="${WLN_EMPTYSTRING}"
local wepkey="${WLN_EMPTYSTRING}"
local wpamode="${WLN_EMPTYSTRING}"
local wpakey="${WLN_EMPTYSTRING}"
local filecontent="${WLN_EMPTYSTRING}"
local phase="${PHASE_NETPLANFUNC_RETRIEVE_DATA}"
local ret="${REJECTED}"
#Start phase
while true
do
case "${phase}" in
"${PHASE_NETPLANFUNC_RETRIEVE_DATA}")
#Retrieve data from database
intf=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__intf")
if [[ -z "${intf}" ]]; then
intf="${WLN_WLAN0}"
fi
#If 'dhcp = false', then the 'addresses' field in 'netplan'
# can NOT be an empty.
#Therefore, if the 'ip' and 'cidr' are Empty Strings, then
# use the default values instead.
dhcp=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__dhcp")
ip=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__ip")
if [[ -z "${ip}" ]]; then
ip="${WLN_IPV4_DEFAULT}"
fi
cidr=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__cidr")
if [[ -z "${cidr}" ]]; then
cidr="${WLN_IPV4_CIDR_PREFIX_24}"
fi
gatewayip=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__gatewayip")
dns1=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__dns1")
dns2=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__dns2")
#If 'dhcpv6 = false', then the 'addresses' field in 'netplan'
# can NOT be an empty.
#Therefore, if the 'ipv6' and 'cidrv6' are Empty Strings, then
# use the default values instead.
dhcpv6=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__dhcpv6")
ipv6=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__ipv6")
if [[ -z "${ipv6}" ]]; then
ipv6="${WLN_IPV6_DEFAULT}"
fi
cidrv6=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__cidrv6")
if [[ -z "${cidrv6}" ]]; then
cidrv6="${WLN_IPV6_CIDR_DEFAULT}"
fi
gatewayipv6=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__gatewayipv6")
dns1v6=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__dns1v6")
dns2v6=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__dns2v6")
ssid=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__ssid")
bssid=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__bssid")
wepmode=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__wepmode")
wepkey=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__wepkey")
wpamode=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__wpamode")
wpakey=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__wpakey")
#Goto next-phase
phase="${PHASE_NETPLANFUNC_YAML_CREATE}"
;;
"${PHASE_NETPLANFUNC_YAML_CREATE}")
#Generate
filecontent="network:\n"
filecontent+=" version: 2\n"
filecontent+=" renderer: networkd\n"
filecontent+=" wifis:\n"
filecontent+=" ${intf}:\n"
if [[ "${dhcp}" == true ]]; then
filecontent+=" dhcp4: true\n"
else
filecontent+=" addresses: [${ip}/${cidr}]\n"
if [[ -n "${gatewayip}" ]]; then
filecontent+=" gateway4: \"${gatewayip}\"\n"
fi
fi
if [[ "${dhcpv6}" == true ]]; then
filecontent+=" dhcp6: true\n"
else
filecontent+=" addresses: [${ipv6}/${cidrv6}]\n"
if [[ -n "${gatewayipv6}" ]]; then
filecontent+=" gateway6: \"${gatewayipv6}\"\n"
fi
fi
if [[ -n "${dns1}" ]] || [[ -n "${dns2}" ]] || [[ -n "${dns1v6}" ]] || [[ -n "${dns2v6}" ]]; then
filecontent+=" nameservers:\n"
if [[ -n "${dns1}" ]]; then
filecontent+=" addresses: [${dns1}]\n"
fi
if [[ -n "${dns2}" ]]; then
filecontent+=" addresses: [${dns2}]\n"
fi
if [[ -n "${dns1v6}" ]]; then
filecontent+=" addresses: [${dns1v6}]\n"
fi
if [[ -n "${dns2v6}" ]]; then
filecontent+=" addresses: [${dns2v6}]\n"
fi
fi
filecontent+=" access-points:\n"
if [[ "${wepmode}" == "${PL_WLN_WEP_MODE_DISABLED}" ]] && \
[[ "${wpamode}" == "${PL_WLN_WPA_DISABLED}" ]]; then
if [[ -n "${bssid}" ]] && [[ "${bssid}" != ${WLN_BSSID_000000} ]]; then
filecontent+=" \"${ssid}${SCAN_SSID_1_HACK}\":\n"
filecontent+=" bssid: \"${bssid}\"\n"
else
filecontent+=" \"${ssid}${SCAN_SSID_1_HACK}\": {}\n" #always use 'scan_ssid=1' in whether the SSID is visible or not.
fi
else #wepmode != PL_WLN_WEP_MODE_DISABLED or wpamode != PL_WLN_WPA_DISABLED
filecontent+=" \"${ssid}${SCAN_SSID_1_HACK}\":\n" #always use 'scan_ssid=1' in whether the SSID is visible or not.
if [[ "${wepmode}" != "${PL_WLN_WEP_MODE_DISABLED}" ]]; then
filecontent+=" password: \"${wepkey}\"\n"
else #wpamode != PL_WLN_WPA_DISABLED
filecontent+=" password: \"${wpakey}\"\n"
fi
if [[ -n "${bssid}" ]] && [[ "${bssid}" != ${WLN_BSSID_000000} ]]; then
filecontent+=" bssid: \"${bssid}\"\n"
fi
fi
#Goto next-phase
phase="${PHASE_NETPLANFUNC_YAML_REMOVE}"
;;
"${PHASE_NETPLANFUNC_YAML_REMOVE}")
if [[ $(RemoveFile "${istargetfpath}") == "${REJECTED}" ]]; then
#Set output-result
ret="${REJECTED}"
#Goto next-phase
phase="${PHASE_NETPLANFUNC_EXIT}"
else
#Goto next-phase
phase="${PHASE_NETPLANFUNC_YAML_WRITE}"
fi
;;
"${PHASE_NETPLANFUNC_YAML_WRITE}")
if [[ $(WriteToFile "${istargetfpath}" "${filecontent}" "true") == "${REJECTED}" ]]; then
#Set output-result
ret="${REJECTED}"
else
#Set output-result
ret="${ACCEPTED}"
fi
#Goto next-phase
phase="${PHASE_NETPLANFUNC_EXIT}"
;;
"${PHASE_NETPLANFUNC_EXIT}")
break
;;
esac
done
#Output
echo "${ret}"
return 0;
}
Netplan_For_AccessPoint() {
#Input args
local isintfstates_ctx_fpath=${1}
#Define constants
local PHASE_NETPLANFUNC_RETRIEVE_DATA=1
local PHASE_NETPLANFUNC_YAML_CREATE=10
local PHASE_NETPLANFUNC_YAML_REMOVE=20
local PHASE_NETPLANFUNC_YAML_WRITE=30
local PHASE_NETPLANFUNC_BRIDGE_ADD_AND_BRINGUP=40
local PHASE_NETPLANFUNC_EXIT=100
#Define variables
local intf="${WLN_EMPTYSTRING}"
local bridge="${WLN_EMPTYSTRING}"
local cidr="${WLN_EMPTYSTRING}"
local cidrv6="${WLN_EMPTYSTRING}"
local ip="${WLN_EMPTYSTRING}"
local ipv6="${WLN_EMPTYSTRING}"
local filecontent="${WLN_EMPTYSTRING}"
local phase="${PHASE_NETPLANFUNC_RETRIEVE_DATA}"
local ret="${REJECTED}"
#Start phase
while true
do
case "${phase}" in
"${PHASE_NETPLANFUNC_RETRIEVE_DATA}")
#Retrieve data from database
intf=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__intf")
bridge=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__bridge")
ip=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__ip")
cidr=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__cidr")
ipv6=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__ipv6")
cidrv6=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__cidrv6")
#Goto next-phase
phase="${PHASE_NETPLANFUNC_YAML_CREATE}"
;;
"${PHASE_NETPLANFUNC_YAML_CREATE}")
#Generate
filecontent="network:\n"
filecontent+=" version: 2\n"
filecontent+=" renderer: networkd\n"
filecontent+=" ethernets:\n"
filecontent+=" ${intf}:\n"
filecontent+=" dhcp4: no\n"
filecontent+=" dhcp6: no\n"
filecontent+=" bridges:\n"
filecontent+=" ${bridge}:\n"
filecontent+=" interfaces: [${WLN_AP_BRIF_LIST}]\n"
filecontent+=" addresses: [${ip}/${cidr}]\n"
filecontent+=" nameservers: {}\n"
filecontent+=" addresses: [${ipv6}/${cidrv6}]\n"
filecontent+=" nameservers: {}"
#Goto next-phase
phase="${PHASE_NETPLANFUNC_YAML_REMOVE}"
;;
"${PHASE_NETPLANFUNC_YAML_REMOVE}")
if [[ $(RemoveFile "${istargetfpath}") == "${REJECTED}" ]]; then
#Set output-result
ret="${REJECTED}"
#Goto next-phase
phase="${PHASE_NETPLANFUNC_EXIT}"
else
#Goto next-phase
phase="${PHASE_NETPLANFUNC_YAML_WRITE}"
fi
;;
"${PHASE_NETPLANFUNC_YAML_WRITE}")
if [[ $(WriteToFile "${istargetfpath}" "${filecontent}" "true") == "${REJECTED}" ]]; then
#Set output-result
ret="${REJECTED}"
#Goto next-phase
phase="${PHASE_NETPLANFUNC_EXIT}"
else
#Goto next-phase
phase="${PHASE_NETPLANFUNC_BRIDGE_ADD_AND_BRINGUP}"
fi
;;
"${PHASE_NETPLANFUNC_BRIDGE_ADD_AND_BRINGUP}")
if [[ $(WLN_Bridge_Add_And_BringUp) == "${REJECTED}" ]]; then
#Set output-result
ret="${REJECTED}"
else
#Set output-result
ret="${ACCEPTED}"
fi
#Goto next-phase
phase="${PHASE_NETPLANFUNC_EXIT}"
;;
"${PHASE_NETPLANFUNC_EXIT}")
break
;;
esac
done
#Output
echo "${ret}"
return 0;
}
Netplan_For_Router() {
#Input args
local isintfstates_ctx_fpath=${1}
#Define constants
local PHASE_NETPLANFUNC_RETRIEVE_DATA=1
local PHASE_NETPLANFUNC_YAML_CREATE=10
local PHASE_NETPLANFUNC_YAML_REMOVE=20
local PHASE_NETPLANFUNC_YAML_WRITE=30
local PHASE_NETPLANFUNC_EXIT=100
#Define variables
local intf="${WLN_EMPTYSTRING}"
local bridge="${WLN_EMPTYSTRING}"
local cidr="${WLN_EMPTYSTRING}"
local cidrv6="${WLN_EMPTYSTRING}"
local ip="${WLN_EMPTYSTRING}"
local ipv6="${WLN_EMPTYSTRING}"
local filecontent="${WLN_EMPTYSTRING}"
local phase="${PHASE_NETPLANFUNC_RETRIEVE_DATA}"
local ret="${REJECTED}"
#Start phase
while true
do
case "${phase}" in
"${PHASE_NETPLANFUNC_RETRIEVE_DATA}")
#Retrieve data from database
intf=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__intf")
bridge=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__bridge")
ip=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__ip")
cidr=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__cidr")
ipv6=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__ipv6")
cidrv6=$(WLN_intfstates_ctx_retrievedata "${isintfstates_ctx_fpath}" "WLN_intfstates_ctx__cidrv6")
#Goto next-phase
phase="${PHASE_NETPLANFUNC_YAML_CREATE}"
;;
"${PHASE_NETPLANFUNC_YAML_CREATE}")
#Generate
filecontent="network:\n"
filecontent+=" version: 2\n"
filecontent+=" renderer: networkd\n"
filecontent+=" ethernets:\n"
filecontent+=" ${intf}:\n"
filecontent+=" dhcp4: no\n"
filecontent+=" dhcp6: no\n"
filecontent+=" bridges:\n"
filecontent+=" ${bridge}:\n"
filecontent+=" interfaces: [${WLN_RT_BRIF_LIST}]\n"
filecontent+=" addresses: [${ip}/${cidr}]\n"
filecontent+=" nameservers: {}\n"
filecontent+=" addresses: [${ipv6}/${cidrv6}]\n"
filecontent+=" nameservers: {}"
#Goto next-phase
phase="${PHASE_NETPLANFUNC_YAML_REMOVE}"
;;
"${PHASE_NETPLANFUNC_YAML_REMOVE}")
if [[ $(RemoveFile "${istargetfpath}") == "${REJECTED}" ]]; then
#Set output-result
ret="${REJECTED}"
#Goto next-phase
phase="${PHASE_NETPLANFUNC_EXIT}"
else
#Goto next-phase
phase="${PHASE_NETPLANFUNC_YAML_WRITE}"
fi
;;
"${PHASE_NETPLANFUNC_YAML_WRITE}")
if [[ $(WriteToFile "${istargetfpath}" "${filecontent}" "true") == "${REJECTED}" ]]; then
#Set output-result
ret="${REJECTED}"
else
#Set output-result
ret="${ACCEPTED}"
fi
#Goto next-phase
phase="${PHASE_NETPLANFUNC_EXIT}"
;;
"${PHASE_NETPLANFUNC_EXIT}")
break
;;
esac
done
#Output
echo "${ret}"
return 0;
}
WLN_Netplan_Apply() {
#Define variables
local ret="${ACCEPTED}"
#Execute command
if [[ $(CmdExec "${WLN_NETPLAN_APPLY}") == true ]]; then
ret="${ACCEPTED}"
else
ret="${REJECTED}"
fi
#Output
echo "${ret}"
return 0;
}