I can’t explain you the joy in writing this for the very first time about Python.

It’s just couple of days since started with Python and a long way to go. It’s a golden rule for any programming language, first program be it in C, C++, Bash, etc etc … “Hello World” comes first.

I started with IP black List check.

I don’t say this as a well written program but to start with, it’s good if not best.

I’ll try to explain each and every line of the program and it’s module.

IP BlackList check Logic:

1. An IP address

2.Check for the valid format of IP address

3. Reverse IP address

4. Use dig command to query the DB against the IP address

5. If the output consists of 127.0.0.* (, etc etc) then the IP is blacklisted in that DB.

Program Description:

import re                                  
import subprocess          
x=raw_input(‘What is your IP address? ‘)

def ipformatcheck(x):
    pattern = r”\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b”
    if re.match(pattern, x):
        #return True
        print (‘Ip format valid’)
        #print (‘Reversing the IP’)
        ip = str(x).split(‘.’)
        rev = ‘%s.%s.%s.%s’ % (ip[3],ip[2],ip[1],ip[0])
                spamdbs = [‘.dnsbl.sorbs.net’, ‘.cbl.abuseat.org’, ‘.bl.spamcop.net’, ‘.zen.spamhaus.org’, ‘.sbl.spamhaus.org’, ‘.xbl.spamhaus.org’, ‘.pbl.spamhaus.org’]
                for db in spamdbs:
                       if db == ‘.pbl.spamhaus.org’:
                       p = subprocess.Popen([“dig”, “+short”, rev+db], stdout=subprocess.PIPE)
                       output, err = p.communicate()
                       print db
                       print output

        #return False
        print (‘IP format InValid’)

Brief Description:

  • import re : This module provides regular expression matching operations : ref : http://docs.python.org/2/library/re.html
  • import subprocess The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.

: ref :  http://docs.python.org/dev/library/subprocess.html

  • pattern = r”\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b”

: ref : http://bytes.com/topic/python/answers/569207-how-validate-ip-address-python

This will validate the whether IP address entered in valid format or not.

  • ip = str(x).split(‘.’)
            rev = ‘%s.%s.%s.%s’ % (ip[3],ip[2],ip[1],ip[0]) 

This will reverse the IP addres, where str(x) is str(IP entered by user).split(‘.’)

Example: http://www.tutorialspoint.com/python/string_split.htm

  • spamdbs = [‘.dnsbl.sorbs.net’, ‘.cbl.abuseat.org’, ‘.bl.spamcop.net’, ‘.zen.spamhaus.org’, ‘.sbl.spamhaus.org’, ‘.xbl.spamhaus.org’, ‘.pbl.spamhaus.org’]

will be querying this set of DB to check IP blacklisting and willbe using for loop.

  • In order to use dig command (linux command) in python, we use subprocess.

: ref :  http://www.cyberciti.biz/faq/python-execute-unix-linux-command-examples/

Program in action:


Format Check: