February 22

Grep a log file and send output to mail

This is a small script that looks for a string in a log file and then the string is found, sents by mail the output

-This example is for Smoothwall, in which i run Guardian Active response and i wanted to know who is blocked

-Place it in your rc.local, reboot and check your mailbox

 

 

 


#!/bin/bash
EMAILADDRESS="root@localhost"
EMAILSUBJECT="Guardian On Firewall "
MESSAGEBODY="/tmp/alert.txt"

##Select you log file
#tail -f /var/log/messages | while read LINE
tail -f /var/log/guardian/guardian.log | while read LINE
do
#Select the string
if [ `echo $LINE | grep -c "blocked"` -gt 0 ]
# if [ `echo $LINE | grep -c "eth"` -gt 0 ]

then
echo "Guardian actions:" > $MESSAGEBODY
#Insert some text before the output
echo $LINE >> $MESSAGEBODY
# echo "Guardian done it again !" >> $MESSAGEBODY
#Here i use sendEmail from Smoothwall, you can use you own sendmail/mail commands
sendEmail -f -u "$EMAILSUBJECT" -t -s smtp.gmail.com:587 -xu user@gmail.com -xp password < $MESSAGEBODY
# fi
done


Tags: ,

Copyright © 1998. All rights reserved.

Posted February 22, 2014 by RaNd in category "/etc/config

Select comment form

Loading Facebook Comments ...

Leave a Reply

Your email address will not be published. Required fields are marked *

five × two =