-
Notifications
You must be signed in to change notification settings - Fork 6
/
SoftAPconfig
executable file
·818 lines (695 loc) · 19.9 KB
/
SoftAPconfig
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
#!/bin/bash
#################################################################################
# Enabling Raspberry Pi to run SmartThings direct-connected device applications
# SoftAP install and configure bash script
# Version 0.20221225
#
# Copyright 2020, 2021, 2022 Todd A. Austin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#################################################################################
# Declare globally used variables
rpimodel=""
osrelease=""
ipOK="no"
ip1=0
ip2=0
ip3=0
ip4=0
static_ip1=0
static_ip2=0
static_ip3=0
static_ip4=0
beg_ip1=0
beg_ip2=0
beg_ip3=0
beg_ip4=0
def_dev="wlan0"
def_addr="192.168.2.1"
def_adrange="192.168.2.2,192.168.2.10"
def_ch="1"
def_cc="US"
currdir="./"
modellist=("a02082" "a22082" "a32082" "a020d3" "a03111" "b03111" "c03111" "b03112" "c03111" "c03112" "9000c1")
ipcount=0
declare -a dhcp_array_dev
declare -a dhcp_array_ip
declare -a dev_array
declare -a type_array
declare -a chan_array
wlan0index=-1
devexistsflag=0
wlan0chan=0
wlan0type=""
devtype=""
# Config file names
dhcpcd="/etc/dhcpcd.conf"
dhcpcdap="/etc/dhcpcd_ap.conf"
hostapd="/etc/hostapd/hostapd.conf"
hostapdconf="/etc/default/hostapd"
dnsmasq="/etc/dnsmasq.conf"
# This function displays a string ($1) for ($2) # of repetitions
disp_sep() {
local v=""
str=$1
num=$2
v=$(printf "%-${num}s" "$str")
echo "${v// /"$str"}"
}
# This function splits an IP address ($1) into 4 global strings ip1-ip4
splitip() {
local array=""
ipstr=$1
IFS='.'
read -a array <<< "$ipstr"
ip1="${array[0]}"
ip2="${array[1]}"
ip3="${array[2]}"
ip4="${array[3]}"
if [ -z "$ip1" ]; then ip1=-1; fi
if [ -z "$ip2" ]; then ip2=-1; fi
if [ -z "$ip3" ]; then ip3=-1; fi
if [ -z "$ip4" ]; then ip4=-1; fi
}
# This function validates IP address ($1), sets global ipOK string to yes or no
checkip() {
inp=$1
splitip "$inp"
ipOK="no"
if [[ ip1 -ge 0 && ip1 -le 255 ]]
then
if [[ ip2 -ge 0 && ip2 -le 255 ]]
then
if [[ ip3 -ge 0 && ip3 -le 255 ]]
then
if [[ ip4 -ge 0 && ip4 -le 255 ]]
then
ipOK="yes"
fi
fi
fi
fi
}
# This function retrieves the current wifi devices from iw dev command
# Output: dev_array, type_array, chan_array, wlan0chan, wlan0type
getdev() {
local result=""
local iflag=-1
local index=0
local iwdev_array=()
result=$(iw dev)
readarray -t iwdev_array <<< $result
lineindex=0;
savIFS=$IFS
IFS=" "
len="${#iwdev_array[@]}"
while [ $lineindex -lt $len ]; do
line=${iwdev_array[lineindex]}
if [ "$line" != "phy#0" ]; then
read -a parms <<< $line
if [[ "${parms[0]}" == *"Interface"* ]]; then
dev_array[index]="${parms[1]}"
lineindex=$((lineindex+1))
while [[ "${iwdev_array[$lineindex]}" != *"Interface"* && $lineindex -lt $len ]]; do
line=${iwdev_array[lineindex]}
read -a parms <<< $line
if [[ "${parms[0]}" == *"type"* ]]; then
type_array[index]=${parms[1]}
else
if [[ "${parms[0]}" == *"channel"* ]]; then
chan_array[index]=${parms[1]}
fi
fi
lineindex=$((lineindex+1))
done
if [[ "${iwdev_array[$lineindex]}" == *"Interface"* ]]; then
lineindex=$((lineindex-1))
index=$((index+1))
fi
fi
fi
lineindex=$((lineindex+1))
done
index=0
iflag=-1
for devitem in "${dev_array[@]}"
do
if [ "$devitem" == "wlan0" ]; then iflag=$index; fi
index=$((index+1))
done
if [ $iflag -ge 0 ]
then
wlan0chan=${chan_array[$iflag]}
wlan0type=${type_array[$iflag]}
fi
IFS=$savIFS
}
# This function retrieves the device/IP pairs from dhcpcd.conf,
# stores ouput in global arrays dhcp_array_dev & dhcp_array_ip,
# and number of found ips in global ipcount
getdhcp() {
local i=0
local ai=0
local count=0
local lines=""
local nxtline=""
local lnum=0
local ltxt=""
ipcount=0
count=$(grep -c "interface" "$dhcpcd")
if [ $count -gt 0 ]
then
lines=$(grep -n "interface" "$dhcpcd" | cut -f1 -d:)
readarray -t lnarray <<< "$lines"
while [[ $i -lt $count ]]
do
lnum="${lnarray[i]}"
ltxt=$(sed -n "$lnum"p "$dhcpcd")
if [[ "$ltxt" != \#* ]]
then
IFS=' '
read -a dvarray <<< "$ltxt"
dhcp_array_dev[ai]="${dvarray[1]}"
nxtline=$((lnum+1))
ltxt=$(sed -n "$nxtline"p "$dhcpcd")
IFS='='
read -a rdarray <<< "$ltxt"
if [[ "${rdarray[0]}" == *"static ip_address"* ]]
then
dhcp_array_ip[ai]="${rdarray[1]}"
ai=$((ai+1))
fi
fi
i=$((i+1))
done
ipcount=$ai
fi
}
######################################################################
#
# - MAIN -
#
######################################################################
[ $UID -eq 0 ] || { echo "This script must be run as root."; exit 1;}
currdir=$(pwd)
if [[ "$currdir" != *"rpi-st-device"* ]]
then
echo -e "Please run this script from the rpi-st-device directory"
exit 1
fi
# Check what hardware we're running on
rpimodel=$(cat /proc/cpuinfo | grep "Revision" | cut -f2 -d" ")
if [[ ! "${modellist[@]}" =~ "${rpimodel}" ]]
then
echo -e "\e[91m\nThis Raspberry Pi model not tested or supported\n\e[0m"
exit -1
fi
# Check what OS version we're running on
osrelease=$(cat /etc/os-release | grep "VERSION_ID" | cut -f2 -d"=")
osrelease="${osrelease%\"}"
osrelease="${osrelease#\"}"
if [ $osrelease -le 8 ]
then
echo -e "\e\91m\nYour OS version is too old; must be Version 8 or later\n\e[0m"
exit -1
fi
# Display notice and get user input to proceed
echo -en "\t"
disp_sep "=" 62
echo
echo -e "\t\t\e[1m\e[93m >>> WARNING!!! <<<"
echo -e "\t\e[0mThis script will prepare your system for SoftAP capability"
echo -e "\t\tdhcpcd.conf will be modified"
echo -e "\t\thostapd and dnsmasq will be installed/updated"
echo
echo -e "\tOriginal files will be saved with 'ORIG' appended to the filename"
echo -e "\tCurrent files will be saved with 'SAVED' appended to the filename"
echo
echo -e "\t\e[95m>> Press \e[97my\e[95m to continue or \e[97mq\e[95m to quit\e[0m"
echo -en "\t"
disp_sep "=" 62
keyin=""
while [[ "$keyin" != "q" && "$keyin" != "y" ]]
do
read -rsn1 keyin
done
echo
if [ "$keyin" == "q" ]; then
exit 1
fi
# Install SoftAP services: hostapd and dnsmasq
#
echo -e "\e[97mInstalling/updating hostapd...\e[0m"
echo
apt install hostapd -y
echo
echo -e "\e[97mInstalling/updating dnsmasq...\e[0m"
echo
apt install dnsmasq -y
echo
echo -e "\e[97mSoftware updates \e[92mCOMPLETE\e[0m"
echo
echo -en "\e[97mDisabling auto-start for these new services...\e[0m"
systemctl unmask hostapd
update-rc.d hostapd disable
update-rc.d dnsmasq disable
# Get the initial default config files
# (there isn't any original hostapd.conf file)
if [ -f "$hostapd" ]
then
cp "$hostapd" /etc/hostapd/hostapd_saved.conf
fi
cp RPIhostapd.conf "$hostapd"
if [ -f "$dnsmasq" ]
then
if [ -f "/etc/dnsmasq.ORIGconf" ]
then
cp "$dnsmasq" /etc/dnsmasq_saved.conf
else
cp "$dnsmasq" /etc/dnsmasq.ORIGconf
fi
fi
cp RPIdnsmasq.conf "$dnsmasq"
# Declare hostapd config file location
sed -i "/DAEMON_CONF=/c\\DAEMON_CONF=$hostapd" "$hostapdconf"
if [ $? -ne 0 ]
then
echo -e "\n\e[91mError setting DAEMON_CONF for hostapd\n\e[0m"
exit -1
fi
echo -e "\e[92m\e[1mCOMPLETE\e[0m"
getinput="y"
while [ "$getinput" == "y" ]
do
# Retrieve current wifi devices from iw dev
getdev
# Initialize user input defaults
cc="$def_cc"
dev="$def_dev"
addr="$def_addr"
adrange="$def_adrange"
if [ $wlan0chan -gt 0 ]; then def_ch=$wlan0chan; fi
ch="$def_ch"
# Time to get input from user
echo
disp_sep "-" 62
instr=""
echo
echo -e "\e[95mPlease provide the following configuration details...\e[0m"
echo
echo -e " (Press ENTER to accept the [\e[96mdefaults\e[0m])"
echo
# Get device name from user and validate
inpOK="no"
while [ "$inpOK" == "no" ]
do
dev="$def_dev"
echo -en "\t\e[0mName for the AP device [\e[96m$dev\e[0m]: \e[96m"
read instr
if [ "$instr" ]; then dev="$instr"; fi
if [ ${#dev} -gt 14 ]
then
echo -e "\t\e[91m** Device name too long; try again"
else
if [ ${dev:0:1} != " " ]
then
if [ ${#dev} -lt 2 ]
then
echo -e "\t\e[91m** Device name too short; try again"
else
# See if device already defined
found=-1
index=0
for adev in "${dev_array[@]}"
do
if [ "$dev" == "$adev" ]; then found=$index; fi
index=$((index+1))
done
if [ $found -lt 0 ]
then
devexistsflag=0
echo -en "\t\e[93m** Warning-this device doesn't exist and will be created. OK? (y/n) "
keyin=""
while [[ "$keyin" != "y" && "$keyin" != "n" ]]
do read -rsn1 keyin; done
echo
if [ "$keyin" == "y" ]; then inpOK="yes"; fi
else
inpOK="yes"
devtype="${type_array[$found]}"
devexistsflag=1
fi
fi
else
dev="$def_dev"
inpOK="yes"
fi
fi
done
# Get static IP address from user and validate
ipOK="no"
while [ "$ipOK" == "no" ]
do
getdhcp
echo -en "\t\e[0mStatic IP address of the AP [\e[96m$addr\e[0m]: \e[96m"
read instr
if [ "$instr" ]; then addr="$instr"; fi
checkip "$addr"
if [[ "$ipOK" == "no" || $ip4 -gt 253 ]]
then
echo -e "\t\e[91m** INVALID IP ADDRESS; TRY AGAIN"
addr="$def_addr"
ipOK="no"
else
# check for possible duplicates from dhcpcd.conf
if [ $ipcount -gt 0 ]
then
found=-1
i=0
for saddr in "${dhcp_array_ip[@]}"
do
if [ "$addr" == "$saddr" ]; then found=$i; fi
i=$((i+1))
done
if [ $found -ge 0 ]
then
if [ "$dev" != "${dhcp_array_dev[$found]}" ]
then
echo -e "\t\e[91m** IP already used in dhcpcd.conf by: \e[1m${dhcp_array_dev[$found]}\e[22m"
addr="$def_addr"
ipOK="no"
fi
fi
fi
fi
done
static_ip1=$ip1
static_ip2=$ip2
static_ip3=$ip3
static_ip4=$ip4
# Calculate recommended ip range based on static address given above
nextip=$((static_ip4+1))
begrange="$ip1.$ip2.$ip3.$nextip"
nextip=$((nextip+10))
if [ $nextip -gt 255 ]; then nextip=255; fi
endrange="$ip1.$ip2.$ip3.$nextip"
def_begrange="$begrange"
def_endrange="$endrange"
echo -e "\t\e[0mIP address range..."
# Get Beginning IP range from user and validate
ipOK="no"
while [ "$ipOK" == "no" ]
do
echo -en "\t\e[0m Beginning address [\e[96m$begrange\e[0m]: \e[96m"
read instr
if [ "$instr" ]; then begrange="$instr"; fi
checkip "$begrange"
if [ "$ipOK" == "yes" ]
then
ipOK="no"
if [ $ip1 -eq $static_ip1 ]
then
if [ $ip2 -eq $static_ip2 ]
then
if [ $ip3 -eq $static_ip3 ]
then
if [ $ip4 -gt $static_ip4 ]
then
if [ $ip4 -le 254 ]
then
ipOK="yes"
consist="yes"
beg_ip1=$ip1
beg_ip2=$ip2
beg_ip3=$ip3
beg_ip4=$ip4
else
echo -e "\t\e[91m** Choose a lower fourth section"
fi
else
echo -e "\t\e[91m** Fourth section not higher than static IP address"
fi
else
echo -e "\t\e[91m** Third section doesn't match static IP address"
fi
else
echo -e "\t\e[91m** Second section doesn't match static IP address"
fi
else
echo -e "\t\e[91m** First section doesn't match static IP address"
fi
else
echo -e "\t** INVALID IP ADDRESS; TRY AGAIN"
fi
if [ "$ipOK" == "no" ]; then begrange="$def_begrange"; fi
done
# Calculate recommended ending ip range
nextip=$((ip4+10))
if [ $nextip -gt 255 ]; then nextip=255; fi
endrange="$ip1.$ip2.$ip3.$nextip"
def_endrange="$endrange"
# Get ending ip range from user and validate
ipOK="no"
while [ "$ipOK" == "no" ]
do
echo -en "\t\e[0m Ending address [\e[96m$endrange\e[0m]: \e[96m"
read instr
if [ "$instr" ]; then endrange="$instr"; fi
checkip "$endrange"
if [ "$ipOK" == "yes" ]
then
ipOK="no"
if [[ $ip1 -eq $beg_ip1 && $ip2 -eq $beg_ip2 && $ip3 -eq $beg_ip3 ]]
then
if [ $ip4 -gt $beg_ip4 ]
then
ipOK="yes"
else
echo -e "\t\e[91m** Invalid IP range"
fi
else
echo -e "\t\e[91m** Range not consistent with beginning IP address **"
fi
else
echo -e "\t\e[91m** INVALID IP ADDRESS; TRY AGAIN"
fi
if [ $ipOK == "no" ]; then endrange="$def_endrange"; fi
done
# Get wifi channel number from user and validate
inpOK="no"
while [ "$inpOK" == "no" ]
do
ch="$def_ch"
echo -en "\t\e[0mWifi channel to use [\e[96m$ch\e[0m]: \e[96m"
read instr
if [ "$instr" ]; then ch="$instr"; fi
if [[ $ch -gt 0 && $ch -le 14 ]]
inpOK="yes"
then
if [[ $wlan0chan -ne 0 && $ch -ne $wlan0chan ]]
then
echo -en "\t\e[93m** Warning: channel not same as current wlan0. Are you sure? (y/n)\e[0m "
keyin=""
while [[ "$keyin" != "y" && "$keyin" != "n" ]]
do read -rsn1 keyin; done
echo
if [ "$keyin" == "n" ]; then inpOK="no"; fi
fi
else
echo -e "\t\e[91m** INVALID CHANNEL NUMBER; TRY AGAIN"
fi
done
# Get country code from user and validate
inpOK="no"
while [ "$inpOK" == "no" ]
do
cc="$def_cc"
echo -en "\t\e[0mCountry code [\e[96m$cc\e[0m]: \e[96m"
read instr
if [ "$instr" ]; then cc="${instr^^}"; fi
if [ ${#cc} -eq 2 ]
then
if [[ "$cc" =~ [A-Z] ]]
then
inpOK="yes"
else
echo -e "\t\e[91m** Must only contain letters"
fi
else
echo -e "\t\e[91m** Invalid country code"
fi
done
# Display summary for final user confirmation
echo -e "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
disp_sep "v-" 32
echo -e "\n\e[97m\e[1mConfirm your selections:\e[0m\n"
echo -e "\t Device name: \e[96m$dev\e[0m"
echo -e "\t Static IP: \e[96m$addr\e[0m"
echo -e "\t IP Range: \e[96m$begrange - $endrange\e[0m"
echo -e "\t Channel: \e[96m$ch\e[0m"
echo -e "\t Country: \e[96m$cc\e[0m"
echo
echo -en "\e[95m >> Press \e[97my\e[95m to accept & continue, \e[97mn\e[95m to re-enter, or \e[97mq\e[95m to quit\e[0m "
keyin=""
while [[ "$keyin" != "y" && "$keyin" != "n" && "$keyin" != "q" ]]
do
read -rsn1 keyin
done
if [[ "$keyin" == "y" || "$keyin" == "q" ]]; then getinput="n"; fi
done
# Done getting config input, now setup config files
echo
adrange="$begrange,$endrange"
if [ "$keyin" == "y" ]
then
disp_sep "- " 32
# If given device name doesn't exist, create it
if [ $devexistsflag -eq 0 ]
then
echo -e "\n\e[97mCreating new wifi AP-type device $dev...\e[0m"
iw phy phy0 interface add $dev type __ap
if [ "$?" == 0 ]
then
echo -e "\n\e[92mCOMPLETE:\e[0m"
systemctl restart networking # not sure this is needed
iw dev
devtype="AP"
echo
else
echo -e "\n\e[91m ^^Error encountered (maybe AP type device already exists)\e[0m"
echo -en "\n\t\e[95mContinue anyway? (y/n) \e[0m"
keyin=""
while [[ "$keyin" != "y" && "$keyin" != "n" ]]
do read -rsn1 keyin; done
if [ "$keyin" == "n" ]
then
echo -e "\n\n\e[0m** EXITING **\n"
exit 1
fi
echo
fi
fi
# Set up dhcpcd_ap.conf file
echo -en "\n\e[97mUpdating dhcpcd AP configuration...\e[0m"
if [ -f "$dhcpcd" ]; then
cp "$dhcpcd" "$dhcpcdap"
else
echo -e "\n\e[91m/etc/dhcpcd.conf is not found; cannot continue"
exit -1
fi
# Check to see if there is already an entry in the file for this dev
line=$(sed -n "/interface $dev/=" "$dhcpcdap")
# If not, then append the new config lines
if [ -z "$line" ]
then
echo -e "\ninterface $dev" >> "$dhcpcdap"
echo -e "\tstatic ip_address=$addr" >> "$dhcpcdap"
echo -e "\tnohook wpa_supplicant" >> "$dhcpcdap"
# Otherwise we need to update existing entry
# ...first find static ip_address line for the device
else
nextln=$((line+1))
result=$(grep -n "static ip_address" "$dhcpcdap" | cut -f1 -d:)
readarray -t array <<< "$result"
i=0
l="${#array[@]}"
fflag=0
while [[ $i -lt $l && $fflag == 0 ]]
do
if [ "${array[i]}" == "$nextln" ]; then fflag=1; fi
i=$((i+1))
done
if [ $fflag -eq 1 ]
then
sed -i "$nextln""s/.*/\tstatic ip_address=$addr/" "$dhcpcdap"
if [ $? != 0 ]; then
errnum=$?
echo -e "\n\n\e[91m** Unexpected sed error# $errnum: Could not update $dhcpcdap\e[0m\n"
exit 1
fi
nextln=$((nextln+1))
sed -i "$nextln""s/.*/\tnohook wpa_supplicant/" "$dhcpcdap"
if [ $? != 0 ]; then
errnum=$?
echo -e "\n\n\e[91m** Unexpected sed error# $errnum: Could not update $dhcpcdap\e[0m\n"
exit 1
fi
else
echo -e "\n\n\t\e[93m** Unexpected contents in dhcpcd.conf; please edit manually **"
echo -e "\n\t\e[33mPress any key to start editor\e[39m"
read -rsn1 keyin
nano "$dhcpcdap"
fi # end if updating existing
fi # end modifying dhcpcd
# check we don't have duplicate IP addresses defined in dhcpcd.conf
# (should never be the case based on previous validation)
count=$(grep -c "$addr" "$dhcpcdap")
if [ $count -gt 1 ]
then
echo -e "\n\t\e[91m** You have duplicate IP addresses in dhcpcd_ap.conf"
echo -e "\n\t\e[39m Edit manually to resolve conflict"
echo -en "\t\e[33m Press any key to start editor...\e[39m"
read -rsn1 keyin
nano "$dhcpcdap"
echo
else
echo -e "\e[92mCOMPLETE\e[0m"
fi
# Configure hostapd and dnsmasq config files
echo
disp_sep "- " 32
echo -en "\n\e[97mUpdating SoftAP config files...\e[0m"
sed -i "/^country_code/c\\country_code=$cc" "$hostapd"
sed -i "/^interface/c\\interface=$dev" "$hostapd"
sed -i "/^channel/c\\channel=$ch" "$hostapd"
sed -i "/^interface/c\\interface=$dev" "$dnsmasq"
sed -i "/^dhcp-range/c\\dhcp-range=$adrange,12h" "$dnsmasq"
sed -i "/^address/c\\address=/gw.wlan/$addr" "$dnsmasq"
echo -e "\e[92mCOMPLETE\e[0m"
echo -e "\e[32m"
disp_sep "*" 25
echo -e "\e[22mCONFIGURATION IS ALL DONE"
disp_sep "*" 25
echo
# WRAP UP WITH OPTIONAL HOSTAPD TEST
echo -e "\e[97mWould you like to manually start hostapd now for testing?"
echo -e "\e[91m WARNING: the wireless connection will be interrupted; proceed only if this is a direct terminal or ethernet connection\e[0m"
echo -en "\n\e[95m>> Proceed? (y/n) "
keyin=""
while [[ "$keyin" != "y" && "$keyin" != "n" ]]
do
read -rsn1 keyin
done
echo
if [ "$keyin" == "y" ]
then
echo
echo -e "\e[93mSwitching to wireless Access Point mode"
cp "$dhcpcd" /etc/dhcpcd_tmp.conf
cp "$dhcpcdap" "$dhcpcd"
systemctl restart dhcpcd
echo -e "\n\e[0m * Starting hostapd; \e[97m\e[1mCtrl-c\e[22m to exit hostapd when done *\e[0m"
echo
/usr/sbin/hostapd "$hostapd"
echo
echo -e "\e[93mSwitching back to wireless client mode"
cp /etc/dhcpcd_tmp.conf "$dhcpcd"
rm /etc/dhcpcd_tmp.conf
systemctl restart dhcpcd
fi
else
exit 1
fi
echo -e "\n\n\e[97m*** DONE ***\e[0m\n"
exit 0