I am able to run this script as a user or root with bash or sh. Doing so gives me the output that I expect (arrayFWS is written to file, then emailed). However, when I run it with crontab with user or root, the netcat output is not written to the array, so the file is empty but email is still sent.
I have determined it to be an issue with the execution of the script only as a cronjob. What is it about the cron service that is not allowing my findings to be stored in the array? Is the netcat results not being piped properly causing nothing to be written?
#!/bin/bash
arrayFWS=() # ARRAY TO STORE FOUND FW's
# -- DEBBUGING MESSAGE
echo $varSTARTMESSAGE
# -- LOOP THAT CYCLES THROUGH FW NUMBERS
for (( i=136; i<=248; i++))
do
# -- DEBUG MESSAGE
echo "Checking FW$i ..."
# -- BUILD VARIABLE TO STORE FILTERED NETCAT SCAN LOOKING FOR A SMTP RESPONSE CONTAINING '200' VALUE -- INITAL SESSION SETUP
varCHK=`nc -w 3 192.168.$i.17 25 | grep "^220\ "`
# -- IF CONDITION THAT WILL CALL THE FILTERED NETCAT SCAN TO RUN
if [ -n "$varCHK" ]; then
echo "Found something at 192.168.$i.17"
echo "arrayFWS now contains: ${arrayFWS[@]}"
# -- WRITES RESPONSIVE FW TO ARRAY FOR LATER USE
arrayFWS=(${arrayFWS[@]} "$i")
fi
# -- DEBUG MESSAGE
echo "Done with FW$i ..."
done
echo " I found the following: ${arrayFWS[@]}"
# -------------------------------------------
varFILENAME="/root/report.txt"
echo "Starting to generate report"
echo > $varFILENAME "There are ${#arrayFWS[@]} FW's online"
echo >> $varFILENAME "----------------------------------------------------"
echo >> $varFILENAME " "
for (( d = 0 ; d < ${#arrayFWS[@]} ; d++ ))
do
echo >> $varFILENAME "FW${arrayFWS[$d]}"
done
# ---------------------------------------------
varSENDER="[email protected]"
(cat /root/report.txt) | mailx -r ${varSENDER} -s "Monitor: Found ${#arrayFWS[@]} FW's" [email protected]
Sample Email when run as user or root
There are 3 FW's online as of 201604155824
----------------------------------------------------
FW123
FW126
FW201
Sample Email when run under crontab (as root or user)
There are 0 FW's online as of 201604153501
----------------------------------------------------
cron
(if you usesystemd
you should see cron logs withjournalctl
). You can also try to uselogger
in your shell script to produce additional logging output for debugging. – Lucas Apr 15 at 23:11/bin/nc
or whatever is appropriate in your system. – LHWizard Apr 16 at 2:45