def new_request(self): if self.ip_addr: # if a CIDR block has already been learnt, go no farther range_id = get_range_id(self.conn, self.ip_addr) if range_id: print >>sys.stderr, "%s is inside range (ID=%d)" % (self.ip_addr, range_id) sys.exit(1) # if a whois request is outstanding for this address, toss the request req_id = get_request_id(self.conn, self.ip_addr) if req_id: print >>sys.stderr, "%s is already pending (request ID %d)" % (self.ip_addr, req_id) sys.exit(2) self.rir = responsible_rir(self.conn, self.ip_addr) self.conn.query("insert into ipv4_whois_req (ip_addr, rir) values (inet('%s'), '%s')" % (self.ip_addr, self.rir))
def update_ip_addr_info(conn, ip_addr, asnum, mm, hostname): print "---asnum:" print asnum try: asn = int(asnum) except ValueError: # instead of a single AS# we have an AS-set or AS-path # (or both). The origin AS is considered to be the last AS#, per # http://iplane.cs.washington.edu/data.html sa = [c for c in asnum] while not ('0' <= sa[-1] <= '9'): del sa[-1] i = -1 while '0' <= sa[i] <= '9': i -= 1 asn = int(''.join(sa[i+1:])) print "---asn" print asn if mm: (lat, long) = (mm.lat, mm.long) else: (lat, long) = (0.0, 0.0) #sometimes these can still be None if not lat: lat = 0.0 if not long: long = 0.0 # marshal formats and data. If MaxMind data is absent (lookup failed) # then only the lat/long will be set (to 0.0,0.0) set_str = "set asnum=%d, lat=%f, long=%f, mm_lat=%f, mm_long=%f, hostname='%s', p_status='%s'" data = [asn, lat, long, lat, long, hostname, 'B'] if mm: def us2bl(s): s = "''".join(s.split("'")) # render "St. John's" properly s = string.replace(s, "_", " ") return unicode(s, 'latin1', 'ignore').encode('utf-8') set_str += ", mm_country='%s', mm_region='%s', mm_city='%s', mm_postal='%s', mm_area_code=%d, mm_dma_code=%d " data += [mm.country, us2bl(mm.region), us2bl(mm.city), us2bl(mm.pcode), mm.area_c, mm.dma_c] print data, ip_addr qstr = ("update ip_addr_info "+set_str+"where ip_addr='%s'") % tuple(data+[ip_addr]) print qstr conn.query(qstr) # if the Autonomous System number is invalid, request a whois lookup on the network if asn < 1: # only make the request if the network is hitherto unknown if not get_range_id(conn, ip_addr): os.system("arin_whois.py -n %s" % ip_addr)