def validatePrefix(self, prefix): """ Validates an Internet network or ip address (/32). :param prefix, The network in CIDR notation. :return, Text of error from cidrize on error, otherwise None. """ try: cidrize.cidrize(prefix) except cidrize.CidrizeError: return sys.exc_value else: return None
def getNetworkLocations(map): f = open("GeoIPCountryWhois.csv", "r") networkLatLon = {} #for i in range( 160223 ): for i in range(100): try: whois = f.readline().split(",") except EOFError: break networkFromIP = whois[0].strip('"') networkToIP = whois[1].strip('"') if networkFromIP == networkToIP: continue ip_range = str(networkFromIP) + "-" + str(networkToIP) cidr_ip = cidrize(ip_range) gi = pygeoip.GeoIP("/usr/local/share/GeoIP/GeoIPCity.dat", pygeoip.STANDARD) try: gir = gi.record_by_addr(networkFromIP) except pygeoip.GeoIPError: print 'Error in:', networkFromIP if gir != None: x, y = map(gir['longitude'], gir['latitude']) networkLatLon[i] = {'xCoord': x, 'yCoord': y, 'cidr': cidr_ip} f.close() return networkLatLon
def parse_scope(): timewarn = 0 print stat+"Parsing input file to CIDR ranges" with open(filein) as f: lines = f.readlines() for line in lines: if '-' in line: if line.split("-")[0] == line.split("-")[1].rstrip(): line = line.split("-")[0] try: cidrfound = str(cidrize(line)).split("'")[1] cidrs.append(cidrfound) except: parserr.append(line) pass if len(parserr) > 0: print warn+"Error parsing the following lines:" for line in parserr: print " "+line.rstrip() print stat+"Parsing CIDR ranges to IP addresses" for cidr in cidrs: if timewarn == 0 and int(cidr.split('/')[1]) < 17: print warn+"One or more large ranges (/16+) detected" print warn+"This is not a problem, but may take additional time to parse" timewarn = 1 for ip in IPNetwork(cidr): if str(ip) not in scope: scope.append(str(ip)) with open(fullpath+"inscope-ips.txt","a+") as f: f.write(str(ip)+'\n') print good+"Total in scope IP addresses: "+str(len(scope)) return scope
def validate_subnet(subnet): if subnet: try: ip = ipaddress.ip_address(subnet) subbed = ipaddress.ip_network(ip) return subbed except: try: subbed = ipaddress.ip_network(subnet) return subbed except: try: subbed = ipaddress.ip_network(subnet, strict=False) msg = 'Address %s is not valid CIDR syntax, using recommended CIDR: %s' % ( subnet, subbed) print(msg) return subbed except: try: cidrip = cidrize(subnet) subbed = ipaddress.ip_network(cidrip) return subbed except: msg = 'Subnet %s is not valid CIDR syntax, cannot process!' % ( subnet) print(msg)
def getNetworkLocations( map ): f = open( "GeoIPCountryWhois.csv", "r" ) networkLatLon = {} #for i in range( 160223 ): for i in range( 100 ): try: whois = f.readline().split(",") except EOFError: break networkFromIP = whois[0].strip( '"' ) networkToIP = whois[1].strip( '"' ) if networkFromIP == networkToIP: continue ip_range = str( networkFromIP ) + "-" + str( networkToIP ) cidr_ip = cidrize( ip_range ) gi = pygeoip.GeoIP( "/usr/local/share/GeoIP/GeoIPCity.dat", pygeoip.STANDARD ) try: gir = gi.record_by_addr( networkFromIP ) except pygeoip.GeoIPError: print 'Error in:', networkFromIP if gir != None: x,y = map( gir[ 'longitude' ], gir[ 'latitude' ] ) networkLatLon[ i ] = { 'xCoord': x, 'yCoord': y, 'cidr': cidr_ip } f.close() return networkLatLon
def range_to_ips(iprange): list_of_ips = [] if iprange: try: ipaddr = ipaddress.ip_address(iprange) list_of_ips.append(ipaddr) except: try: ipaddrs = ipaddress.ip_network(iprange) for ipaddr in ipaddrs: list_of_ips.append(ipaddr) except: try: ipaddrs = ipaddress.ip_network(iprange, strict=False) msg = 'Address %s is not valid CIDR syntax, using recommended CIDR: %s' % ( iprange, ipaddrs) print(msg) for ipaddr in ipaddrs: list_of_ips.append(ipaddr) except: try: cidrip = cidrize(iprange) for cidr in cidrip: ipaddrs = ipaddress.ip_network(cidr) for ipaddr in ipaddrs: list_of_ips.append(ipaddr) except: msg = 'Address %s is not valid CIDR syntax, cannot process!' % ( iprange) print(msg) return list_of_ips
def main(): for x in [ '32', '31', '30', '29', '28', '27', '26', '25', '24', '23', '22', '21', '20', '19', '18', '17', '16', '15', '14', '13', '12', '11', '10', '9', '8' ]: ipaddr = [] try: ipaddr = sys.argv[1] + '/' + x except IndexError: print "usage: %s 1.2.3.4" % _SELF sys.exit(-1) try: cidr = cidrize(ipaddr, modular=False) if cidr: print output_both(cidr, validate=True) except IndexError, err: sys.exit(-1)
def main(): for x in [ "32", "31", "30", "29", "28", "27", "26", "25", "24", "23", "22", "21", "20", "19", "18", "17", "16", "15", "14", "13", "12", "11", "10", "9", "8", ]: ipaddr = [] try: ipaddr = sys.argv[1] + "/" + x except IndexError: print "usage: %s 1.2.3.4" % _SELF sys.exit(-1) try: cidr = cidrize(ipaddr, modular=False) if cidr: print output_both(cidr, validate=True) except IndexError, err: sys.exit(-1)
def test_output_str(self): cidr = cidrize.cidrize('10.20.30.40-50', strict=True) sep = ', ' expected = '10.20.30.40/29, 10.20.30.48/31, 10.20.30.50/32' self.assertEqual(expected, cidrize.output_str(cidr, sep))
def test_dump(self): cidr = cidrize.cidrize('1.2.3.*') result = cidrize.dump(cidr) self.assertEqual(str, type(result))
def test_range6(self): cidr = cidrize.cidrize('2001:1234::0.0.64.0-2001:1234::FFff') sep = ', ' expected = '2001:1234::4000/114, 2001:1234::8000/113' self.assertEqual(expected, cidrize.output_str(cidr, sep))