Sunday, March 21, 2010

Script to Check IP on SPAM Sources and getting EMAIL and SMS alerts.

As a System Administrators "The Key to a good performing Server is Good IP Reputation"

Maintaining a MAIL Servers or Web Servers is big time for System Admins,there is always a big fear for your IP getting Blacklisted on different SPAM sources due to which mails originated from your server either are not accepted or deffered by the Recipient Server.It would of great help if we get timely EMAIL as well as SMS alerts of IP getting blacklisted. This would make servers perform well as BOUNCES do use system resources which impact on system overall performance (Explaining Which in this article is OUT OF SCOPE.)

You can get this by Paying a Handsome amount of money to a Service Provider or DO MY way. I have written a Shell script using which you can get timely alerts as your IP gets blacklisted. You can put the script in crontab to run every Hour or so.

To START with Copy the script onto you system

# mkdir dnsbl && cd dnsbl

# vim dnsbl_check.sh


Copy the Below Script

#!/bin/bash

# Date: Mar 20,2010
# Author: Ashwin Muni
# Purpose: Check the IP Against Major SPAM Sources.

## Uncomment to Debug
# set -x

# Variables
tmp_file='/tmp/dnsbl'

#IN_DNSBL=127.0.0.[2-6]
#IN_DNSBL=127.0.0.
IN_DNSBL='127.0.0.2|127.0.0.3|127.0.0.4|127.0.0.5|127.0.0.6|127.0.0.7|127.0.0.8|127.0.0.9|127.0.0.10|127.0.0.'
DIG=`which dig`
MAIL_ADMIN="test@example.com ashwin@linuxmaza.com"

###################################################

# SCRIPT START

> $tmp_file

echo "Below IPs are Listed" >> $tmp_file

if [ "$#" == 1 ]; then

for i in `cat rbllist.txt`; do
IP_REV=`echo $1 | awk -F\. '{ print $4"."$3"."$2"."$1 }'`
$DIG $IP_REV.$i | grep $IN_DNSBL

if [ $? == '0' ]; then
#echo "$1 Listed on $i"
echo -e "\033[31m \033[1m PROBLEM : Listed on $i \033[0m \033[22m"
echo "################################ Attention : $1 Listed on $i" >> $tmp_file
else
echo -e "Not Listed on $i : \033[32m \033[1m OK \033[22m \033[0m "
echo "$1 Not Listed on $i" >> $tmp_file
fi
done

echo -e "\033[31m \033[1m ===================$1 is LISTED ON BELOW SPAM SOURCES====================== \033[0m \033[22m"

cat $tmp_file | mail -s "DNSBL REPORT FOR $1" $MAIL_ADMIN

else

echo -e "\t\t\t\t\033[31m \033[1m Enter Proper Arguments:\n Script Usage :\n /bin/sh $0 IP.ADD.RE.SS \033[0m \033[22m"

# EOF

################################################


Save the file Using ":wq"

Make necessary changes in the Script like the System Admin email address to sent Emails.

You will need the SPAM sources to check which you can find Here MAJOR SPAM SOURCES

Copy all the SPAM Sources and paste it in a txt file named "rbllist.txt"

# vi rbllist.txt


Should show you all the Major SPAM Sources for Checking your IPs.

Note: The script and the rbllist.txt should exist in the Same directory.

Once done we will give executable permission to the script which allows us to run it.

# chmod 755 dnsbl_check.sh


OR

# chmod +x dnsbl_check.sh


Now Run the Script

#./dnsbl_check.sh 100.200.100.200


You can put the script in crontab to run it regularly.

17 comments:

  1. ufc: You can use either Wordpress or Even Joomla or Drupal
    http://www.joomla.org
    http://www.wordpress.org

    ReplyDelete
  2. I saw many sites but yours is awsome, bookmarked for future referrence.

    ReplyDelete
  3. You post great articles. Bookmarked !

    ReplyDelete
  4. Great post! you should do a follow up i think!

    ReplyDelete
  5. Great post! you should do a follow up i think!

    ReplyDelete
  6. Great post! you should do a follow up i think!

    ReplyDelete
  7. Great post! Maybe you should do a follow up article about this?

    ReplyDelete
  8. Great post! Maybe you should do a follow up article about this?

    ReplyDelete
  9. Great post! Maybe you should do a follow up article about this?

    ReplyDelete
  10. Great post! Maybe you should do a follow up article about this?

    ReplyDelete
  11. Hi Ashwin,

    Your script stopped my searching to a list of blacklists and script to automate the black list checking checking of ips. Your script working well and I can easily understand the logic used here. The free rbl services are not functioning well because they are not managing it properly, so if we have a list of perfectly managed rbl then it would be a gift...

    Keep going ...

    Warm regards,
    Haridas N.

    ReplyDelete
  12. Hi Ashwin,

    Small fix to your scirpt....

    we need to use egrep '127.0.0.2 | 127.0.0.3 | etc...' instead of using a variable with grep , it will take it as one string so we will not get any matched string even when there is a replay.

    Thanks,
    Haridas N.

    ReplyDelete
  13. But I could not get MAJOR SPAM SOURCES. It is not working. Please help me to give the alternet link.

    Thanks

    Aloy Das
    BBTS
    July 16, 2012

    ReplyDelete
  14. Hi Ashwim,

    I got the MAJOR SPAM SOURCES from following link: http://linuxmaza.blogspot.com/2010/03/major-spam-sources-where-your-ips-can.html

    Thanks again for your great post.

    Aloy

    ReplyDelete
  15. Hi,

    I am facing some problem when run the script, I am getting following error:

    [root@localhost]# ./dnsbl_check.sh 119.18.145.7
    ./dnsbl_check.sh: line 25: syntax error near unexpected token `newline'
    ./dnsbl_check.sh: line 25: `echo "################################ Attention : $1 Listed on $i" >> '

    Please help me.

    Regards

    Aloy

    ReplyDelete