-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPITe.sh
executable file
·416 lines (356 loc) · 11 KB
/
PITe.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
#!/bin/bash
#--------------------------------------------------------------------
# (c) Koen Hufkens for BlueGreen Labs (BV)
#
# This script installs all necessary configuration
# files as required to upload images to the PhenoCam server
# (phenocam.nau.edu) on your NetCam Live2 camera REMOTELY with
# minimum interaction with the camera.
#
# Use is permitted within the context of the PhenoCam US network,
# in its standard configuration. For all exceptions contact
# BlueGreen Labs
#
#--------------------------------------------------------------------
#---- global login banner ----
echo ""
echo "===================================================================="
echo ""
echo " Phenocam Installation Tool (PIT) V2 for NetCam Live2 cameras"
echo ""
echo " (c) BlueGreen Labs (BV) 2024 - https://bluegreenlabs.org"
echo ""
echo " -----------------------------------------------------------"
echo ""
#---- subroutines ----
# error handling installation subroutines
error_exit(){
echo ""
echo " NOTE: If no confirmation of a successful upload is provided,"
echo " or warnings are shown, check all script parameters."
echo ""
echo "===================================================================="
exit 0
}
# error handling key retrieval routine
error_key(){
echo ""
echo " WARNING: No login key (pair) 'phenocam_key' found... "
echo " (please run the installation routine)"
echo ""
echo "===================================================================="
exit 0
}
# error handling purging routine
error_purge(){
echo ""
echo " WARNING: Purging of system settings failed, please try again!"
echo ""
echo "===================================================================="
exit 0
}
# error handling test routine
error_upload(){
echo ""
echo " WARNING: Image upload failed, check your network connection"
echo " and settings!"
echo ""
echo "===================================================================="
exit 0
}
# define usage
usage() {
echo "
This scripts covers the installation of your
Stardot NetCam Live2 PhenoCam
please use the following arguments.
Arguments which require an additional parameter
have descriptions enclosed in <> brackets. Other
arguments are binary choices.
Usage: $0
[-i <camera ip address>]
[-p <camera password>]
[-n <camera name>]
[-o <time offset from UTC/GMT>]
[-s <start time 0-23>]
[-e <end time 0-23>]
[-m <interval minutes>]
[-d <destination> which network to use, either 'phenocam' or 'icos']
[-u uploads images if specified, requires -i to be specified]
[-v validate login credentials for sFTP transfers]
[-r retrieves login key if specified, requires -i to be specified]
[-x purges all previous settings and keys if specified, requires -i to be specified]
[-h calls this menu if specified]
" 1>&2
exit 0
}
validate() {
echo " Trying to validate sFTP login"
echo ""
# check if IP is provided
if [ -z ${ip} ]; then
echo " No IP address provided"
error_upload
fi
# create command
command="
sh /mnt/cfg1/scripts/phenocam_validate.sh
"
# execute command
ssh admin@${ip} ${command} 2>/dev/null
echo ""
echo "[if no warnings are given your logins were successful]"
echo ""
echo "===================================================================="
exit 0
}
upload() {
echo " Trying to upload image to the server"
echo ""
# check if IP is provided
if [ -z ${ip} ]; then
echo " No IP address provided"
error_upload
fi
# create command
command="
sh /mnt/cfg1/scripts/phenocam_upload.sh
"
# execute command
ssh admin@${ip} ${command} || error_upload 2> /dev/null
echo ""
echo "===================================================================="
exit 0
}
# if the retrieve the public key
retrieve() {
# check if IP is provided
if [ -z ${ip} ]; then
echo " No IP address provided"
error_key
fi
# create command
command="
if [ -f '/mnt/cfg1/phenocam_key' ]; then dropbearkey -t ecdsa -s 521 -f /mnt/cfg1/phenocam_key -y; else exit 1; fi
"
echo " Retrieving the public key login credentials"
echo ""
# execute command
ssh admin@${ip} ${command} > tmp.pub || error_key 2>/dev/null
# strip out the public key
# no header or footer
grep "ecdsa-sha2" tmp.pub > phenocam_key.pub
rm -rf tmp.pub
echo ""
echo " The public key was written to the 'phenocam_key.pub' file"
echo " in your current working directory!"
echo ""
echo " Forward this file to phenocam@nau.edu to finalize your"
echo " sFTP installation."
echo ""
echo "===================================================================="
exit 0
}
# if the retrieve argument is active retrieve the
# public private keys, check if there are more than
# two arguments given to avoid accidental purging
purge() {
echo " Purging all previous settings and login credentials"
echo ""
# check if IP is provided
if [ -z ${ip} ]; then
echo " No IP address provided"
error_purge
fi
# ASK FOR CONFIRMATION!!!
read -p "Do you wish to perform this action?" yesno
case $yesno in
[Yy]* )
echo "Purging the system settings..."
;;
[Nn]* )
echo "You answered no, exiting"
exit_purge
;;
* )
exit_purge
;;
esac
# create command
command="
rm -rf /mnt/cfg1/settings.txt
rm -rf /mnt/cfg1/.password
rm -rf /mnt/cfg1/phenocam_key
rm -rf /mnt/cfg1/update.txt
rm -rf /mnt/cfg1/scripts/
"
# execute command
ssh admin@${ip} ${command} || error_purge 2>/dev/null
echo ""
echo " Done, cleaned the camera settings!"
echo ""
echo "===================================================================="
exit 0
}
#---- parse arguments (and/or execute subroutine calls) ----
# grab arguments
while getopts "hi:p:n:o:s:e:m:d:uvrx" option;
do
case "${option}"
in
i) ip=${OPTARG} ;;
p) pass=${OPTARG} ;;
n) name=${OPTARG} ;;
o) offset=${OPTARG} ;;
s) start=${OPTARG} ;;
e) end=${OPTARG} ;;
m) int=${OPTARG} ;;
d) dest=${OPTARG} ;;
u) upload ;;
v) validate ;;
r) retrieve ;;
x) purge ;;
h | *) usage; exit 0 ;;
esac
done
#---- installation routine ----
# validating parameters
if [[ -z ${ip} || ${ip} == -* ]]; then
echo " WARNING: No IP address provided"
error_exit
fi
if [[ -z ${pass} || ${pass} == -* ]]; then
echo " WARNING: No password provided"
error_exit
fi
if [[ -z ${name} || ${name} == -* ]]; then
echo " WARNING: No camera name provided"
error_exit
fi
if [[ -z ${offset} || ${offset} == -* ]]; then
echo " WARNING: No GMT time offset provided"
error_exit
fi
if [[ -z ${dest} || ${dest} == -* ]]; then
echo " WARNING: provided destination argument is empty"
error_exit
fi
if [[ "${dest}" != "phenocam" && "${dest}" != "icos" ]]; then
echo " WARNING: network option is not valid (should be 'phenocam' or 'icos')"
error_exit
fi
if [[ -z ${start} || ${start} == -* ]]; then
echo " NOTE: No start time (24h format) provided, using the default (9h)"
start='9'
fi
if [[ -z ${end} || ${end} == -* ]]; then
echo " NOTE: No end time (24h format) provided, using the default (22h)"
end='22'
fi
if [[ -z ${int} || ${int} == -* ]]; then
echo " NOTE: No interval (in minutes) provided, using the default (30 min)"
int='30'
fi
# Rename server variables to URLs
if [[ ${dest} == "phenocam" ]]; then
url="phenocam.nau.edu"
else
url="icos01.uantwerpen.be"
fi
# Default to GMT time zone
tz="GMT"
# colour settings
red="220"
green="125"
blue="220"
brightness="128"
contrast="128"
sharpness="128"
hue="128"
saturation="100"
backlight="0"
command="
echo TRUE > /mnt/cfg1/update.txt &&
echo ${name} > /mnt/cfg1/settings.txt &&
echo ${offset} >> /mnt/cfg1/settings.txt &&
echo ${tz} >> /mnt/cfg1/settings.txt &&
echo ${start} >> /mnt/cfg1/settings.txt &&
echo ${end} >> /mnt/cfg1/settings.txt &&
echo ${int} >> /mnt/cfg1/settings.txt &&
echo ${red} >> /mnt/cfg1/settings.txt &&
echo ${green} >> /mnt/cfg1/settings.txt &&
echo ${blue} >> /mnt/cfg1/settings.txt &&
echo ${brightness} >> /mnt/cfg1/settings.txt &&
echo ${sharpness} >> /mnt/cfg1/settings.txt &&
echo ${hue} >> /mnt/cfg1/settings.txt &&
echo ${contrast} >> /mnt/cfg1/settings.txt &&
echo ${saturation} >> /mnt/cfg1/settings.txt &&
echo ${backlight} >> /mnt/cfg1/settings.txt &&
echo ${dest} >> /mnt/cfg1/settings.txt &&
echo ${pass} > /mnt/cfg1/.password &&
echo ${url} > /mnt/cfg1/server.txt &&
if [ ! -f /mnt/cfg1/phenocam_key ]; then dropbearkey -t ecdsa -s 521 -f /mnt/cfg1/phenocam_key >/dev/null; fi &&
cd /var/tmp; cat | base64 -d | tar -x &&
if [ ! -d '/mnt/cfg1/scripts' ]; then mkdir /mnt/cfg1/scripts; fi &&
cp /var/tmp/files/* /mnt/cfg1/scripts &&
rm -rf /var/tmp/files &&
sh /mnt/cfg1/scripts/check_firmware.sh || return 1 &&
echo '#!/bin/sh' > /mnt/cfg1/userboot.sh &&
echo 'sh /mnt/cfg1/scripts/phenocam_install.sh' >> /mnt/cfg1/userboot.sh &&
echo 'sh /mnt/cfg1/scripts/phenocam_upload.sh' >> /mnt/cfg1/userboot.sh &&
echo '' &&
echo ' Successfully uploaded install instructions.' &&
echo ' The camera configuration will take effect on reboot.' &&
echo '' &&
echo ' The following options have been set:' &&
echo ' ------------------------------------' &&
echo '' &&
echo ' Sitename: ${name} | Timezone: GMT${offset}' &&
echo ' Upload start - end: ${start} - ${end} (h)' &&
echo ' Upload interval: every ${int} (min)' &&
echo '' &&
echo ' And the following colour settings:' &&
echo ' ----------------------------------' &&
echo '' &&
echo ' Gain values (R G B): ${red} ${green} ${blue}' &&
echo ' Brightness: ${brightness} | Sharpness: ${sharpness}' &&
echo ' Hue: ${hue} | Contrast: ${contrast}' &&
echo ' Saturation: ${saturation} | Backlight: ${backlight}' &&
echo '' &&
echo ' ----------------------------------' &&
echo '' &&
echo ' NOTE:' &&
echo ' A key (pair) exists or was generated, please run:' &&
echo ' ./PIT.sh -i ${ip} -r' &&
echo ' to display/retrieve the current login key' &&
echo ' and send this key to phenocam@nau.edu to' &&
echo ' or phenocam@uantwerpen.be to complete the install.' &&
echo '' &&
echo '====================================================================' &&
echo '' &&
echo ' --> SUCCESSFUL UPLOAD OF THE INSTALLATION SCRIPT' &&
echo ' --> THE CAMERA WILL REBOOT TO COMPLETE THE INSTALL' &&
echo ' --> THIS CONFIGURATION USES THE - ${dest} - NETWORK' &&
echo ' --> USING THE - ${url} - SERVER' &&
echo '' &&
echo ' [NOTE: the full install will take several reboot cycles (~5 min !!),' &&
echo ' please wait before logging in or triggering the script again. The' &&
echo ' current SSH connection will be closed for reboot in 30 sec.]' &&
echo '' &&
echo '====================================================================' &&
echo '' &&
sh /mnt/cfg1/scripts/reboot_camera.sh
"
echo ""
echo " Please login to execute the installation script."
echo ""
# install command
BINLINE=$(awk '/^__BINARY__/ { print NR + 1; exit 0; }' $0)
tail -n +${BINLINE} $0 | ssh admin@${ip} ${command} || error_exit 2>/dev/null
#---- purge password from history ----
# remove last lines from history
# containing the password
history -d -1--2
# exit
exit 0
__BINARY__