From 53192e5961d82dba04dcbdbb322fb0c0a8be79b3 Mon Sep 17 00:00:00 2001 From: Laurent Ghigonis Date: Sun, 10 Mar 2013 00:33:26 +0100 Subject: dtube: search for closest IPrange if not not found --- dtube/dtube.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'dtube') diff --git a/dtube/dtube.py b/dtube/dtube.py index 61ca79d..b0a868d 100755 --- a/dtube/dtube.py +++ b/dtube/dtube.py @@ -61,7 +61,20 @@ class He: match = re.search(r'^([0-9]+(?:\.[0-9]+){3}/[0-9]+)$', target) if match: if verbose: print "Target identified as IPrange %s" % match.group(1) - return HeIPrange(match.group(1), verbose=verbose) + try: + he = HeIPrange(match.group(1), verbose=verbose) + except Exception, e: + if str(e) == "Target not found": + print e + print "Trying to get closest range announcements" + ip = HeIP(match.group(1).split('/')[0], verbose=verbose) + as_target = max(ip.AS.iterkeys(), key=(lambda key: ip.AS[key]['position'])) + iprange = ip.AS[as_target]["IPrange"] + print "New IPrange selected: %s" % iprange + he = HeIPrange(iprange, verbose=verbose) + else: + raise e + return he match = re.search(r'^([0-9]+(?:\.[0-9]+){3})$', target) if match: if verbose: print "Target identified as IP %s" % match.group(1) @@ -80,14 +93,17 @@ class He: def parse_AS_announces_table(cls, table_it): as_it = table_it.xpath('tbody/tr') AS = dict() + position = 0 if as_it: for a in as_it: AS[a.xpath('td[position()=1]/a/child::text()')[0]] = { + "position": position, "AS_href": a.xpath('td[position()=1]/a')[0].get('href'), "IPrange": a.xpath('td[position()=2]/a/child::text()')[0], "IPrange_href": a.xpath('td[position()=2]/a')[0].get('href'), "description": a.xpath('td[position()=3]/child::text()')[0] } + position += 1 AS_txt = "" if len(AS.keys()) > 0: for a in AS: -- cgit v1.2.3-59-g8ed1b