Commit 7f333571 authored by Tommy Lau's avatar Tommy Lau

Add a python script to get cidr from arin

parent 51fdc654
www.google.com
www.google.com.hk
www.google.sg
www.google.co.jp
accounts.google.com
plus.google.com
play.google.com
news.google.com
mail.google.com
wallet.google.com
www.gstatic.com
ssl.gstatic.com
lh5.googleusercontent.com
youtube.com
www.youtube.com
m.youtube.com
i.ytimg.com
s.ytimg.com
r2---sn-npo7zn7s.googlevideo.com
tc.v1.cache2.googlevideo.com
tc.v10.cache1.googlevideo.com
tc.v10.cache8.googlevideo.com
manifest.googlevideo.com
yt3.ggpht.com
www.blogger.com
googleblog.blogspot.com
facebook.com
www.facebook.com
developers.facebook.com
l.facebook.com
static.xx.fbcdn.net
scontent-a-sin.xx.fbcdn.net
scontent-b-sin.xx.fbcdn.net
fbcdn-profile-a.akamaihd.net
fbexternal-a.akamaihd.net
fbcdn-sphotos-d-a.akamaihd.net
bit.ly
www.twitter.com
m.twitter.com
mobile.twitter.com
support.twitter.com
about.twitter.com
twimg.com
abs.twimg.com
pbs.twimg.com
ma.twimg.com
t.co
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import xml.etree.ElementTree
import urllib2
def get_netmask(mask):
bits = 0
for i in xrange(32 - mask, 32):
bits |= (1 << i)
return "%d.%d.%d.%d" % ((bits & 0xff000000) >> 24, (bits & 0xff0000) >> 16, (bits & 0xff00) >> 8, (bits & 0xff))
def get_decimal_ip(ip):
ip_split = ip.split('.')
ip_decimal = 0
for i in ip_split:
ip_decimal += int(i)
ip_decimal <<= 8
ip_decimal >>= 8
return ip_decimal
def query_cidr(ip):
url = "http://whois.arin.net/rest/nets;q=%s?showDetails=true&showARIN=false&ext=netref2" % ip
f = urllib2.urlopen(url)
ss = f.read()
root = xml.etree.ElementTree.fromstring(ss)
net_block = root.find("{http://www.arin.net/whoisrws/core/v1}net").find(
"{http://www.arin.net/whoisrws/core/v1}netBlocks").find("{http://www.arin.net/whoisrws/core/v1}netBlock")
start_address = net_block.find("{http://www.arin.net/whoisrws/core/v1}startAddress").text
cidr_length = int(net_block.find("{http://www.arin.net/whoisrws/core/v1}cidrLength").text)
return start_address, get_netmask(cidr_length)
if __name__ == "__main__":
route_table = {}
with open("domains.txt", "r") as f:
for line in f:
domain = line.strip()
if len(domain) != 0:
print("Processing domain [%s] " % domain),
ip = socket.gethostbyname(domain)
print("IP: %s" % ip),
decimal_ip = get_decimal_ip(ip)
exist = False
for t in route_table:
if (t & decimal_ip) == t:
exist = True
break
if exist:
print "exist, skip . . ."
else:
addr, mask = query_cidr(ip)
route_table[get_decimal_ip(addr)] = (addr, mask)
print("CIDR: %s/%s" % (addr, mask))
tables = sorted(route_table.items())
print tables
with open("route.txt", "w") as f:
for route in tables:
print("route = %s/%s" % (route[1][0], route[1][1]))
f.write("route = %s/%s\n" % (route[1][0], route[1][1]))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment