From a41f63598bce392cc192959bc34a795797e352bf Mon Sep 17 00:00:00 2001 From: Laurent Ghigonis Date: Thu, 28 Feb 2013 03:52:51 +0100 Subject: dtube: implement HeIP to get info from an IP --- dtube/dtube.py | 65 +++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 10 deletions(-) (limited to 'dtube') diff --git a/dtube/dtube.py b/dtube/dtube.py index ca1be1c..80ca57d 100755 --- a/dtube/dtube.py +++ b/dtube/dtube.py @@ -4,8 +4,10 @@ # 2013 Laurent Ghigonis # # TODO -# * ip dns country -# * -t +# * dns country +# * -a show all (href ...) +# * [-t ranges|A|PTR|ip] +# * [-f filter] import sys import os @@ -22,13 +24,12 @@ from optparse import OptionParser import conf_dtube def usage(): - # [-t ranges|A|PTR|ip] [-f filter] return """usage: %s [-h] [-v] AS | range-cidr | IP | DNS | country Examples: %s AS23118 %s 91.220.156.0/24 -%s 8.8.8.8 +%s 208.67.222.222 %s ovh.net %s US""" % (sys.argv[0], sys.argv[0], sys.argv[0], sys.argv[0], sys.argv[0], sys.argv[0]) @@ -53,23 +54,23 @@ class He: def initTarget(cls, target, verbose=False): match = re.search(r'^AS([0-9]*)$', target) if match: - print "Target identified as AS %s" % match.group(1) + if verbose: print "Target identified as AS %s" % match.group(1) return HeAS(match.group(1), verbose=verbose) match = re.search(r'^([0-9]+(?:\.[0-9]+){3}/[0-9]+)$', target) if match: - print "Target identified as IPrange %s" % match.group(1) + if verbose: print "Target identified as IPrange %s" % match.group(1) return HeIPrange(match.group(1), verbose=verbose) match = re.search(r'^([0-9]+(?:\.[0-9]+){3})$', target) if match: - print "Target identified as IP %s" % match.group(1) + if verbose: print "Target identified as IP %s" % match.group(1) return HeIP(match.group(1), verbose=verbose) match = re.search(r'(.*\..*)', target) if match: - print "Target identified as DNS %s" % match.group(1) + if verbose: print "Target identified as DNS %s" % match.group(1) return HeDNS(match.group(1), verbose=verbose) match = re.search(r'^([^\.0-9]*)$', target) if match: - print "Target identified as Country %s" % match.group(1) + if verbose: print "Target identified as Country %s" % match.group(1) return HeCountry(match.group(1), verbose=verbose) raise Exception("Unable to identify target as AS / IPrange / IP / DNS / Country") @@ -90,6 +91,8 @@ class He: self.tree = etree.HTML(self.html.content) if not getattr(self.html, 'from_cache', False): time.sleep(random.random() * 3) # Forced to be kind + if self.verbose and self.html.from_cache: + print "Response from cache" class HeAS(He): url_prefix = '/AS' @@ -179,7 +182,49 @@ class HeIP(He): url_prefix = '/ip/' def __init__(self, IP, fromfile=None, verbose=False): - raise Exception("IP Not supported yet") + self.IP = str(IP) + He.__init__(self, conf_dtube.url_root + HeIP.url_prefix + self.IP, + fromfile, verbose=verbose) + self.parse() + + def parse(self): + # IP Info + info_it = self.tree.xpath('//div[@id="ipinfo"]')[0] + as_it = info_it.xpath('table/tbody/tr') + self.AS = dict() + if as_it: + for a in as_it: + self.AS[a.xpath('td[position()=1]/a/child::text()')[0]] = { + "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] + } + self.AS_txt = "" + if len(self.AS.keys()) > 0: + for a in self.AS: + self.AS_txt += a + '\t' + self.AS[a]["IPrange"] \ + + '\t' + self.AS[a]["description"] + '\n' + # XXX Whois + # DNS + dns_it = self.tree.xpath('//div[@id="dns"]')[0] + self.dns = dict() + self.dns["PTR"] = dns_it.xpath('a[position()=1]/child::text()')[0] + self.dns["PTR_href"] = dns_it.xpath('a[position()=1]')[0].get('href') + self.dns["A_list"] = dns_it.xpath('a[position()>1]/child::text()') + self.dns["A_href_list"] = [a.get('href') for a in dns_it.xpath('a[position()>1]')] + self.dns_txt = "%s\n" % self.dns["PTR"] + if len(self.dns.keys()) > 0: + self.dns_txt += ' '.join(self.dns["A_list"]) + '\n' + # XXX RBL + + def show(self): + print "====== AS ======" + print "AS\tIPrange\t\tDescription" + print self.AS_txt + print "====== DNS ======" + print "PTR and A records" + print self.dns_txt class HeDNS(He): url_prefix = '/ip/' -- cgit v1.2.3-59-g8ed1b