forked from offspot/kiwix-plug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_plug.sh
executable file
·204 lines (185 loc) · 13.4 KB
/
setup_plug.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
#!/bin/bash
# List deb packages to check/install:
# apt-get -o DPkg::options::=--force-confmiss --assume-yes install dialog install dnsmasq-base awstats nginx wireless-tools ntpdate
# Init a few variables
SSH_LOGIN=root
SSH_PASS=nosoup4u
COMMANDS=/tmp/setup_plug_commands.sh
# Check if the DNS works
`host www.kiwix.org > /dev/null`
EXIT_VALUE=$?
if [ ! "$EXIT_VALUE" = "0" ]
then
echo "Was not able to resolve www.kiwix.org. Are you access to Internet is OK?"
exit 1
fi
# Check if the access to Internet works
`ping -c3 www.kiwix.org > /dev/null`
EXIT_VALUE=$?
if [ ! "$EXIT_VALUE" = "0" ]
then
echo "Was not able to ping www.kiwix.org. Are you access to Internet is OK?"
exit 1
fi
# Find the IP of the plug
IP=`sudo arp-scan --localnet | grep -E $'\t(f0:ad:4e|40:2c:f4)' | cut -s -f1 | tail -n1`
if [ "$IP" = "" ]
then
echo "Unable to find the IP of the plug on your local network."
exit 1
else
echo "The IP of the plug is $IP"
fi
# Copy init.d script
pscp -pw "$SSH_PASS" scripts/kiwix-plug.plug "$SSH_LOGIN@$IP:/etc/init.d/kiwix-plug" <<EOF
n
EOF
# Write remote commands in a file
echo -e " \n\
echo \"\" \n\
echo \"Successfuly connected to the plug...\" \n\
" > $COMMANDS
# Setup the environement variable for non-interactive tty
echo -e " \n\
export DEBIAN_FRONTEND=noninteractive \n\
" >> $COMMANDS
# For security reason run dpkg
echo -e " \n\
dpkg --configure -a \n\
" >> $COMMANDS
# Check if dialog is there and install it otherwise
echo -e " \n\
DIALOG=\`dpkg -l dialog | grep ii\` \n\
if [ \"\$DIALOG\" = \"\" ] \n\
then \n\
echo \"Installing dialog...\" \n\
apt-get update \n\
apt-get --assume-yes install dialog \n\
if [ \"$?\" != \"0\" ] \n\
then \n\
echo \"Unable to install correctly dialog\" \n\
exit 1 \n\
else \n\
echo \"dialog installation successful\" \n\
fi \n\
else \n\
echo \"dialog is already installed.\" \n\
fi \n\
" >> $COMMANDS
# Check if dnsmasq is there and install it otherwise
echo -e " \n\
DNSMASQ=\`whereis dnsmasq | cut --delimiter=\":\" -f2 | cut --delimiter=\" \" -f2\` \n\
if [ \"\$DNSMASQ\" = \"\" ] \n\
then \n\
echo \"Installing dnsmasq...\" \n\
apt-get update \n\
apt-get --assume-yes install dnsmasq-base \n\
if [ \"$?\" != \"0\" ] \n\
then \n\
echo \"Unable to install correctly dnsmasq\" \n\
exit 1 \n\
else \n\
echo \"dnsmasq installation successful\" \n\
fi \n\
else \n\
echo \"dnsmasq is already installed.\" \n\
fi \n\
" >> $COMMANDS
# Check if awstats is installed
echo -e " \n\
AWSTATS=\`dpkg -l awstats | grep ii\` \n\
if [ \"\$AWSTATS\" = \"\" ] \n\
then \n\
echo \"Installing awstats...\" \n\
apt-get update \n\
apt-get --assume-yes install awstats \n\
if [ \"$?\" != \"0\" ] \n\
then \n\
echo \"Unable to install correctly awstats\" \n\
exit 1 \n\
else \n\
echo \"awstats installation successful\" \n\
fi \n\
else \n\
echo \"awstats is already installed.\" \n\
fi \n\
" >> $COMMANDS
# Check if nginx is there and install it otherwise. Dreamplugs are
# delivered with a self-compiled nginx, so we need to check the
# package instead of the binary
echo -e " \n\
echo \"(Re)-nstalling nginx...\" \n\
apt-get update \n\
apt-get remove --purge nginx nginx-full nginx-common \n\
apt-get -o DPkg::options::=--force-confmiss --assume-yes install nginx-common nginx-full \n\
" >> $COMMANDS
# Check if wireless-tools is installed
echo -e " \n\
WTOOLS=\`dpkg -l wireless-tools | grep ii\` \n\
if [ \"\$WTOOLS\" = \"\" ] \n\
then \n\
echo \"Installing wireless-tools...\" \n\
apt-get update \n\
apt-get --assume-yes install wireless-tools \n\
if [ \"$?\" != \"0\" ] \n\
then \n\
echo \"Unable to install correctly dialog\" \n\
exit 1 \n\
else \n\
echo \"dialog installation successful\" \n\
fi \n\
else \n\
echo \"wireless-tools are already installed.\" \n\
fi \n\
" >> $COMMANDS
# Check if ntpdate is installed and update the clock (ntpdate-debian)
echo -e " \n\
NTPDATE=\`dpkg -l ntpdate | grep ii\` \n\
if [ \"\$NTPDATE\" = \"\" ] \n\
then \n\
echo \"Installing ntpdate...\" \n\
apt-get update \n\
apt-get --assume-yes install ntpdate \n\
if [ \"$?\" != \"0\" ] \n\
then \n\
echo \"Unable to install correctly ntpdate\" \n\
exit 1 \n\
else \n\
echo \"ntpdate installation successful\" \n\
fi \n\
else \n\
echo \"ntpdate is already installed.\" \n\
fi \n\
ntpdate-debian \n\
" >> $COMMANDS
# Setup the init.d script
echo -e " \n\
IN_RC_LOCAL=\`grep \"/etc/init.d/kiwix-plug\" /etc/rc.local\` \n\
if [ \"\$IN_RC_LOCAL\" = \"\" ] \n\
then \n\
echo \"Updating /etc/rc.local...\" \n\
sed -i -e 's/exit 0//' /etc/rc.local \n\
echo \"\" >> /etc/rc.local \n\
echo \"/etc/init.d/kiwix-plug start\" >> /etc/rc.local \n\
echo \"\" >> /etc/rc.local \n\
else \n\
echo \"rc.local already updated\" \n\
fi \n\
chmod +x /etc/init.d/kiwix-plug \n\
chmod +x /etc/rc.local \n\
" >> $COMMANDS
# Avoid ubsmount mounting a flashdrive with a 077 umask
echo -e " \n\
if [ -f \"/etc/usbmount/usbmount.conf\" ] \n\
then \n\
sed -i \"s/umask=077/umask=022/g\" /etc/usbmount/usbmount.conf \n\
else \n\
echo \"no usbmount config file to patch\" \n\
fi \n\
" >> $COMMANDS
# Connect the plug per ssh and run a few commands
plink -ssh -pw "$SSH_PASS" "$SSH_LOGIN@$IP" -m $COMMANDS <<EOF
n
EOF
# End music
beep -f 659 -l 460 -n -f 784 -l 340 -n -f 659 -l 230 -n -f 659 -l 110 -n -f 880 -l 230 -n -f 659 -l 230 -n -f 587 -l 230 -n -f 659 -l 460 -n -f 988 -l 340 -n -f 659 -l 230 -n -f 659 -l 110 -n -f 1047 -l 230 -n -f 988 -l 230 -n -f 784 -l 230 -n -f 659 -l 230 -n -f 988 -l 230 -n -f 1318 -l 230 -n -f 659 -l 110 -n -f 587 -l 230 -n -f 587 -l 110 -n -f 494 -l 230 -n -f 740 -l 230 -n -f 659 -l 460