def testInOperator(self):
		"""Test the 'in' operator for IPRanges."""
		r = netblock.IPRanges('127/8')
		self.assertEqual('127.0.0.1' in r, 1)
		self.assertEqual('126.255.255.255' in r, 0)
		# Check that everything actually, you know, works.
		r = netblock.IPRanges('127.0.0.0/24')
		r.add("128.0.0.0/23")
		r.add("255.255.255.0/24")
		r.add("0.0.0.0/24")
		# Three /24s and a /23 are 256*5.
		self.assertEqual(len(r), 1280)
		c = 0
		for e in r:
			self.assertEqual(e in r, 1)
			# Now test the number version.
			en = netblock.strtoip(e)
			self.assertEqual(en in r, 1)
			c += 1
		# Make sure we actually enumerated everything.
		self.assertEqual(c, len(r))
Esempio n. 2
0
 def testInOperator(self):
     """Test the 'in' operator for IPRanges."""
     r = netblock.IPRanges('127/8')
     self.assertEqual('127.0.0.1' in r, 1)
     self.assertEqual('126.255.255.255' in r, 0)
     # Check that everything actually, you know, works.
     r = netblock.IPRanges('127.0.0.0/24')
     r.add("128.0.0.0/23")
     r.add("255.255.255.0/24")
     r.add("0.0.0.0/24")
     # Three /24s and a /23 are 256*5.
     self.assertEqual(len(r), 1280)
     c = 0
     for e in r:
         self.assertEqual(e in r, 1)
         # Now test the number version.
         en = netblock.strtoip(e)
         self.assertEqual(en in r, 1)
         c += 1
     # Make sure we actually enumerated everything.
     self.assertEqual(c, len(r))
Esempio n. 3
0
 def getlipn(self):
     if self._lipn is None:
         self._lipn = netblock.strtoip(self._lip)
     return self._lipn
Esempio n. 4
0
def ipto32int(ip):
    r = netblock.strtoip(ip)
    if r < 0x80000000L:
        return int(r)
    else:
        return int(r - 0x100000000L)
Esempio n. 5
0
 def module_run(self, searches):
     #search = self.options['search']
     headers = {'Accept': 'application/json'}
     for search in searches:
         url = 'http://whois.arin.net/rest/orgs;name=%s' % (
             urllib.quote(search))
         orgs = self._request(url, headers, 'orgs', 'orgRef')
         for org in orgs:
             self.heading(org['@name'], level=0)
             url = org['$']
             resp = self.request(url, headers=headers)
             # add company
             self.add_companies(company=org['@name'])
             # add location
             address = ', '.join((
                 # street address
                 self._enum_ref(resp.json['org']['streetAddress']['line'])
                 [-1]['$'].title(),
                 # city
                 resp.json['org']['city']['$'].title(),
                 # state, postal code or country
                 '%s %s' % (resp.json['org']['iso3166-2']['$'].upper(),
                            resp.json['org']['postalCode']['$'])
                 if 'iso3166-2' in resp.json['org'] else
                 resp.json['org']['iso3166-1']['name']['$'].title(),
             )).strip()
             self.output('Location: %s' % (address))
             self.add_locations(street_address=address)
             # add netblocks
             url = 'http://whois.arin.net/rest/org/%s/nets' % (
                 org['@handle'])
             nets = self._request(url, headers, 'nets', 'netRef')
             for net in nets:
                 try:
                     begin = netblock.strtoip(net['@startAddress'])
                     end = netblock.strtoip(net['@endAddress'])
                     blocks = netblock.lhcidrs(begin, end)
                 except ValueError:
                     self.alert('IPv6 ranges not supported: %s-%s' %
                                (net['@startAddress'], net['@endAddress']))
                     continue
                 for block in blocks:
                     ip = netblock.iptostr(block[0])
                     cidr = '%s/%s' % (ip, str(block[1]))
                     self.output('Netblock: %s' % (cidr))
                     self.add_netblocks(netblock=cidr)
             # add contacts
             url = 'http://whois.arin.net/rest/org/%s/pocs' % (
                 org['@handle'])
             pocLinks = self._request(url, headers, 'pocs', 'pocLinkRef')
             for pocLink in pocLinks:
                 url = pocLink['$']
                 resp = self.request(url, headers=headers)
                 poc = resp.json['poc']
                 emails = self._enum_ref(poc['emails']['email'])
                 for email in emails:
                     fname = poc['firstName'][
                         '$'] if 'firstName' in poc else None
                     lname = poc['lastName']['$']
                     name = ' '.join([x for x in [fname, lname] if x])
                     email = email['$']
                     title = 'Whois contact (%s)' % (
                         pocLink['@description'])
                     city = poc['city']['$'].title()
                     state = poc['iso3166-2']['$'].upper(
                     ) if 'iso3166-2' in poc else None
                     region = ', '.join([x for x in [city, state] if x])
                     country = poc['iso3166-1']['name']['$'].title()
                     self.output('Contact: %s (%s) - %s (%s - %s)' %
                                 (name, email, title, region, country))
                     self.add_contacts(first_name=fname,
                                       last_name=lname,
                                       email=email,
                                       title=title,
                                       region=region,
                                       country=country)