forked from treehouses/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.sh
executable file
·956 lines (872 loc) · 28.7 KB
/
cli.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
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
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
#!/bin/bash
SCRIPTPATH=$(realpath "$0")
SCRIPTFOLDER=$(dirname "$SCRIPTPATH")
TEMPLATES="$SCRIPTFOLDER/templates"
function help {
case $1 in
rename)
echo ""
echo "Usage: $(basename "$0") rename <hostname>"
echo ""
echo "Changes the hostname"
echo ""
echo "Example:"
echo " $(basename "$0") rename rpi"
echo " Sets the hostname to 'rpi'."
echo ""
;;
password)
echo ""
echo "Usage: $(basename "$0") password <password>"
echo ""
echo "Changes the password for 'pi' user"
echo ""
echo "Example:"
echo " $(basename "$0") password ABC"
echo " Sets the password for 'pi' user to 'ABC'."
echo ""
;;
sshkeyadd)
echo ""
echo "Usage: $(basename "$0") sshkeyadd <public_key>"
echo ""
echo "Adds a public key to 'pi' and 'root' user's authorized_keys"
echo ""
echo "Example:"
echo " $(basename "$0") sshkeyadd \"\""
echo " The public key between quotes will be added to authorized_keys so user can login without password for both 'pi' and 'root' user."
echo ""
;;
hotspot)
echo ""
echo "Usage: treehouses hotspot <ESSID> [password]"
echo ""
echo "Creates a mobile hotspot"
echo ""
echo "Examples:"
echo " treehouses hotspot hotspotname hotspotpassword"
echo " Creates a hotspot with ESSID 'hotspotname' and password 'hotspotpassword'."
echo ""
echo " treehouses hotspot hotspotname"
echo " Creates an open hotspot with ESSID 'hotspotname'."
echo ""
;;
version)
echo ""
echo "Usage: $(basename "$0") version"
echo ""
echo "Returns the version of $(basename "$0") command"
echo ""
echo "Example:"
echo " $(basename "$0") version"
echo " Prints the version of $(basename "$0") currently installed."
echo ""
;;
detectrpi)
echo ""
echo "Usage: $(basename "$0") detectrpi"
echo ""
echo "Detects the hardware version"
echo ""
echo "Example:"
echo " $(basename "$0") detectrpi"
echo " Prints the model number"
;;
ethernet)
echo ""
echo "Usage: $(basename "$0") ethernet <ip> <mask> <gateway> <dns>"
echo ""
echo "Configures ethernet interface (eth0) to use a static ip address"
echo ""
echo "Example:"
echo " $(basename "$0") ethernet 192.168.1.101 255.255.255.0 192.168.1.1 9.9.9.9"
echo " Sets the ethernet interface IP address to 192.168.1.101, mask 255.255.255.0, gateway 192.168.1.1, DNS 9.9.9.9"
echo ""
;;
wifi)
echo ""
echo "Usage: $(basename "$0") wifi <ESSID> [password]"
echo ""
echo "Connects to a wifi network"
echo ""
echo "Example:"
echo " $(basename "$0") wifi home homewifipassword"
echo " Connects to a wifi network named 'home' with password 'homewifipassword'."
echo ""
echo " $(basename "$0") wifi yourwifiname"
echo " Connects to an open wifi network named 'home'."
echo ""
;;
staticwifi)
echo ""
echo "Usage: $(basename "$0") staticwifi <ip> <mask> <gateway> <dns> <ESSID> [password]"
echo ""
echo "Configures wifi interface (wlan0) to use a static ip address"
echo ""
echo "Examples:"
echo " $(basename "$0") staticwifi 192.168.1.101 255.255.255.0 192.168.1.1 9.9.9.9 home homewifipassword"
echo " Connects to wifi named 'home' with password 'homewifipassword' and sets the wifi interface IP address to 192.160.1.1, mask 255.255.255.0, gateway 192.168.1.1, DNS 9.9.9.9"
echo ""
echo " $(basename "$0") staticwifi 192.168.1.101 255.255.255.0 192.168.1.1 9.9.9.9 home"
echo " Connects to an open wifi named 'home' and sets the wifi interface IP address to 192.160.1.1, mask 255.255.255.0, gateway 192.168.1.1, DNS 9.9.9.9"
echo ""
;;
default)
echo ""
echo "Usage: $(basename "$0") default"
echo ""
echo "Resets the raspberry pi to default"
echo ""
echo "Example:"
echo " $(basename "$0") default"
echo " This will allow you to return back to the original configuration for all the services and settings which were set for the image when it was first installed."
echo " This will not delete any new files you created."
echo ""
;;
upgrade)
echo ""
echo "Usage: $(basename "$0") upgrade [-f]"
echo ""
echo "Upgrades $(basename "$0") package using npm"
echo ""
echo "Example:"
echo " $(basename "$0") upgrade"
echo " This will upgrade the $(basename "$0") package using npm and will try to install the latest version of $(basename "$0") running on your system"
echo " If the latest version if installed it will not try to reinstall it"
echo ""
echo " $(basename "$0") upgrade -f"
echo " This will do the same as upgrade, but the version will not be checked. Meaning that this will force the installation of the latest version"
echo ""
;;
bridge)
echo ""
echo "Usage: $(basename "$0") bridge <WifiESSID> <HotspotESSID> [WifiPassword] [HotspotPassword]"
echo ""
echo "Bridges the wlan0 interface to ap0, creating a hotspot with desired configuration."
echo ""
echo "Examples:"
echo " $(basename "$0") bridge MyWifi MyHotspot"
echo " This will connect to 'MyWifi' which is an open essid, and create an open hotspot called 'MyHotspot'"
echo ""
echo " $(basename "$0") bridge MyWifi MyHotspot 12345678"
echo " This will connect to 'MyWifi' which has a password '12345678', and create an open hotspot called 'MyHotspot'"
echo ""
echo " $(basename "$0") bridge MyWifi MyHotspot 12345678 hotspot123"
echo " This will connect to 'MyWifi' which has a password '12345678', and create a password hotspot called 'MyHotspot' with the password 'hotspot123'"
echo ""
echo " $(basename "$0") bridge MyWifi MyHotspot \"\" 12345678"
echo " This will connect to 'MyWifi' which is an open essid, and create a password hotspot called 'MyHotspot' with the password '12345678'"
echo ""
;;
timezone)
echo ""
echo "Usage: $(basename "$0") timezone <timezone>"
echo ""
echo "Sets the system timezone"
echo ""
echo "Example:"
echo " $(basename "$0") timezone Etc/GMT-3"
echo " This will set the raspberry pi time to GMT-3"
echo " When using Etc/GMT you can specify the offset, from GMT-14 up to GMT+12"
echo " Available timezones are inside /usr/share/zoneinfo/"
echo ""
;;
container)
echo ""
echo "Usage: $(basename "$0") container <docker|balena|none>"
echo ""
echo "Starts the desired container."
echo ""
echo "Example:"
echo " $(basename "$0") container docker"
echo " This will start and enable the docker service. The balena service will be stopped and disabled."
echo ""
echo " $(basename "$0") container docker"
echo " This will start and enable the balena service. The docker service will be stopped and disabled."
echo ""
echo " $(basename "$0") container none"
echo " This will stop and disable the balena and docker service."
echo ""
;;
expandfs)
echo ""
echo "Usage: $(basename "$0") expandfs"
echo ""
echo "Expands the partition of the raspberry pi image to use the whole disk"
echo ""
echo "Example:"
echo " $(basename "$0") expandfs"
echo " The partition of the SD card in which the raspberry pi image is stored will be expanded to match the available space on the SD card after a reboot"
echo ""
;;
vnc)
echo ""
echo "Usage: $(basename "$0") vnc <on|off>"
echo ""
echo "Enables or disables the VNC server service"
echo ""
echo "Example:"
echo " $(basename "$0") vnc on"
echo " The VNC service will be enabled. This will allow devices on your network to be able to connect to the raspberry pi using VNC viewer."
echo ""
echo " $(basename "$0") vnc off"
echo " The VNC service will be disabled."
echo ""
;;
ssh)
echo ""
echo "Usage: $(basename "$0") ssh <on|off>"
echo ""
echo "Enables or disables the SSH service"
echo ""
echo "Example:"
echo " $(basename "$0") ssh on"
echo " The SSH service will be enabled. This will allow devices on your network to be able to connect to the raspberry pi using SSH."
echo ""
echo " $(basename "$0") ssh off"
echo " The SSH service will be disabled."
echo ""
;;
locale)
echo ""
echo "Usage: $(basename "$0") locale <locale>"
echo ""
echo "Sets the system locale"
echo ""
echo "Example:"
echo " $(basename "$0") local en_US"
echo " This will set the raspberry pi locale to en_US."
echo " The supported languages are in /usr/share/i18n/SUPPORTED"
echo ""
;;
bluetooth)
echo ""
echo "Usage: $(basename "$0") bluetooth <on|off>"
echo ""
echo "Switches between hotspot / regular bluetooth mode"
echo ""
echo "Example:"
echo " $(basename "$0") bluetooth on"
echo " This will start the bluetooth server, which lets the user control the raspberry pi using the mobile app."
echo ""
echo " $(basename "$0") bluetooth off"
echo " This will stop the bluetooth server, and bring everything back to regular mode."
echo ""
;;
*)
echo "Usage: $(basename "$0")"
echo
echo " expandfs expands the partition of the RPI image to the maximum of the SDcard"
echo " rename <hostname> changes hostname"
echo " password <password> changes the password for 'pi' user"
echo " sshkeyadd <public_key> adds a public key to 'pi' and 'root' user's authorized_keys"
echo " version returns the version of $(basename "$0") command"
echo " detectrpi detects the hardware version of a raspberry pi"
echo " ethernet <ip> <mask> <gateway> <dns> configures rpi network interface to a static ip address"
echo " wifi <ESSID> [password] connects to a wifi network"
echo " staticwifi <ip> <mask> <gateway> <dns> configures rpi wifi interface to a static ip address"
echo " <ESSID> [password]"
echo " bridge <ESSID> <hotspotESSID> configures the rpi to bridge the wlan interface over a hotspot"
echo " [password] [hotspotPassword]"
echo " container <none|docker|balena> enables (and start) the desired container"
echo " bluetooth <on|off> switches between bluetooth hotspot mode / regular bluetooth and starts the service"
echo " hotspot <ESSID> [password] creates a mobile hotspot"
echo " timezone <timezone> sets the timezone of the system"
echo " locale <locale> sets the system locale"
echo " ssh <on|off> enables or disables the ssh service"
echo " vnc <on|off> enables or disables the vnc server service"
echo " default sets a raspbian back to default configuration"
echo " upgrade upgrades $(basename "$0") package using npm"
echo
;;
esac
}
function start_service {
if [ "$(systemctl is-active "$1" 2>/dev/null)" = "inactive" ]
then
systemctl start "$1" >/dev/null 2>/dev/null
fi
}
function restart_service {
systemctl stop "$1" >/dev/null 2>/dev/null
systemctl start "$1" >/dev/null 2>/dev/null
}
function stop_service {
if [ "$(systemctl is-active "$1" 2>/dev/null)" = "active" ]
then
systemctl stop "$1" >/dev/null 2>/dev/null
fi
}
function enable_service {
if [ "$(systemctl is-enabled "$1" 2>/dev/null)" = "disabled" ]
then
systemctl enable "$1" >/dev/null 2>/dev/null
fi
}
function disable_service {
if [ "$(systemctl is-enabled "$1" 2>/dev/null)" = "enabled" ]
then
systemctl disable "$1" >/dev/null 2>/dev/null
fi
}
function expandfs () {
# expandfs is way too complex, it should be handled by raspi-config
raspi-config --expand-rootfs >/dev/null 2>/dev/null
echo "Success: the filesystem will be expanded on the next reboot"
}
function rename () {
CURRENT_HOSTNAME=$(< /etc/hostname tr -d " \\t\\n\\r")
echo "$1" > /etc/hostname
sed -i "s/127.0.1.1.*$CURRENT_HOSTNAME/127.0.1.1\\t$1/g" /etc/hosts
hostname "$1"
echo "Success: the hostname has been modified"
}
function password () {
echo "pi:$1" | chpasswd
echo "Success: the password has been changed"
}
function sshkeyadd () {
mkdir -p /root/.ssh /home/pi/.ssh
chmod 700 /root/.ssh /home/pi/.ssh
echo "$@" >> /home/pi/.ssh/authorized_keys
chmod 600 /home/pi/.ssh/authorized_keys
chown -R pi:pi /home/pi/.ssh
echo "$@" >> /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
echo "====== Added to 'pi' and 'root' user's authorized_keys ======"
echo "$@"
}
function version {
node -p "require('$SCRIPTFOLDER/package.json').version"
}
function checkroot {
if [ "$(id -u)" -ne 0 ];
then
echo "Error: Must be run with root permissions"
exit 1
fi
}
function detectrpi {
declare -A rpimodels
rpimodels["Beta"]="BETA"
rpimodels["0002"]="RPIB"
rpimodels["0003"]="RPIB"
rpimodels["0004"]="RPIB"
rpimodels["0005"]="RPIB"
rpimodels["0006"]="RPIB"
rpimodels["0007"]="RPIA"
rpimodels["0008"]="RPIA"
rpimodels["0009"]="RPIA"
rpimodels["000d"]="RPIB"
rpimodels["000e"]="RPIB"
rpimodels["000f"]="RPIB"
rpimodels["0010"]="RPIB+"
rpimodels["0011"]="CM"
rpimodels["0012"]="RPIA+"
rpimodels["0013"]="RPIB+"
rpimodels["0014"]="CM"
rpimodels["0015"]="RPIA+"
rpimodels["a01040"]="RPI2B"
rpimodels["a01041"]="RPI2B"
rpimodels["a21041"]="RPI2B"
rpimodels["a22042"]="RPI2B"
rpimodels["900021"]="RPIA+"
rpimodels["900032"]="RPIB+"
rpimodels["900092"]="RPIZ"
rpimodels["900093"]="RPIZ"
rpimodels["920093"]="RPIZ"
rpimodels["9000c1"]="RPIZW"
rpimodels["a02082"]="RPI3B"
rpimodels["a020a0"]="CM3"
rpimodels["a22082"]="RPI3B"
rpimodels["a32082"]="RPI3B"
rpimodels["a020d3"]="RPI3B+"
rpimodel=$(grep Revision /proc/cpuinfo | sed 's/.* //g' | tr -d '\n')
echo ${rpimodels[$rpimodel]}
}
function restart_wifi {
systemctl disable hostapd || true
systemctl disable dnsmasq || true
restart_service dhcpcd
stop_service hostapd
stop_service dnsmasq
ifup wlan0 || true
ifdown wlan0 || true
sleep 1
ifup wlan0 || true
}
function wifi {
wifinetwork=$1
wifipassword=$2
if [ -n "$wifipassword" ]
then
if [ ${#wifipassword} -lt 8 ]
then
echo "Error: password must have at least 8 characters"
exit 1
fi
fi
cp "$TEMPLATES/network/interfaces/modular" /etc/network/interfaces
cp "$TEMPLATES/network/wlan0/default" /etc/network/interfaces.d/wlan0
cp "$TEMPLATES/network/eth0/default" /etc/network/interfaces.d/eth0
cp "$TEMPLATES/network/dhcpcd/modular" /etc/dhcpcd.conf
cp "$TEMPLATES/network/dnsmasq/default" /etc/dnsmasq.conf
cp "$TEMPLATES/rc.local/default" /etc/rc.local
cp "$TEMPLATES/network/10-wpa_supplicant" /lib/dhcpcd/dhcpcd-hooks/10-wpa_supplicant
rm -rf /etc/udev/rules.d/90-wireless.rules
{
echo "ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev"
echo "update_config=1"
wificountry="US"
if [ -r /etc/rpi-wifi-country ];
then
wificountry=$(cat /etc/rpi-wifi-country)
fi
echo "country=$wificountry"
} > /etc/wpa_supplicant/wpa_supplicant.conf
if [ -z "$wifipassword" ];
then
{
echo "network={"
echo " ssid=\"$wifinetwork\""
echo " key_mgmt=NONE"
echo "}"
} >> /etc/wpa_supplicant/wpa_supplicant.conf
restart_wifi >/dev/null 2>/dev/null
echo "open wifi network"
else
wpa_passphrase "$wifinetwork" "$wifipassword" >> /etc/wpa_supplicant/wpa_supplicant.conf
restart_wifi >/dev/null 2>/dev/null
echo "password network"
fi
}
function container {
container=$1
if [ "$container" = "docker" ]; then
disable_service balena
stop_service balena
enable_service docker
start_service docker
echo "Success: docker has been enabled and started."
elif [ "$container" = "balena" ]; then
disable_service docker
stop_service docker
enable_service balena
start_service balena
echo "Success: balena has been enabled and started."
elif [ "$container" = "none" ]; then
disable_service balena
disable_service docker
stop_service docker
stop_service balena
echo "Success: docker and balena have been disabled and stopped."
else
echo "Error: only 'docker', 'balena', 'none' options are supported";
fi
}
function bluetooth {
status=$1
if [ "$status" = "on" ]; then
cp "$TEMPLATES/bluetooth/hotspot" /etc/systemd/system/dbus-org.bluez.service
enable_service rpibluetooth
restart_service bluetooth
restart_service rpibluetooth
sleep 5 # wait 5 seconds for bluetooth to be completely up
echo "Success: the bluetooth service, and the hotspot service have been started."
elif [ "$status" = "off" ]; then
cp "$TEMPLATES/bluetooth/default" /etc/systemd/system/dbus-org.bluez.service
disable_service rpibluetooth
stop_service rpibluetooth
restart_service bluetooth
sleep 3 # Wait few seconds for bluetooth to start
restart_service bluealsa # restart the bluetooth audio service
echo "Success: the bluetooth service has been switched to default, and the hotspot service has been stopped."
else
echo "Error: only 'on', 'off' options are supported";
fi
}
function restart_ethernet {
ifup eth0 || true
ifdown eth0 || true
sleep 1
ifup eth0 || true
}
function ethernet {
cp "$TEMPLATES/network/interfaces/modular" /etc/network/interfaces
cp "$TEMPLATES/network/eth0/static" /etc/network/interfaces.d/eth0
sed -i "s/IPADDRESS/$1/g" /etc/network/interfaces.d/eth0
sed -i "s/NETMASK/$2/g" /etc/network/interfaces.d/eth0
sed -i "s/GATEWAY/$3/g" /etc/network/interfaces.d/eth0
sed -i "s/DNS/$4/g" /etc/network/interfaces.d/eth0
restart_ethernet >/dev/null 2>/dev/null
echo "This pirateship has anchored successfully!"
}
function restart_hotspot {
restart_service dhcpcd || true
ifdown wlan0 || true
sleep 1 || true
ifup wlan0 || true
sysctl net.ipv4.ip_forward=1 || true
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE || true
iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT || true
iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT || true
restart_service dnsmasq || true
restart_service hostapd || true
enable_service hostapd || true
enable_service dnsmasq || true
}
function hotspot {
essid=$1
password=$2
channels=(1 6 11)
channel=${channels[$((RANDOM % ${#channels[@]}))]};
if [ -n "$password" ];
then
if [ ${#password} -lt 8 ];
then
echo "Error: password must have at least 8 characters"
exit 1
fi
fi
cp "$TEMPLATES/network/interfaces/modular" /etc/network/interfaces
cp "$TEMPLATES/network/wlan0/hotspot" /etc/network/interfaces.d/wlan0
cp "$TEMPLATES/network/eth0/default" /etc/network/interfaces.d/eth0
cp "$TEMPLATES/network/dhcpcd/modular" /etc/dhcpcd.conf
cp "$TEMPLATES/network/dnsmasq/hotspot" /etc/dnsmasq.conf
cp "$TEMPLATES/network/hostapd/default" /etc/default/hostapd
cp "$TEMPLATES/rc.local/hotspot" /etc/rc.local
cp "$TEMPLATES/network/10-wpa_supplicant" /lib/dhcpcd/dhcpcd-hooks/10-wpa_supplicant
rm -rf /etc/udev/rules.d/90-wireless.rules
if [ -n "$password" ];
then
cp "$TEMPLATES/network/hostapd/password" /etc/hostapd/hostapd.conf
sed -i "s/ESSID/$essid/g" /etc/hostapd/hostapd.conf
sed -i "s/PASSWORD/$password/g" /etc/hostapd/hostapd.conf
sed -i "s/CHANNEL/$channel/g" /etc/hostapd/hostapd.conf
restart_hotspot >/dev/null 2>/dev/null
else
cp "$TEMPLATES/network/hostapd/no_password" /etc/hostapd/hostapd.conf
sed -i "s/ESSID/$essid/g" /etc/hostapd/hostapd.conf
sed -i "s/CHANNEL/$channel/g" /etc/hostapd/hostapd.conf
restart_hotspot >/dev/null 2>/dev/null
fi
echo "This pirateship has anchored successfully!"
}
function timezone {
timezone="$1"
if [ -z "$timezone" ];
then
echo "Error: the timezone is missing"
exit 1;
fi
if [ ! -f "/usr/share/zoneinfo/$timezone" ];
then
echo "Error: the timezone is not supported"
exit 1;
fi
rm /etc/localtime
echo "$timezone" > /etc/timezone
dpkg-reconfigure -f noninteractive tzdata 2>/dev/null
echo "Success: the timezone has been set"
}
function locale {
locale="$1"
if [ -z "$locale" ];
then
echo "Error: the locale is missing"
exit 1
fi
if ! locale_line="$(grep "^$locale " /usr/share/i18n/SUPPORTED)";
then
echo "Error: the specified locale is not supported"
exit 1
fi
encoding="$(echo "$locale_line" | cut -f2 -d " ")"
echo "$locale $encoding" > /etc/locale.gen
sed -i "s/^\\s*LANG=\\S*/LANG=$locale/" /etc/default/locale
dpkg-reconfigure -f noninteractive locales -q 2>/dev/null
echo "Success: the locale has been changed"
}
function ssh {
status=$1
if [ "$status" = "on" ]; then
enable_service ssh
start_service ssh
echo "Success: the ssh service has been started and enabled when the system boots"
elif [ "$status" = "off" ]; then
disable_service ssh
stop_service ssh
echo "Success: the ssh service has been stopped and disabled when the system boots."
else
echo "Error: only 'on', 'off' options are supported";
fi
}
function vnc {
status=$1
if [ ! -d /usr/share/doc/realvnc-vnc-server ] ; then
echo "Error: the vnc server is not installed, to install it run:"
echo "apt-get install realvnc-vnc-server"
exit 1;
fi
if [ "$status" = "on" ]; then
enable_service vncserver-x11-serviced.service
start_service vncserver-x11-serviced.service
echo "Success: the vnc service has been started and enabled when the system boots"
elif [ "$status" = "off" ]; then
disable_service vncserver-x11-serviced.service
stop_service vncserver-x11-serviced.service
echo "Success: the vnc service has been stopped and disabled when the system boots."
else
echo "Error: only 'on', 'off' options are supported";
fi
}
function default {
cp "$TEMPLATES/network/interfaces/default" "/etc/network/interfaces"
cp "$TEMPLATES/network/wpa_supplicant" "/etc/wpa_supplicant/wpa_supplicant.conf"
cp "$TEMPLATES/rc.local/default" "/etc/rc.local"
cp "$TEMPLATES/network/dnsmasq/default" "/etc/dnsmasq.conf"
cp "$TEMPLATES/network/dhcpcd/default" "/etc/dhcpcd.conf"
cp "$TEMPLATES/network/10-wpa_supplicant" /lib/dhcpcd/dhcpcd-hooks/10-wpa_supplicant
rm -rf /etc/udev/rules.d/90-wireless.rules
rm -rf /etc/hostapd.conf
rm -rf /etc/network/interfaces.d/*
rm -rf /etc/rpi-wifi-country
rename "raspberrypi" > /dev/null 2>/dev/null
bluetooth "off" > /dev/null 2>/dev/null
stop_service hostapd
stop_service dnsmasq
disable_service hostapd
disable_service dnsmasq
case $(detectrpi) in
RPIZ|RPIZW)
{
echo "auto usb0"
echo "allow-hotplug usb0"
echo "iface usb0 inet ipv4ll"
} > /etc/network/interfaces.d/usb0
;;
esac
echo 'Success: the rpi has been reset to default, please reboot your device'
}
function upgrade {
if ! [[ "$*" = *"-f"* ]];
then
last_version=$(npm show @treehouses/cli version)
if [ "$last_version" = "$(version)" ];
then
echo "$(basename "$0") is already up to date."
exit
fi
fi
npm install -g '@treehouses/cli'
}
function staticwifi {
cp "$TEMPLATES/network/interfaces/modular" /etc/network/interfaces
cp "$TEMPLATES/network/wlan0/static" /etc/network/interfaces.d/wlan0
cp "$TEMPLATES/network/10-wpa_supplicant" /lib/dhcpcd/dhcpcd-hooks/10-wpa_supplicant
rm -rf /etc/udev/rules.d/90-wireless.rules
sed -i "s/IPADDRESS/$1/g" /etc/network/interfaces.d/wlan0
sed -i "s/NETMASK/$2/g" /etc/network/interfaces.d/wlan0
sed -i "s/GATEWAY/$3/g" /etc/network/interfaces.d/wlan0
sed -i "s/DNS/$4/g" /etc/network/interfaces.d/wlan0
essid="$5"
password="$6"
if [ -n "$password" ];
then
if [ ${#password} -lt 8 ];
then
echo "Error: password must have at least 8 characters"
exit 1
fi
fi
{
echo "ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev"
echo "update_config=1"
wificountry="US"
if [ -r /etc/rpi-wifi-country ];
then
wificountry=$(cat /etc/rpi-wifi-country)
fi
echo "country=$wificountry"
} > /etc/wpa_supplicant/wpa_supplicant.conf
if [ -z "$password" ];
then
{
echo "network={"
echo " ssid=\"$wifinetwork\""
echo " key_mgmt=NONE"
echo "}"
} >> /etc/wpa_supplicant/wpa_supplicant.conf
else
wpa_passphrase "$essid" "$password" >> /etc/wpa_supplicant/wpa_supplicant.conf
fi
restart_wifi >/dev/null 2>/dev/null
echo "Success: the wifi settings have been changed, a reboot is required in order to see the changes"
}
# treehouses bridge MyNetwork HotspotNetwork MyNetworkPassword HotspotPassword
function bridge {
case $(detectrpi) in
RPI3B|RPIZW|RPI3B+)
;;
*)
echo "Your rpi model is not supported"
exit 1;
esac
wifiessid="$1"
hotspotessid="$2"
wifipassword="$3"
hotspotpassword="$4"
channels=(1 6 11)
channel=${channels[$((RANDOM % ${#channels[@]}))]};
if [ -z "$hotspotessid" ];
then
echo "a hotspot essid is required"
exit 1
fi
if [ -n "$wifipassword" ];
then
if [ ${#wifipassword} -lt 8 ];
then
echo "Error: wifi password must have at least 8 characters"
exit 1
fi
fi
if [ -n "$hotspotpassword" ];
then
if [ ${#hotspotpassword} -lt 8 ];
then
echo "Error: hotspot password must have at least 8 characters"
exit 1
fi
fi
cp "$TEMPLATES/network/dnsmasq/bridge" "/etc/dnsmasq.conf"
cp "$TEMPLATES/network/interfaces/modular" /etc/network/interfaces
cp "$TEMPLATES/network/wlan0/bridge" /etc/network/interfaces.d/ap0
if [ -z "$hotspotpassword" ];
then
cp "$TEMPLATES/network/hostapd/bridge_no_password" /etc/hostapd/hostapd.conf
else
cp "$TEMPLATES/network/hostapd/bridge_password" /etc/hostapd/hostapd.conf
sed -i "s/PASSWORD/$hotspotpassword/g" /etc/hostapd/hostapd.conf
fi
cp "$TEMPLATES/network/hostapd/default" /etc/default/hostapd
sed -i "s/CHANNEL/$channel/g" /etc/hostapd/hostapd.conf
sed -i "s/ESSID/$hotspotessid/g" /etc/hostapd/hostapd.conf
{
echo "ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev"
echo "update_config=1"
wificountry="US"
if [ -r /etc/rpi-wifi-country ];
then
wificountry=$(cat /etc/rpi-wifi-country)
fi
echo "country=$wificountry"
} > /etc/wpa_supplicant/wpa_supplicant.conf
if [ -z "$wifipassword" ];
then
{
echo "network={"
echo " ssid=\"$wifiessid\""
echo " key_mgmt=NONE"
echo "}"
} >> /etc/wpa_supplicant/wpa_supplicant.conf
else
{
echo "network={"
echo " ssid=\"$wifiessid\""
echo " psk=\"$wifipassword\""
echo "}"
} >> /etc/wpa_supplicant/wpa_supplicant.conf
fi
enable_service hostapd
enable_service dnsmasq
cp "$TEMPLATES/network/90-wireless.rules" /etc/udev/rules.d/90-wireless.rules
rm -rf /lib/dhcpcd/dhcpcd-hooks/10-wpa_supplicant
cp "$TEMPLATES/network/10-wpa_supplicant_bridge" /lib/dhcpcd/dhcpcd-hooks/10-wpa_supplicant
cp "$TEMPLATES/rc.local/bridge" /etc/rc.local
echo "the bridge has been built ;), a reboot is required to apply changes"
}
case $1 in
expandfs)
checkroot
expandfs
;;
rename)
checkroot
rename "$2"
;;
password)
checkroot
password "$2"
;;
sshkeyadd)
checkroot
shift
sshkeyadd "$@"
;;
version)
version
;;
detectrpi)
detectrpi
;;
wifi)
checkroot
wifi "$2" "$3"
;;
staticwifi)
checkroot
staticwifi "$2" "$3" "$4" "$5" "$6" "$7"
;;
container)
checkroot
container "$2"
;;
bluetooth)
checkroot
bluetooth "$2"
;;
ethernet)
checkroot
ethernet "$2" "$3" "$4" "$5"
;;
hotspot)
checkroot
hotspot "$2" "$3"
;;
timezone)
checkroot
timezone "$2"
;;
locale)
checkroot
locale "$2"
;;
ssh)
checkroot
ssh "$2"
;;
vnc)
checkroot
vnc "$2"
;;
default)
checkroot
default
;;
upgrade)
checkroot
shift
upgrade "$@"
;;
bridge)
checkroot
bridge "$2" "$3" "$4" "$5"
;;
help)
help "$2"
;;
*)
help
;;
esac