-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.1-Install-Wifi-Adhoc.bash
543 lines (414 loc) · 15.3 KB
/
3.1-Install-Wifi-Adhoc.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
#!/bin/bash
## #############################################################################
## ABOUT:
## This script will configure wifi ad-hoc network using hostapd and dnsmasq
## This script will write config scripts for:
## * Hostapd
## * Dnsmasq
## * SSH Server
## * Network interface names and IP
##
## Post installation feature:
## Use a remote client to ssh into this Debian server.
##
## Guidance:
## https://www.debian.org/doc/manuals/debian-reference/ch05.en.html
## #############################################################################
function Tput_Colors {
## Foreground Color using ANSI escape
FG_BLACK=$(tput setaf 0)
FG_RED=$(tput setaf 1)
FG_GREEN=$(tput setaf 2)
FG_YELLOW=$(tput setaf 3)
FG_BLUE=$(tput setaf 4)
FG_MAGENTA=$(tput setaf 5)
FG_CYAN=$(tput setaf 6)
FG_WHITE=$(tput setaf 7)
FG_NoColor=$(tput sgr0)
## Background Color using ANSI escape
BG_BLACK=$(tput setab 0)
BG_RED=$(tput setab 1)
BG_GREEN=$(tput setab 2)
BG_YELLOW=$(tput setab 3)
BG_BLUE=$(tput setab 4)
BG_MAGENTA=$(tput setab 5)
BG_CYAN=$(tput setab 6)
BG_WHITE=$(tput setab 7)
BG_NoColor=$(tput sgr0)
## set mode using ANSI escape
MODE_BOLD=$(tput bold)
MODE_DIM=$(tput dim)
MODE_BEGIN_UNDERLINE=$(tput smul)
MODE_EXIT_UNDERLINE=$(tput rmul)
MODE_REVERSE=$(tput rev)
MODE_ENTER_STANDOUT=$(tput smso)
MODE_EXIT_STANDOUT=$(tput rmso)
# clear styles using ANSI escape
STYLES_OFF=$(tput sgr0)
FGBG_NoColor=$(tput sgr0)
}
function About_Message {
## Cover page about information
clear
echo -e "$FG_YELLOW $MODE_BOLD
## #############################################################################
## ABOUT:
## This script will configure wifi ad-hoc network using hostapd and dnsmasq
## This script will write config scripts for:
## * Hostapd
## * Dnsmasq
## * SSH Server
## * Network interface names and IP
##
## Post installation feature:
## Use a remote client to ssh into this Debian server.
##
## Guidance:
## https://www.debian.org/doc/manuals/debian-reference/ch05.en.html
## #############################################################################
$STYLES_OFF"
read -p "Press enter to continue ..." pauseEnter
echo ""
}
function Write_Grub_Defaults {
## Make wifi = wlan0, and ethernet=eth0 instead of however else the kernel decided to name them.
echo "$FGBG_NoColor---"
echo "$FG_YELLOW"
echo -e "Do you want to update grub to recognize Wifi and Ethernet interfaces as \"wlan0\" and \"eth0\", instead of however else the kernel decided to name them by default?\n\tNote:\n\t\tComputer will reboot.\n $FGBG_NoColor"
read -e -p "Update the /etc/default/grub file? [ Y/n ]: " -i "n" yn
case $yn in
[Yy]* )
echo -e "\nEditing the /etc/default/grub file ...\n"
if [ -f /etc/default/grub ]; then
sudo cp /etc/default/grub /etc/default/grub.backup.$(date +%d%b%Y_%H%M%S)
sleep 3s
fi
## replace the line:
## GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
## with
## GRUB_CMDLINE_LINUX_DEFAULT="net.ifnames=0"
sudo sed '/quiet/c\GRUB_CMDLINE_LINUX_DEFAULT="net.ifnames=0"' /etc/default/grub > /tmp/grub.temp
sleep 3s
sudo mv /tmp/grub.temp /etc/default/grub
sleep 3s
sudo update-grub
echo -e "\nDebian will need to restart to reinitialize everything.\n"
read -p "Press ENTER to reboot..."
sudo reboot
;;
* )
echo -e "\n$FG_MAGENTA\tContinuing without editing the interface names... \
\n\tWriting a delay script for ethernet connection at boot.$STYLES_OFF"
;;
esac
}
function Install_Packages {
sudo apt install -y firmware-linux-free
sudo apt install -y firmware-linux-nonfree
sudo apt install -y firmware-iwlwifi
sudo apt install -y resolvconf
sudo apt install -y dnsmasq
sudo apt install -y iptables
sudo apt install -y hostapd
sudo apt install -y ifplugd
sudo apt install -y openssh-server
}
function Write_Network_Interfaces {
mywlan=$1
myeth=$2
tempFile=~/interfaces
#destinationFile=/etc/network/interfaces
destinationFile=/etc/network/interfaces.d/setup
echo "## $destinationFile
## #############################################################################
## Class network addresses net mask net mask/bits # of subnets
## A 10.x.x.x 255.0.0.0 /8 1
## B 172.16.x.x — 172.31.x.x 255.255.0.0 /16 16
## C 192.168.0.x — 192.168.255.x 255.255.255.0 /24 256
## #############################################################################
## Note:
## If one of these addresses is assigned to a host, then that host must not
## access the Internet directly but must access it through a gateway that acts
## as a proxy for individual services
## #############################################################################
auto lo
iface lo inet loopback
auto $myeth
#allow-auto $myeth
#allow-hotplug $myeth
iface $myeth inet dhcp
#iface $myeth inet static
# address 192.168.1.100
# netmask 255.255.255.0
# broadcast 192.168.1.255
# gateway 192.168.1.1
# #dns-domain localhost
# #dns-nameservers 192.168.1.2
auto $mywlan
iface $mywlan inet static
address 10.42.0.1
netmask 255.0.0.0
wireless-channel 1
wireless-mod ad-hoc
## Wifi using the wpasupplicant package
#wpa-ssid ATT123
#wpa-psk MYPASSPHRASE" > $tempFile
echo "$FG_CYAN"
echo -e "\n##\n## $tempFile\n##"
cat $tempFile
echo -e "\n $STYLES_OFF"
read -e -p "Apply config file $tempFile to $destinationFile? [ y/N ]: " -i "n" yn
case $yn in
[Yy]* )
if [ -f $destinationFile ]; then
sudo cp $destinationFile $destinationFile.backup.$(date +%d%b%Y_%H%M%S)
fi
sudo mv $tempFile $destinationFile
;;
esac
}
function Write_Hostapd_Conf {
## https://wiki.debian.org/hostap
## aptitude install hostap-utils wireless-tools
mywlan=$1
myssid=$2
mypassword=$3
tempFile=~/hostapd.conf
destinationFile=/etc/hostapd/hostapd.conf
echo "## $destinationFile
interface=$mywlan
driver=nl80211
ssid=$myssid
hw_mode=g
channel=1
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=$mypassword
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP" > $tempFile
echo "$FG_CYAN"
echo -e "\n##\n## $tempFile\n##"
cat $tempFile
echo -e "\n $STYLES_OFF"
read -e -p "Apply config file $tempFile to $destinationFile? [ y/N ]: " -i "n" yn
case $yn in
[Yy]* )
if [ -f $destinationFile ]; then
sudo cp $destinationFile $destinationFile.backup.$(date +%d%b%Y_%H%M%S)
fi
sudo mv $tempFile $destinationFile
;;
esac
}
function Write_Dnsmasq_Conf {
## https://wiki.debian.org/dnsmasq
## apt-get install dnsmasq
mywlan=$1
myeth=$2
tempFile=~/dnsmasq.conf
destinationFile=/etc/dnsmasq.conf
echo "## $destinationFile
interface=$mywlan
## Ip range for clients
#dhcp-range=10.42.0.0,10.42.0.8,12h
dhcp-range=$mywlan,10.42.0.1,10.42.0.24,12h
## Router ip
#dhcp-option=3,10.42.0.1
" > $tempFile
echo "$FG_CYAN"
echo -e "\n##\n## $tempFile\n##"
cat $tempFile
echo -e "\n $STYLES_OFF"
read -e -p "Apply config file $tempFile to $destinationFile? [ y/N ]: " -i "n" yn
case $yn in
[Yy]* )
if [ -f $destinationFile ]; then
sudo cp $destinationFile $destinationFile.backup.$(date +%d%b%Y_%H%M%S)
sudo cp /etc/default/hostapd /etc/default/hostapd.backup.$(date +%d%b%Y_%H%M%S)
fi
sudo mv $tempFile $destinationFile
sudo sed '/DAEMON_CONF=""/c\DAEMON_CONF="/etc/hostapd/hostapd.conf"' /etc/default/hostapd > ~/default_hostapd
if [ -f ~/init_d_hostapd ]; then
sudo mv ~/default_hostapd /etc/default/hostapd
fi
;;
esac
}
function Init_Hotspot_Interfaces {
mywlan=$1
sudo ip link set $mywlan up
sudo systemctl stop dnsmasq
sudo systemctl stop hostapd
sudo systemctl unmask hostapd
sudo systemctl enable hostapd
sudo systemctl restart hostapd
sudo systemctl enable dnsmasq
sudo systemctl restart dnsmasq
}
function Customize_HostSshd {
sleep 4s
clear
echo "$FG_CYAN"
echo "Customize SSH Configuration: "
echo ""
echo "Customize /etc/ssh/sshd_config or use defaults?"
echo ' "y" to manually customize'
echo ' "n" to use preset defaults'
echo ""
read -e -p "Edit the ssh config file, sshd_config? [y/N] : " -i "y" yn
myListenAddresses=$(ip -o -4 addr show | awk '{print $4}' | cut -d/ -f1 | awk '{print "ListenAddress "$1}' )
case $yn in
[Yy]* )
sudo systemctl stop sshd
read -e -p "Port ( 22 ) : " -i "22" myPort
## Listen address
echo -e "${FG_RED}${MODE_BOLD}SSHD Listen address candidates:\n"
ip -o -4 addr show
echo "${STYLES_OFF}${FG_CYAN}"
read -e -p "ListenAddress ( 10.42.0.1 ) : " -i "10.42.0.1" myListenAddress
read -e -p "PermitRootLogin ( no ) : " -i "no" myPermitRootLogin
read -e -p "AllowUsers ( mezcel ) : " -i "mezcel" myAllowUsers
read -e -p "AuthorizedKeysFile ( .ssh/authorized_keys ) : " -i ".ssh/authorized_keys" myAuthorizedKeysFile
read -e -p "ChallengeResponseAuthentication (no) : " -i "no" myChallengeResponseAuthentication
read -e -p "UsePAM ( yes ) : " -i "yes" myUsePAM
read -e -p "PrintMotd ( no ) : " -i "no" myPrintMotd
read -e -p "Subsystem ( sftp /usr/lib/ssh/sftp-server ) : " -i "sftp /usr/lib/ssh/sftp-server" mySubsystem
#read -e -p "X11Forwarding ( yes ) : " -i "yes" myX11Forwarding
#read -e -p "X11UseLocalhost ( yes ) : " -i "yes" myX11UseLocalhost
if [ -f /etc/ssh/sshd_config ]; then
sudo cp /etc/hostname /etc/hostname.backup.$(date +%d%b%Y_%H%M%S)
fi
temporaryConfig=~/sshd_config.temp
echo "Port $myPort" >> $temporaryConfig
echo "## Added localhost just incase sshd was started without a network infrastructure." >> $temporaryConfig
#echo "ListenAddress 127.0.0.1" >> $temporaryConfig
#echo "#ListenAddress 192.168.0.100" >> $temporaryConfig
#echo "#ListenAddress 10.42.0.1" >> $temporaryConfig
echo -e "$myListenAddresses" >> $temporaryConfig
echo "ListenAddress $myListenAddress" >> $temporaryConfig
echo "PermitRootLogin $myPermitRootLogin" >> $temporaryConfig
echo "AllowUsers $myAllowUsers" >> $temporaryConfig
echo "AuthorizedKeysFile $myAuthorizedKeysFile" >> $temporaryConfig
echo "ChallengeResponseAuthentication $myChallengeResponseAuthentication" >> $temporaryConfig
echo "UsePAM $myUsePAM" >> $temporaryConfig
echo "PrintMotd $myPrintMotd" >> $temporaryConfig
echo "Subsystem $mySubsystem" >> $temporaryConfig
#echo "X11Forwarding $myX11Forwarding" >> $temporaryConfig
#echo "X11UseLocalhost $myX11UseLocalhost" >> $temporaryConfig
echo "#MaxAuthTries 4" >> $temporaryConfig
echo "#ClientAliveInterval 180" >> $temporaryConfig
if [ -f $temporaryConfig ]; then
echo -e "$FG_MAGENTA\tWriting /etc/ssh/sshd_config ... $STYLES_OFF"
sudo mv $temporaryConfig /etc/ssh/sshd_config
sleep 3s
## Preview Sshd Configs
echo "$BG_MAGENTA"
echo -n "## Your SSHD Config.\n\tTake note of the Listen Addresses.\n $MODE_BOLD"
sudo cat /etc/ssh/sshd_config
echo "$STYLES_OFF"
read -p "Press Enter to continue ..." pauseEnter
fi
;;
* )
echo -e ""
;;
esac
echo "$STYLES_OFF"
}
function Delay_SSHD_Interface {
## Workaround to prevent errors related to sshd.service starting before a network is available
## sudo systemctl edit sshd
sudo mkdir -p /etc/systemd/system/ssh.service.d
temporaryConfig=~/wait_conf.temp
echo '[Unit]' > $temporaryConfig
echo 'Wants=network-online.target' >> $temporaryConfig
echo 'After=network-online.target' >> $temporaryConfig
if [ -f $temporaryConfig ]; then
echo -e "$FG_MAGENTA\tWriting /etc/systemd/system/ssh.service.d/wait.conf ... $STYLES_OFF"
sudo mv $temporaryConfig /etc/systemd/system/ssh.service.d/wait.conf
sleep 3s
echo -e "$FG_MAGENTA\tWriting daemon-reload ... $STYLES_OFF"
sudo systemctl daemon-reload
sleep 3s
fi
}
function Write_Configs {
clear
echo -e "\n## Current interface names and ip's ##\n $FG_YELLOW"
ip a
echo "$STYLES_OFF"
## ethernet interface name
myeth=$(ls /sys/class/net | grep -E "eth")
if [ -z $myeth ]; then
myeth=$(ls /sys/class/net | grep -E "enp")
if [ -z $myeth ]; then
myeth=$(ls /sys/class/net | grep -E "en")
fi
fi
if [ -z $myeth ]; then
myeth=eth0
fi
read -e -p "Enter eth interface name. [ $myeth ]: " -i "$myeth" myeth
## wireless interface name
mywlan=$(ls /sys/class/net | grep -E "wl")
if [ -z $mywlan ]; then
mywlan=wlan0
fi
read -e -p "Enter wlan interface name. [ $mywlan ]: " -i "$mywlan" mywlan
myssid=$(hostname)-hostapd
read -e -p "Enter a hotspot SSID name. [ $myssid ]: " -i "$myssid" myssid
mypassword=password1234
read -e -p "Enter a SSID password for $myssid. [ $mypassword ]: " -i "$mypassword" mypassword
######
Write_Network_Interfaces $mywlan $myeth
Write_Hostapd_Conf $mywlan $myssid $mypassword
Write_Dnsmasq_Conf $mywlan $myeth
Init_Hotspot_Interfaces $mywlan
Customize_HostSshd
#Delay_SSHD_Interface
}
function ShellInABoxD {
echo ""
promptQuestion="Do you want to install \"shellinabox\"? [ y/N ]: "
yn=n
sleep 1s
read -e -p "$promptQuestion" -i "$yn" yn
case $yn in
[Yy]* )
echo -e "\n Installing ...\n"
sleep 1s
sudo apt install -y shellinabox
;;
esac
echo ""
promptQuestion="Do you want to \"enable\" shellinabox? [ y/N ]: "
yn=n
read -e -p "$promptQuestion" -i "$yn" yn
case $yn in
[Yy]* )
echo -e "\n Enabling ...\n"
sleep 1s
sudo systemctl enable shellinabox.service
echo -e "\nNOTE: \
\n\tThe default shellinabox url will be at port 4200. \
\n\tExample: https://10.42.0.1:4200 \n"
read -p "Press ENTER to continue ..." pauseEnter
;;
esac
}
function MAIN_SCRIPT {
## Main script
About_Message
Write_Grub_Defaults
Install_Packages
Write_Configs
}
## #############################################################################
## RUN
## #############################################################################
Tput_Colors
MAIN_SCRIPT
ShellInABoxD