I'm new to the world of scripting and and currently using a script to perform a health check on the san devices that I manage.
The script is simple and redirects output of commands to a current file from remote device to my local host. I then use a diff command to compare output of current file and reference file and get a email alert of the difference.
However the problem that I'm currently facing is that every time I run the script, I get a email after the script is executed, that is even if there is no difference in the comparison, I get a blank email from the script execution.
As this is related to performing a automated health check, getting a blank email everyday will be a nuisance. Is there any way of tweaking the diff or add some more code to make sure that when there is no difference showing from the diff, I should not get a email and only get a email if there is a difference and showing the difference.
current script
### saves output of command switchshow in a file on local host
ssh user@ip_of_switch switchshow > switchshow_results
### Compares current output to reference file and mails the difference
diff switchshow_reference switchshow_results \
| mail -s device_PORT_ERROR email_recipient
Please let me know if any other information is required.
Thanks for the help and details explanation Slm :)
I have so far got the email part working (I get an email only if there is a difference from diff
command). However the email that I now get has its test concatenated into one long string, which is bad for viewing and not easy to understand for the people who will be getting it.
My current script is pasted below:
ssh test@ip_of_device switchshow > switchshow_results
variable=`diff switchshow_reference switchshow_results`
if [[ $variable -eq 0 ]]
then
echo $"nothing"
else
echo $variable | mail -s switch_HARDWARE_CHECK recipeint_email_address
fi
The output of the above script is blank and I don't get any email when the diff
command does not find any difference. However when the diff
commands finds a difference, I get a email as pasted below:
16c16 < 0 0 010000 id N4 Online FC F-Port 1 N Port + 1 NPIV public --- > 0 0 010000 id N4 No_Light FC F-Port 1 N Port + 1 NPIV public 26c26 < 10 10 010a00 id N4 Online FC F-Port 1 N Port + 1 NPIV public --- > 10 10 010a00 id N4 No_Light FC F-Port 1 N Port + 1 NPIV public 29c29 < 13 13 010d00 id N4 Online FC F-Port 50:06:01:67:3b:20:23:0a --- > 13 13 010d00 id N4 No_Light FC F-Port 50:06:01:67:3b:20:23:0a
This should ideally look like this in the email..
16c16 < 0 0 010000 id N4 Online FC F-Port 1 N Port + 1 NPIV public --- > 0 0 010000 id N4 No_Light FC F-Port 1 N Port + 1 NPIV public
26c26 < 10 10 010a00 id N4 Online FC F-Port 1 N Port + 1 NPIV public --- > 10 10 010a00 id N4 No_Light FC F-Port 1 N Port + 1 NPIV public
29c29 < 13 13 010d00 id N4 Online FC F-Port 50:06:01:67:3b:20:23:0a --- > 13 13 010d00 id N4 No_Light FC F-Port 50:06:01:67:3b:20:23:0a
Output should display one line at a time in the email. Any suggestions?