Changeset 156

Show
Ignore:
Timestamp:
02/03/10 20:28:58 (5 months ago)
Author:
mbooth
Message:

Use a library to properly deal with the generation of lists of IPs from subnets.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/misc/dns-o-matic.py

    r129 r156  
    66#   $ ./dns-o-matic.py > report.html 
    77# 
    8 # This script requires the python DNS module available from http://pydns.sourceforge.net/ which is 
     8# This script requires the python DNS and netaddr modules, which are 
    99# probably already available in your favourite Linux distribution. 
    1010# From Ubuntu: 
    11 #   $ apt-get install python-dns 
     11#   $ apt-get install python-dns python-netaddr 
    1212# From Fedora: 
    13 #   $ yum install python-pydns 
     13#   $ yum install python-pydns python-netaddr 
    1414 
    1515 
    1616import sys 
    1717import DNS 
     18from netaddr import IPNetwork, IPAddress 
     19 
    1820DNS.DiscoverNameServers() 
    1921 
     
    5658    # reverse lookup request gives you all the hostnames that the given ip address resolves to 
    5759    if type == 'PTR': 
    58         a,b,c,d = record.split('.') 
    59         r = Request('.'.join([d,c,b,a]) + '.in-addr.arpa',type,nameserver) 
     60        rec = IPAddress(record) 
     61        r = Request(rec.reverse_dns,type,nameserver) 
    6062 
    6163    # forward lookup request gives you all the ip addresses that the given hostname resolves to 
     
    136138 
    137139    # iterate through all valid addresses in the subnet 
    138     for count in range(1,255): 
    139     #for count in range(28,29): 
    140         ip = subnet + "." + `count` 
     140    for ip in [x for x in list(subnet) if x != subnet.broadcast and x != subnet.ip]: 
    141141        sys.stderr.write("Processing %s\n" % ip); 
    142         address = {'ip':ip, 'results':[]} 
     142        address = {'ip':str(ip), 'results':[]} 
    143143        for ns,pri in nameservers: 
    144             RecursiveLookup(address,ip,'PTR',ns) 
     144            RecursiveLookup(address,str(ip),'PTR',ns) 
    145145 
    146146        # forget about addresses that don't have records in *any* nameservers 
     
    208208<html> 
    209209<head> 
    210   <title>DNS-o-matic Report</title> 
     210  <title>DNS-o-Matic Report</title> 
    211211  <style> 
    212212    th { 
     
    239239</head> 
    240240<body> 
    241 <h1>DNS-o-matic Report</h1> 
     241<h1>DNS-o-Matic Report</h1> 
    242242""" 
    243243 
    244244domain = 'cse-servelec.com' 
     245subnets = [IPNetwork('194.62.153.0/24'), 
     246           IPNetwork('194.62.154.0/24'), 
     247           ] 
     248 
    245249nslist = GetNameServers(domain) 
    246250PrintNameServers(domain,nslist) 
    247 PrintSubnetAnalysis(GetSubnetAnalysis(nslist, '194.62.153')) 
    248 #PrintSubnetAnalysis(GetSubnetAnalysis(nslist, '194.62.154')) 
     251for subnet in subnets: 
     252    analysis = GetSubnetAnalysis(nslist, subnet) 
     253    PrintSubnetAnalysis(analysis) 
    249254 
    250255print """