-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstaller.sh
241 lines (222 loc) · 8.77 KB
/
Installer.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
#!/bin/bash
# _____ _____ _____ _
# | __ \_ _| / ____| | |
# | |__) || | | (___ ___| |_ _ _ _ __
# | ___/ | | \___ \ / _ \ __| | | | '_ \
# | | _| |_ ____) | __/ |_| |_| | |_) |
# |_| |_____| |_____/ \___|\__|\__,_| .__/
# | |
# |_|
#
# This Script is for initial Pi configuration
# It is developed on personal purpose. More information can be found in the Github repo.
# https://github.com/macntech/Pi-Setup
#
# Author: Johannes Geisslinger
# Web: https://macandtech.com
# Check install of whiptail before entering the tool
clear
if ! which whiptail &>/dev/null; then
echo "This Installer depends on whiptail. Please install whiptail first."
exit 1
fi
#### VARIABLES ####
INSTALL_DIR="/home/boot"
PROFIL_PATH="/etc/profile"
MOTD_PATH="/etc/motd"
HOSTNAME_FILE=/etc/hostname
INTERFACE_FILE="/etc/dhcpcd.conf"
SSH_FILE="/etc/ssh/sshd_config"
SOFTWARE="PI Setup"
CURRENT_HOSTNAME=`cat /etc/hostname | tr -d " \t\n\r"`
RC_LOCAL="/etc/rc.local"
#### END VARIABLES #####
# Function to setup the Hostname of the Raspberry
# Function Status: Done
function SetupHostname {
{
sleep 0.25
echo -e "XXX\n50\nUpdating Hostname... \nXXX"
echo "$HOSTNAME" > "$HOSTNAME_FILE"
sed -i "s/127.0.1.1.*$CURRENT_HOSTNAME/127.0.1.1\t$HOSTNAME/g" /etc/hosts
sleep 0.25
echo -e "XXX\n100\nUpdating Hostname...Done \nXXX"
sleep 0.5
} | whiptail --gauge "Please wait..." 6 60 0
}
# Function to request the static IP from the user entry
# Function Status: Done
function SetStaticNetwork {
act_IP=$(hostname -I)
while [[ -z $net_result ]] || [[ $net_result == "1" ]] ; do
IP=$(whiptail --inputbox --nocancel "IP Adress for your system" 8 78 $act_IP --title "Network Settings" 3>&1 1>&2 2>&3)
GATEWAY=$(whiptail --inputbox --nocancel "Please enter the Gateway of your network" 8 78 Name --title "Network Setting" 3>&1 1>&2 2>&3)
if (whiptail --title "Network Settings" --yesno "Do you want to set a domain for your Raspberry?" 8 78); then
DOMAIN=$(whiptail --inputbox --nocancel "Please enter the Domain (FQDN) of your Raspberry (e.g. rasperry.local)" 10 78 Name --title "Network Setting" 3>&1 1>&2 2>&3)
else
echo "::: LOG ::: Setup Domain cancelled"
fi
whiptail --title "Are the settings correct?" --yesno "\n IP Adress: $IP \n Gateway: $GATEWAY \n Domain: $DOMAIN \n" 12 78 3>&1 1>&2 2>&3
net_result=$?
done
# Call the Function to write the network information
WriteNetwork
}
# Function to Write the IP into dhcpcd
# Function Status: Done
function WriteNetwork {
{
sleep 0.5
echo -e "XXX\n0\nUpdating Network Config... \nXXX"
#Write Network Settings in File
echo "interface eth0" >> $INTERFACE_FILE
echo -e "XXX\n25\nUpdating IP Config... \nXXX"
echo "static ip_address=$IP" >> $INTERFACE_FILE
echo -e "XXX\n50\nUpdating Gateway Config... \nXXX"
echo "static routers=$GATEWAY" >> $INTERFACE_FILE
echo -e "XXX\n75\nUpdating Domain Config... \nXXX"
echo "static domain_name=$DOMAIN" >> $INTERFACE_FILE
echo "static domain_search=$DOMAIN" >> $INTERFACE_FILE
echo -e "XXX\n100\nUpdating Network Config...Done \nXXX"
sleep 0.5
} | whiptail --gauge "Please wait..." 6 60 0
}
# Function to enable the root user SSH and set password
# Function Status: Done
function SetRootPW {
while [[ -z $password_result ]] || [[ $password_result == "1" ]] ; do
rootpasswd1=$(whiptail --passwordbox "Enter new password for root:" 10 60 3>&1 1>&2 2>&3)
rootpasswd2=$(whiptail --passwordbox "Repeat new password for root:" 10 60 3>&1 1>&2 2>&3)
if [ $rootpasswd1 != $rootpasswd2 ]; then
whiptail --msgbox "Passwords do not match" 10 60
! true
fi
password_result=$?
done
writeRoot
}
function writeRoot {
{
echo -e "XXX\n30\nEnable Root User... \nXXX"
sudo sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' $SSH_FILE
sleep 0.5
echo -e "XXX\n60\nUpdating Root Password... \nXXX"
echo -e "$rootpasswd1\n$rootpasswd2" | passwd root
sleep 0.5
echo -e "XXX\n80\nRestarting SSH Service... \nXXX"
sudo service ssh restart
sleep 0.5
echo -e "XXX\n100\nEnable Root Complete... \nXXX"
sleep 0.5
} | whiptail --gauge "Please wait..." 6 60 0
}
# Function to setup the new SSH Login Screen
# Function Status: Done
function SetupLoginScreen {
{
echo -e "XXX\n10\nChecking install dir... \nXXX"
mkdir -p "$INSTALL_DIR"
sleep 0.5
echo -e "XXX\n20\nCopy Files... \nXXX"
cp -b motd.sh "$INSTALL_DIR"
chmod 777 "$INSTALL_DIR/motd.sh"
sleep 0.5
echo -e "XXX\n40\nWriting new Entries... \nXXX"
echo "$INSTALL_DIR/motd.sh" >> "$PROFIL_PATH"
sleep 0.5
echo -e "XXX\n60\nRemove old Files... \nXXX"
rm -f /etc/profile.d/wifi-check.sh
rm -f /etc/update-motd.d/10-uname
> /etc/motd
sleep 0.25
echo -e "XXX\n100\nLogin Screen completed... \nXXX"
sleep 0.25
} | whiptail --gauge "Please wait..." 6 60 0
}
# Function to setup the I2C OLED Config of the Raspberry
# Function Status: Done
function SetupI2C {
{
sleep 0.5
echo -e "XXX\n5\nRefresh Update Library... \nXXX"
apt-get update
mkdir -p "$INSTALL_DIR"
echo -e "XXX\n10\nSetup PIP I2C... \nXXX"
apt-get install python3-pip -y
echo -e "XXX\n20\nSetup I2C Tools... \nXXX"
apt-get install i2c-tools -y
echo -e "XXX\n30\nSetup RPI.GPIO... \nXXX"
pip3 install RPI.GPIO
echo -e "XXX\n40\nSetup ADAFruit Blinka... \nXXX"
pip3 install adafruit-blinka
echo -e "XXX\n50\nSetup ADAFruit SSD1306... \nXXX"
pip3 install adafruit-circuitpython-ssd1306
echo -e "XXX\n60\nSetup Python Pil... \nXXX"
apt-get install python3-pil -y
echo -e "XXX\n70\nCopy Files... \nXXX"
cp -u stats.py "$INSTALL_DIR"
echo -e "XXX\n100\nDone \nXXX"
echo "::: LOG ::: I2C OLED Setup completed."
sleep 1
} | whiptail --gauge "Please wait..." 6 60 0
}
function copyStats {
sudo sed -i '/exit 0/i sudo python3 \/home\/boot\/stats.py \&' /etc/rc.local
}
############## Start the main Setup Tool ################
echo "::: LOG ::: Setup Started"
whiptail --title "PI Setup" --msgbox "This is a simple support tool to help setup Raspberry Pi.\n\nDetails can be found in the GitHub Repo.\n\nVisit me at https://macandtech.com" 15 78
while [ 1 ]
do
#Main Menu
CHOICE=$(
whiptail --title "Pi Setup" --menu "Make your choice" 16 100 9 \
"1)" "Start Initialization" \
"2)" "Enable I2C Screen Output" \
"3)" "End script" 3>&2 2>&1 1>&3
)
#Selection
case $CHOICE in
"1)")
echo "::: LOG ::: Initialization started"
#Setup Hostname first
HOSTNAME=$(whiptail --inputbox --nocancel "Please enter the Hostname for your system" 8 78 Hostname --title "Hostname" 3>&1 1>&2 2>&3)
SetupHostname
echo "::: LOG ::: Hostname changed to $HOSTNAME"
#Give Option to Set Fixed IP
if (whiptail --title "Network Settings" --yesno "Do you need a fixed IP Adress on the Raspberry" 8 78); then
SetStaticNetwork
echo "::: LOG ::: Setup Fixed Network at $IP / $GATEWAY / $DOMAIN"
else
echo "::: LOG ::: Setup Fix IP cancelled"
fi
#Set Root user
if (whiptail --title "Root User Setup" --yesno "Do you want to setup and enable SSH root user?" 8 78); then
echo "::: LOG ::: Setup SSH Root User confirmed."
SetRootPW
else
echo "::: LOG ::: Setup SSH Root User denied."
fi
#Do main manipulation
#Inform user about start of script
whiptail --title "Initialization" --msgbox "The system will now start initialization of new SSH screen. Please confirm." 8 78
SetupLoginScreen
;;
"2)")
whiptail --title "Setup Screen Output" --msgbox "The system will now setup I2C OLED Screen Output. If you already have the depending software installed, nothing will be installed and only the Screen script is copied. Please confirm." 8 78
SetupI2C
copyStats
whiptail --title "Setup Screen Output" --msgbox "All files are setup and dependencies are installed. Please enable I2C AND SPI with raspi-config command." 8 78
;;
"3)")
if (whiptail --title "Reboot" --yesno "After you finish the setup you should reboot your PI to enable all settings made (Please remember your fix IP if set during setup). You can find the Logfile under ${PWD} \n\nDo you want to reboot now?" 12 78); then
echo "::: LOG ::: Setup completed. Reboot confirmed. Shutdown in 1 Minute. To cancel type shutdown -c"
sudo shutdown -r 1
else
echo "::: LOG ::: Setup completed. Reboot cancelled. Please reboot manually."
exit
fi
;;
esac
done
exit