-
Notifications
You must be signed in to change notification settings - Fork 0
/
ntios_wln_subfunc.sh
executable file
·694 lines (543 loc) · 16.3 KB
/
ntios_wln_subfunc.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
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
#!/bin/bash
#---FUNCTIONS
Chmod() {
#Input args
local istargetfpath=${1}
local ispermissionval=${2}
#Define variables
local printmsg="${WLN_EMPTYSTRING}"
local ret=false
if [[ $(FileExists "${istargetfpath}") == true ]]; then
#Change permissions
sudo chmod "${ispermissionval}" "${istargetfpath}" >/dev/null;exitcode=$?;pid=$!;wait ${pid}
#Print
if [[ ${exitcode} -eq 0 ]]; then
printmsg="${WLN_PRINTMSG_STATUS}: Change permissions of ${WLN_LIGHTGREY}${istargetfpath}${WLN_RESETCOLOR}: ${WLN_PRINTMSG_DONE}"
ret=true
else
printmsg="${WLN_PRINTMSG_STATUS}: Change permissions of ${WLN_LIGHTGREY}${istargetfpath}${WLN_RESETCOLOR}: ${WLN_PRINTMSG_FAILED}"
ret=false
fi
else
printmsg="${WLN_PRINTMSG_STATUS}: file '${WLN_LIGHTGREY}${istargetfpath}${WLN_RESETCOLOR}' "
printmsg+="does ${WLN_PRINTMSG_NOT} exist"
ret=false
fi
#Print
DebugPrint "${printmsg}"
#Output
echo "${ret}"
return 0;
}
CmdExec() {
#Input args
local cmd=${1}
local sudo_isnot_required=${2}
#Define variables
local printmsg="${WLN_EMPTYSTRING}"
local ret=false
##Update print-message
printmsg="${WLN_PRINTMSG_STATUS}: Execute ${WLN_LIGHTGREY}${cmd}${WLN_RESETCOLOR}: ${WLN_PRINTMSG_IN_PROGRESS}"
#Print
DebugPrint "${printmsg}"
#Execute command
# shellcheck disable=SC2086
# sudo ${cmd} >/dev/null;exitcode=$?;pid=$!;wait ${pid}
if [[ ${sudo_isnot_required} == true ]]; then
${cmd} ; exitcode=$? ; pid=$! ; wait ${pid}
else
sudo ${cmd} ; exitcode=$? ; pid=$! ; wait ${pid}
fi
#Update print-message
if [[ ${exitcode} -eq 0 ]]; then
printmsg="${WLN_PRINTMSG_STATUS}: Execute ${WLN_LIGHTGREY}${cmd}${WLN_RESETCOLOR}: ${WLN_PRINTMSG_DONE}"
ret=true
else
printmsg="${WLN_PRINTMSG_STATUS}: Execute ${WLN_LIGHTGREY}${cmd}${WLN_RESETCOLOR}: ${WLN_PRINTMSG_FAILED}"
ret=false
fi
#Print
DebugPrint "${printmsg}"
#Output
echo "${ret}"
return 0;
}
CopyFile() {
#Input args
local issourcefpath=${1}
local istargetfpath=${2}
#Define variables
local cmd="${WLN_EMPTYSTRING}"
local printmsg="${WLN_EMPTYSTRING}"
local ret=false
#Check if 'issourcefpath' exists
if [[ $(FileExists "${issourcefpath}") == false ]]; then
printmsg="${WLN_PRINTMSG_STATUS}: file '${WLN_LIGHTGREY}${issourcefpath}${WLN_RESETCOLOR}' "
printmsg+="does ${WLN_PRINTMSG_NOT} exist"
ret=false
else
#Write to file
sudo cp "${issourcefpath}" "${istargetfpath}" >/dev/null; exitcode=$?; pid=$!; wait ${pid}
#Print
if [[ ${exitcode} -eq 0 ]]; then
printmsg="${WLN_PRINTMSG_STATUS}: copy from '${WLN_LIGHTGREY}${issourcefpath}${WLN_RESETCOLOR}' "
printmsg+="to '${WLN_LIGHTGREY}${istargetfpath}${WLN_RESETCOLOR}': ${WLN_PRINTMSG_DONE}"
ret=true
else
printmsg="${WLN_PRINTMSG_STATUS}: copy from '${WLN_LIGHTGREY}${issourcefpath}${WLN_RESETCOLOR}' "
printmsg+="to '${WLN_LIGHTGREY}${istargetfpath}${WLN_RESETCOLOR}': ${WLN_PRINTMSG_FAILED}"
ret=false
fi
fi
#Print
DebugPrint "${printmsg}"
#Output
echo "${ret}"
return 0;
}
CurlDomainGet() {
#Define variables
local ret="${WLN_EMPTYSTRING}"
#Get Domain-code using curl
ret=$(curl --silent https://ipinfo.io | grep -o 'country.*' | cut -d":" -f2 | cut -d"\"" -f2)
#Output
echo "${ret}"
return 0
}
DaisyChain_IsEnabled() {
#Remark:
# This function is defined in ntios_net.cpp
#Check if Path exist
local ret=false
local currDaisyChainModeSetting_U8=${PL_NET_DAISYCHAINSET_OFF}
currDaisyChainModeSetting_U8=$(cat "${NET_DAISYCHAIN_MODE_FPATH}")
if [[ $(FileExists "${NET_DAISYCHAIN_MODE_FPATH}") == true ]]; then
# if [[ -f "${NET_DAISYCHAIN_MODE_FPATH}" ]]; then
if [[ ${currDaisyChainModeSetting_U8} -eq ${PL_NET_DAISYCHAINSET_ON} ]]; then
ret=true
fi
fi
#Output
echo "${ret}"
return 0
}
DebugPrint() {
#Input args
local printmsg=${1}
#Get the current shell-terminal
local tty_curr=$(tty)
#Print
#Remark:
# By redirecting the 'echo' output to the current shell-terminal (e.g. /dev/ttyS1)
# ...the 'echo' output can be seen on the terminal.
# Without the redirection (1>${tty_curr}), this is not possible!
echo -e "${printmsg}" 1>${tty_curr}
}
DirExists() {
#Input args
local isdir=${1}
#Define variables
local ret=false
#Check if 'isdir' exists
if [[ -z "${isdir}" ]]; then
ret=false
else
if sudo test -d "${isdir}"; then
ret=true
else
ret=false
fi
fi
#Output
echo "${ret}"
return 0
}
FileExists() {
#Input args
local istargetfpath=${1}
#Define variables
local ret=false
#Check if 'istargetfpath' exists
if [[ -z "${istargetfpath}" ]]; then
ret=false
else
if sudo test -f "${istargetfpath}"; then
ret=true
else
ret=false
fi
fi
#Output
echo "${ret}"
return 0
}
EvalCmdExec() {
#Input args
local cmd=${1}
local sudo_isnot_required=${2}
#Define variables
local printmsg="${WLN_EMPTYSTRING}"
local ret="${WLN_EMPTYSTRING}"
##Update print-message
printmsg="${WLN_PRINTMSG_STATUS}: Execute ${WLN_LIGHTGREY}${cmd}${WLN_RESETCOLOR}: ${WLN_PRINTMSG_IN_PROGRESS}"
#Print
DebugPrint "${printmsg}"
#Prepend 'sudo' (if applicable)
if [[ ${sudo_isnot_required} == false ]]; then
cmd="sudo ${cmd}"
fi
#Execute command
ret=$(eval ${cmd}) ; exitcode=$? ; pid=$! ; wait ${pid}
#Update print-message
if [[ ${exitcode} -eq 0 ]]; then
printmsg="${WLN_PRINTMSG_STATUS}: Execute ${WLN_LIGHTGREY}${cmd}${WLN_RESETCOLOR}: ${WLN_PRINTMSG_DONE}"
else
printmsg="${WLN_PRINTMSG_STATUS}: Execute ${WLN_LIGHTGREY}${cmd}${WLN_RESETCOLOR}: ${WLN_PRINTMSG_FAILED}"
fi
#Print
DebugPrint "${printmsg}"
#Output
echo "${ret}"
return 0;
}
PatternIsFound() {
#Input args
local ispattern=${1}
local istargetfpath=${2}
#Define constants
local PHASE_PATTERNISFOUND_PATTERN_CHECK=1
local PHASE_PATTERNISFOUND_PATH_CHECK=10
local PHASE_PATTERNISFOUND_PATTERN_VALIDATE=20
local PHASE_PATTERNISFOUND_EXIT=100
#Define variables
local cmd="${WLN_EMPTYSTRING}"
local phase="${PHASE_PATTERNISFOUND_PATTERN_CHECK}"
local ret=false
#Start phase
while true
do
case "${phase}" in
"${PHASE_PATTERNISFOUND_PATTERN_CHECK}")
if [[ -z "${ispattern}" ]]; then
ret=false
phase="${PHASE_PATTERNISFOUND_EXIT}"
else
phase="${PHASE_PATTERNISFOUND_PATH_CHECK}"
fi
;;
"${PHASE_PATTERNISFOUND_PATH_CHECK}")
if [[ $(FileExists "${istargetfpath}") == false ]]; then
ret=false
phase="${PHASE_PATTERNISFOUND_EXIT}"
else
phase="${PHASE_PATTERNISFOUND_PATTERN_VALIDATE}"
fi
;;
"${PHASE_PATTERNISFOUND_PATTERN_VALIDATE}")
cmd="grep \"${ispattern}\" \"${istargetfpath}\""
if [[ -z $(EvalCmdExec "${cmd}" "false") ]]; then
ret=false
else
ret=true
fi
phase="${PHASE_PATTERNISFOUND_EXIT}"
;;
"${PHASE_PATTERNISFOUND_EXIT}")
break
;;
esac
done
#Output
echo "${ret}"
return 0
}
IwDomainGet() {
#Define variables
local ret=${WLN_EMPTYSTRING}
#Get Domain-code
ret=$(sudo iw reg get | grep -o "country.*" | cut -d" " -f2 | cut -d":" -f1)
#Output
echo "${ret}"
return 0
}
GetInterfaceState() {
#Input args
local isintf=${1}
#Define variables
local intfstate=${WLN_UNKNOWN}
local intfstate_alternative=${WLN_PATTERN_DOWN}
#Get interface-state
intfstate=$(cat "/sys/class/net/${isintf}/operstate")
#Chekc if 'intfstate = unknown'
if [[ "${intfstate}" != "${WLN_UP}" ]] && [[ "${intfstate}" != "${WLN_DOWN}" ]]; then
#Check the interface-state using the 'ip' command
intfstate_alternative=$(ip a list "${isintf}" | grep -o "${WLN_PATTERN_UP}.*" | cut -d"," -f1)
#Translate 'intfstate_alternative' to 'intfstate'
if [[ "${intfstate_alternative}" == "${WLN_PATTERN_UP}" ]]; then
intfstate="${WLN_UP}"
else
intfstate="${WLN_DOWN}"
fi
fi
#Output
echo "${intfstate}"
return 0;
}
IpIsForwarded() {
#Input args
local isPattern=${1}
#Define variables
local stdOutput=${WLN_EMPTYSTRING}
#Check if pattern is present
stdOutput=$(grep -w "^${isPattern}" "${WLN_SYSCTL_CONF_FPATH}")
if [[ -n "${stdOutput}" ]]; then
echo true
else
echo false
fi
}
IsHex() {
#--------------------------------------------------------------------
# Note:
# Input arg 'isHex' is a value WITHOUT any preceding hex-sign (e.g. 0x)
#--------------------------------------------------------------------
#Input args
local isHex=${1}
#Define variables
local hex_exec_ret=0
local exitcode=0
local ret=false
#Get exit-code after executing the hex-command
hex_exec_ret=$( (( 16#$isHex )) 2>&1); exitcode=$?
if [[ ${exitcode} -eq 0 ]]; then
ret=true
else
ret=false
fi
#Output
echo "${ret}"
return 0;
}
Mkdir() {
#Input args
local isdir=${1}
#Define variables
local cmd="${WLN_EMPTYSTRING}"
local ret="${WLN_EMPTYSTRING}"
#Check and create dir
if [[ $(DirExists "${isdir}") == false ]]; then
cmd="mkdir -p ${isdir}"
ret=$(CmdExec "${cmd}")
else
ret=true
fi
#Output
echo "${ret}"
return 0;
}
Ping() {
#Input args
local isintf=${1}
local ipaddr=${2}
local count=${3}
local deadline=${4}
#Define variables
local exitcode=0
local ret=false
#Execute ping
ping -n -I${isintf} -c "${count}" -w "${deadline}" "${ipaddr}" >/dev/null 2>&1; exitcode=$?
#Output
echo "${exitcode}"
return 0;
}
PortIsAllowed() {
#Input args
local isPort=${1}
#Define variables
local stdOutput=${WLN_EMPTYSTRING}
#Check if port is already allowed
# shellcheck disable=SC2086
stdOutput=$(sudo iptables -S | grep -w "${isPort}" | grep -w "${WLN_PATTERN_ACCEPT}")
if [[ -n "${stdOutput}" ]]; then
# shellcheck disable=SC2086
stdOutput=$(sudo ip6tables -S | grep -w "${isPort}" | grep -w "${WLN_PATTERN_ACCEPT}")
if [[ -n "${stdOutput}" ]]; then
echo true
else
echo false
fi
else
echo false
fi
}
RemoveDir() {
#Input args
local istargetdir=${1}
#Define variables
local printmsg="${WLN_EMPTYSTRING}"
local ret=false
#Remove 'wifi-powersave-off.service'
if [[ $(DirExists "${istargetdir}") == true ]]; then
sudo rm -rf "${istargetdir}" >/dev/null;exitcode=$?;pid=$!;wait ${pid}
if [[ ${exitcode} -eq 0 ]]; then
printmsg="${WLN_PRINTMSG_STATUS}: Remove ${WLN_LIGHTGREY}${istargetdir}${WLN_RESETCOLOR}: ${WLN_PRINTMSG_DONE}"
ret=true
else
printmsg="${WLN_PRINTMSG_STATUS}: Remove ${WLN_LIGHTGREY}${istargetdir}${WLN_RESETCOLOR}: ${WLN_PRINTMSG_FAILED}"
ret=false
fi
else
printmsg="${WLN_PRINTMSG_STATUS}: directory '${WLN_LIGHTGREY}${istargetdir}${WLN_RESETCOLOR}' "
printmsg+="does ${WLN_PRINTMSG_NOT} exist (ignore)"
ret=true
fi
#Print
DebugPrint "${printmsg}"
#Output
echo "${ret}"
return 0;
}
RemoveFile() {
#Input args
local istargetfpath=${1}
#Define variables
local printmsg="${WLN_EMPTYSTRING}"
local ret=false
#Remove 'wifi-powersave-off.service'
if [[ $(FileExists "${istargetfpath}") == true ]]; then
sudo rm "${istargetfpath}" >/dev/null;exitcode=$?;pid=$!;wait ${pid}
if [[ ${exitcode} -eq 0 ]]; then
printmsg="${WLN_PRINTMSG_STATUS}: Remove ${WLN_LIGHTGREY}${istargetfpath}${WLN_RESETCOLOR}: ${WLN_PRINTMSG_DONE}"
ret=true
else
printmsg="${WLN_PRINTMSG_STATUS}: Remove ${WLN_LIGHTGREY}${istargetfpath}${WLN_RESETCOLOR}: ${WLN_PRINTMSG_FAILED}"
ret=false
fi
else
printmsg="${WLN_PRINTMSG_STATUS}: file '${WLN_LIGHTGREY}${istargetfpath}${WLN_RESETCOLOR}' "
printmsg+="does ${WLN_PRINTMSG_NOT} exist (ignore)"
ret=true
fi
#Print
DebugPrint "${printmsg}"
#Output
echo "${ret}"
return 0;
}
Substitute_String_With_Another_String() {
#Input args
local istargetfpath=${1}
local oldstring=${2}
local newstring=${3}
#Substitute
sudo sed -i "s/${oldstring}/${newstring}/g" ${istargetfpath}
}
Sudoers_Allow_SetOfCmds() {
#Input args
local ispid=${1}
#Define variables
local cmd="${WLN_EMPTYSTRING}"
# local col1="${WLN_EMPTYSTRING}"
# local col2="${WLN_EMPTYSTRING}"
# local col3="${WLN_EMPTYSTRING}"
local colitem="${WLN_EMPTYSTRING}"
local arritem="${WLN_EMPTYSTRING}"
local srv_input="${WLN_EMPTYSTRING}"
local i=1
local ret_dummy=false
#Define command line for 'ispid'
cmd="systemctl start ${SYSTEMCTL_NTIOS_SU_ADD_AT}${ispid}"
#Execute command
ret_dummy=$(CmdExec "${cmd}" "false")
#Loop thru array
for arritem in "${BASH_SETSOFCMDS_ARR[@]}"
do
#Reset variables (IMPORTANT)
colitem="${WLN_EMPTYSTRING}"
srv_input="${WLN_EMPTYSTRING}"
i=1
while true
do
#Retrieve 'colitem' from 'arritem'
colitem=$(echo "${arritem}" | cut -d"," -f${i})
#Append to 'srv_input'
if [[ -n "${colitem}" ]]; then #is NOT <Empty String>
srv_input+="${WLN_FOURBACKSLASHES_SLASH_HEX}${colitem}"
else #is <Empty String>
srv_input+="${WLN_FOURBACKSLASHES_SPACE_HEX}${WLN_FOURBACKSLASHES_ASTERISK_HEX}"
break
fi
#Increment index
((i++))
done
#Define command line for 'cmdfpath'
cmd="systemctl start ${SYSTEMCTL_NTIOS_SU_ADD_AT}${srv_input}"
#Execute command
ret_dummy=$(CmdExec "${cmd}" "false")
done
}
Touch() {
#Input args
local istargetfpath=${1}
#Define variables
local cmd="${WLN_EMPTYSTRING}"
local ret="${WLN_EMPTYSTRING}"
#Check and create file
if [[ ! -f "${istargetfpath}" ]]; then
cmd="touch ${istargetfpath}"
ret=$(CmdExec "${cmd}")
else
ret=true
fi
#Output
echo "${ret}"
return 0;
}
Touch_and_Chmod() {
#Input args
local istargetfpath=${1}
local ispermissionval=${2}
#Define variables
local ret="${WLN_EMPTYSTRING}"
#Check and create file
if [[ $(Touch "${istargetfpath}" ) == true ]]; then
if [[ $(Chmod "${istargetfpath}" "${ispermissionval}") == true ]]; then
ret=true
else
ret=false
fi
else
ret=true
fi
#Output
echo "${ret}"
return 0;
}
WriteToFile() {
#Input args
local istargetfpath=${1}
local data=${2}
local issudoer=${3}
#Define variables
local printmsg="${WLN_EMPTYSTRING}"
local ret=false
#Write to file
if [[ "${issudoer}" == true ]]; then
echo -e "${data}" | sudo tee "${istargetfpath}" >/dev/null;exitcode=$?;pid=$!;wait ${pid}
else
echo -e "${data}" | tee "${istargetfpath}" >/dev/null;exitcode=$?;pid=$!;wait ${pid}
fi
#Print
if [[ ${exitcode} -eq 0 ]]; then
printmsg="${WLN_PRINTMSG_STATUS}: write to ${WLN_LIGHTGREY}${istargetfpath}${WLN_RESETCOLOR}: ${WLN_PRINTMSG_DONE}"
ret=true
else
printmsg="${WLN_PRINTMSG_STATUS}: write to ${WLN_LIGHTGREY}${istargetfpath}${WLN_RESETCOLOR}: ${WLN_PRINTMSG_FAILED}"
ret=false
fi
#Print
DebugPrint "${printmsg}"
#Output
echo "${ret}"
return 0;
}