@@ -56,6 +56,7 @@ Usage: $script [OPTIONS]
5656 [ -e ] : Change @email address (default: $EMAIL )
5757 [ -m ] : Change mail (system alias) (default: $EMAIL )
5858 [ -n ] : Do not send email report (default: $SEND_EMAIL )
59+ [ -s ] : Suppress non error messages
5960 [ -v ] : Print blacklist version
6061 [ -h ] : this help message
6162
@@ -65,6 +66,7 @@ Examples:
6566 $script -b /my/custom/bots.d (Download globalblacklist.conf & update with your custom bots.d location)
6667 $script -e yourname@youremailaddress.com (Download globalblacklist.conf specifying your email address for the notification)
6768 $script -u /path/to/install-ngxblocker (Use custom path to install-ngxblocker to update bots.d / conf.d include files)
69+ $script -s -m webmaster (Send mail to a system alias address & give less verbose messages for cron)
6870EOF
6971 exit 0
7072}
@@ -77,21 +79,22 @@ check_version() {
7779# local version
7880version=$( grep " Version:" $file | sed ' s|^.*: V||g' )
7981date=$( grep " Updated:" $file | sed ' s|^.*: ||g' )
80- printf " \nLOCAL Version: $BOLDWHITE$version$RESET \n"
81- printf " Updated: $date \n\n"
82+ print_message " \nLOCAL Version: $BOLDWHITE$version$RESET \n"
83+ print_message " Updated: $date \n\n"
8284
8385# remote version
8486curl -s --limit-rate 5k -r $range --location $url -o $tmp
8587remote_ver=$( grep " Version:" $tmp | sed ' s|^.*: V||g' )
8688remote_date=$( grep " Updated:" $tmp | sed ' s|^.*: ||g' )
87- printf " REMOTE Version: $BOLDWHITE$remote_ver$RESET \n"
88- printf " Updated: $remote_date \n"
89+ print_message " REMOTE Version: $BOLDWHITE$remote_ver$RESET \n"
90+ print_message " Updated: $remote_date \n"
8991rm -f $tmp
9092
9193if [ " $version " != " $remote_ver " ]; then
92- printf " \nUpdate Available => $BOLDMAGENTA$remote_ver$RESET \n\n"
94+ print_message " \nUpdate Available => $BOLDMAGENTA$remote_ver$RESET \n\n"
95+ return 1
9396else
94- printf " \nLatest Blacklist Already Installed: $BOLDGREEN$version$RESET \n\n"
97+ print_message " \nLatest Blacklist Already Installed: $BOLDGREEN$version$RESET \n\n"
9598fi
9699else
97100printf " ${BOLDRED} ERROR${RESET} : Missing '$file ' => ${BOLDWHITE} running $INSTALLER :${RESET} \n"
@@ -100,8 +103,6 @@ check_version() {
100103check_version
101104fi
102105fi
103-
104- exit 0
105106}
106107
107108check_dirs () {
@@ -215,10 +216,18 @@ check_depends() {
215216fi
216217}
217218
219+ print_message () {
220+ local msg=" $@ "
221+
222+ if [ " $VERBOSE " != " N" ]; then
223+ printf " $msg "
224+ fi
225+ }
226+
218227get_options () {
219228local arg= opts=
220229
221- while getopts :c:b:u:r:e:m:nvh opts " $@ "
230+ while getopts :c:b:u:r:e:m:nvsh opts " $@ "
222231do
223232if [ -n " ${OPTARG} " ]; then
224233case " $opts " in
@@ -237,6 +246,7 @@ get_options() {
237246m) EMAIL=$arg ;; # /etc/aliases no sanity checks
238247n) SEND_EMAIL=N ;;
239248v) check_depends; check_version ;;
249+ s) export VERBOSE=N ;;
240250h) usage ;;
241251\? ) usage ;;
242252:) check_args $OPTARG none none ;;
@@ -267,25 +277,25 @@ main() {
267277output=$CONF_DIR /$file
268278
269279# check for updated blacklist
270- update= $( check_version | tail -n 2 )
271- printf " \n $ update\n\n "
280+ check_version
281+ update= $?
272282
273- if echo $update | grep ^Update 1> /dev/null ; then
283+ if [ $update = 1 ] ; then
274284
275285# download globalblacklist update
276286tmp=$( mktemp)
277287mkdir -p $CONF_DIR
278- printf " ${BOLDWHITE} Downloading: $file "
288+ local dl_msg= " ${BOLDWHITE} Downloading: $file "
279289curl --fail --connect-timeout 60 --retry 10 --retry-delay 5 -so $tmp $url
280290retval=$?
281291
282292case " $retval " in
283- 0) printf " ...${BOLDGREEN} OK ${RESET} \n\n"
293+ 0) print_message " $dl_msg ...${BOLDGREEN} [OK] ${RESET} \n\n"
284294 mv $tmp $output
285295 ;;
286- 22) printf " ...${BOLDRED} ERROR 404: $url ${RESET} \n\n" ;;
287- 28) printf " ...${BOLDRED} ERROR TIMEOUT: $url ${RESET} \n\n" ;;
288- * ) printf " ...${BOLDRED} ERROR CURL: ($retval ){RESET}\n\n" ;;
296+ 22) printf " $dl_msg ...${BOLDRED} ERROR 404: $url ${RESET} \n\n" ;;
297+ 28) printf " $dl_msg ...${BOLDRED} ERROR TIMEOUT: $url ${RESET} \n\n" ;;
298+ * ) printf " $dl_msg ...${BOLDRED} ERROR CURL: ($retval ){RESET}\n\n" ;;
289299esac
290300
291301# download new bots.d / conf.d files
@@ -297,13 +307,16 @@ main() {
297307# re-read nginx configuration
298308if [ $retval = 0 ]; then
299309
300- $service nginx reload
310+ $service nginx reload 2>&1 > /dev/null
301311if [ $? = 0 ]; then
302312status=" ${BOLDGREEN} [OK]${RESET} "
313+ print_message " \nReloading NGINX configuration...$status \n"
314+
303315else
304316status=" ${BOLDRED} [FAILED]${RESET} "
317+ printf " \nReloading NGINX configuration...$status \n"
318+
305319fi
306- printf " \nReloading NGINX configuration...$status \n"
307320else
308321printf " \n${BOLDRED} Download failed${RESET} : not reloading NGINX config\n"
309322fi
@@ -315,7 +328,10 @@ main() {
315328# email report
316329check_mail_depends
317330case " $SEND_EMAIL " in
318- y* |Y* )printf " Emailing report to: ${BOLDWHITE} $EMAIL ${RESET} \n\n" ;
331+ y* |Y* )if [ " $VERBOSE " = " N" ]; then
332+ grep " Version:" $CONF_DIR /globalblacklist.conf | tr -d ' #'
333+ fi
334+ print_message " Emailing report to: ${BOLDWHITE} $EMAIL ${RESET} \n\n" ;
319335# remove ansi colour codes
320336sed -i ' s/\x1b\[[0-9;]*m//g' $EMAIL_REPORT
321337cat $EMAIL_REPORT | mail -s " Nginx Bad Bot Blocker Updated" $EMAIL
@@ -338,5 +354,8 @@ exit $?
338354# Here's another example to run it daily at midday using a command line switch to set the email address for the notification
339355# 00 12 * * * sudo /usr/sbin/update-ngxblocker -e yourname@youremailprovider.com
340356
357+ # Less verbose logging to a system alias mail address (root crontab)
358+ # 00 12 * * * /usr/sbin/update-ngxblocker -s -m webmaster
359+
341360# better logging for cron jobs:
342361# https://serverfault.com/questions/137468/better-logging-for-cronjobs-send-cron-output-to-syslog
0 commit comments