This repository was archived by the owner on Oct 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpinet-workshop
181 lines (157 loc) · 5.11 KB
/
pinet-workshop
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
#!/bin/bash
# Part of PiNet https://github.com/PiNet/PiNet-Workshop
#
# See LICENSE file for copyright and license details
version=0.0.1
#PiNet-Workshop
#A simple tool for doing batch functions for Raspberry Pi workshops using PiNet.
NBDRun() {
#Checks if it should be auto NBD compressing or not, if it should be, it recompresses the image
echo "--------------------------------------------------------"
echo "Compressing the image, this will take roughly 5 minutes"
echo "--------------------------------------------------------"
ltsp-update-image /opt/ltsp/armhf #If NBD is enabled, recompress the image
}
CheckTerminalSize(){
#Resizes the terminal to the correct size for the menus
eval `resize`
if [ "$LINES" -le "28" ]; then
a='\e[8;28;'
b='t'
printf "$a$COLUMNS$b"
fi
eval `resize`
if [ "$COLUMNS" -le "90" ]; then
a='\e[8;'
b=';90t'
printf "$a$LINES$b"
fi
}
fixGroups(){
#Adds users to correct needed groups. Can take a while if there is a lot of users!
#Adds all the groups if they don't exist
for ugroup in adm dialout cdrom audio users sudo video games plugdev input; do
egrep -i "^$ugroup" /etc/group >> /dev/null
if [ $? = 1 ]; then
groupadd $ugroup
fi
done
#Adds all users to all the required groups if they aren't already in them
cut -d: -f1,3 /etc/passwd | egrep ':[0-9]{4}$' | cut -d: -f1 | while IFS= read -r user #Gets
do
for ugroup in adm dialout cdrom audio users video games plugdev input pupil; do
if ! groups "$user" | grep -q -E ' $ugroup(\s|$)'; then
usermod -a -G $ugroup $user
fi
done
done
}
fixGroupsSingle() {
#Check a single user is in the correct groups
for ugroup in adm dialout cdrom audio users video games plugdev input pupil; do
if ! groups "$1" | grep -q -E ' $ugroup(\s|$)'; then
usermod -a -G $ugroup $1
fi
done
}
DisplayUsers(){
#Displays system users onto the terminal
clear
local TEACHER=$(cat /etc/group | grep --regex "^teacher:.*" | awk -F: '{print $4}')
echo "-------------------"
echo "Current Linux users"
echo "-------------------"
cut -d: -f1,3 /etc/passwd | egrep ':[0-9]{4}$' | cut -d: -f1 | while IFS= read -r user
do
echo "$user"
done
echo "---------------"
echo "Current Linux users in the teacher group"
echo "$TEACHER"
echo "---------------"
echo "Press enter to continue"
read
}
CreateBatch(){
username=$(whiptail --inputbox "Enter the base username" 8 78 --title "Username" 3>&1 1>&2 2>&3)
userNum=$(whiptail --inputbox "How many users would you like to create? E.g. pi1, pi2" 8 78 --title "How many?" 3>&1 1>&2 2>&3)
password=$(whiptail --inputbox "What do you want to set the password to?" 8 78 --title "Password" 3>&1 1>&2 2>&3)
for i in $(seq 1 $userNum)
do
batchUser=$username$i
egrep "^$batchUser" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
whiptail --title "Error" --msgbox "$batchUser already exists!" 8 78
else
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
useradd -m -s /bin/bash -p $pass $batchUser
fixGroupsSingle $batchUser
fi
done
}
RemoveBatch(){
username=$(whiptail --inputbox "Enter the base username" 8 78 --title "Username" 3>&1 1>&2 2>&3)
userNum=$(whiptail --inputbox "How many of the batch would you like to remove from 1? E.g. pi1, pi2" 8 78 --title "How many?" 3>&1 1>&2 2>&3)
for i in $(seq 1 $userNum)
do
batchUser=$username$i
egrep "^$batchUser" /etc/passwd >/dev/null
userdel -rf $batchUser
if [ ! $? -eq 0 ]; then
whiptail --title "Error" --msgbox "$batchUser does not exist!" 8 78
else
userdel -rf $batchUser
fi
done
DisplayUsers
}
Menu(){
IP=`ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`
MENUEPT=$(whiptail --title "PiNet-Workshop $version - $IP" --cancel-button "Quit" --ok-button "Select" --menu "What would you like to do?" 20 85 10 \
"Create-batch-users" "Create a batch of users following a pattern" \
"Remove-batch-users" "Delete a batch user set" \
"Group-check" "Checks all users are in the correct system groups" \
"NBD-recompress" "Force an NBD compress if changes are made outside PiNet" \
3>&1 1>&2 2>&3)
case "$MENUEPT" in
Create-batch-users)
CreateBatch
Menu
;;
Remove-batch-users)
RemoveBatch
Menu
;;
Group-check)
fixGroups
Menu
;;
NBD-recompress)
NBDRun
Menu
;;
*)
exit
;;
esac
}
if [ "$(id -u)" != "0" ]; then #Check if script is being run as root
echo "This script must be run as root" 1>&2
exit 1
fi
if [ ! "$BASH_VERSION" ] ; then #Checks that PiNet is being run using bash and not ash (aka sh)
whiptail --title "Shell error" --msgbox "Please do not run PiNet-workshops with sudo sh $0. Please run it with bash using sudo bash $0" 8 78
echo "Please do not run PiNet-workshops with sh $0. Please run it with bash using sudo bash $0" 1>&2
exit 1
fi
CheckTerminalSize
sleep 0.05
if [ ! -d /opt/ltsp/armhf ]; then
whiptail --title "Welcome" --yesno 'PiNet installation has not be detected! Do you want to continue?' 8 78
exitstatus=$?
if [ $exitstatus = 0 ]; then
Menu
fi
else
Menu
fi