3/24/2015

Crontabs, Nifty Notifications

When creating crontabs to exec a routine, its sometimes helpful to send the results as an wbem indication, snmptrap or email message often these are received on mobile device and a quick glance should indicate status, here's how to do that.

First a typical crontab routine


# crontab -e


58 7 * * * ( /wpa/test-auth-eduroam/wpa_supplicant/eapol_test -c /wpa/test-auth-eduroam/wpa_supplicant/peap-mschapv2.conf -a 192.168.2.180 -s "inwhowetrust" | grep ^SUCCESS > /dev/null ) && { echo "<p style='font-family:arial;color:green;font-size:20px;'> SUCCESS </p>" > test-auth-eduroam1.txt; } || { echo "<p style='font-family:arial;color:red;font-size:20px;' > FAILED </p>" > test-auth-eduroam1.txt; } ; mail -s "$(echo -e "eduroam - testing\nContent-Type: text/html")" john.willis@johnwillis.com < test-auth-eduroam1.txt
It follows the regular pattern of

[when/howoften]

[what/todo]

followed by a logical clause that creates a custom msg file based on the results of a grep for a condition in the results of the output of a command

[ 58 7 * * * ]
<<<<<<<< is the usual "when/howoften"


( /wpa/test-auth-eduroam/wpa_supplicant/eapol_test -c /wpa/test-auth-eduroam/wpa_supplicant/peap-mschapv2.conf -a 192.168.2.180 -s "inwhowetrust" | grep ^SUCCESS  > /dev/null )
<<<<<<<< is the usual "what/todo"

it "includes" a follow up task by piping the output of the command through a grep statement to search for a condition and consigns the rest to the bit bucket devnull

but the result of the grep statement sets a conditional status

which is used by the next piece

&& { echo "<p style='font-family:arial;color:green;font-size:20px;'>  SUCCESS  </p>" > test-auth-eduroam1.txt; } || { echo "<p style='font-family:arial;color:red;font-size:20px;' >  FAILED  </p>" > test-auth-eduroam1.txt; }

to "explicitly" echo an html formatted  msg based on the value of that "conditional status" from the grep statement

it is "either" going to be

>>>>>> "green formatted SUCCESS"

or

>>>>>> "red formatted FAILED"

the last piece merely "compounds" the number of things to do before this crontab is complete with a semicolon in normal bash shell fashion

mail -s "$(echo -e "eduroam - testing\nContent-Type: text/html")" john.willis@johnwillis.com < test-auth-eduroam1.txt

it sets a mail msg subject line and content header

directs the msg to a recipient

and inports the custom msg body from the content file filled with the desired formatted result of the grep test

the nice thing about this method is it demonstrates the level of sophistication achieveable using basic tools and the original crontab method in concert with what I like to call "horizontal programming" or "horizontal procedureal thinking"

there is no reason this could not be extended indefinately in the orthogonal dimension except possible command shell buffer limits.. and unfortunately that does seem to be the case with certain malware