def get_actual_ips(net): # Make range object r = IpRangeList(net) # Generate list of all addresses ips = list(r.__iter__()) # Remove the broadcast ips.pop() # Remove the network ID ips = ips[1:] # Done return ips
def get_reserved_hosts(subnet): #res = dbconn.execute("SELECT address FROM ip.addresses WHERE api.get_address_range(address) IN (SELECT name FROM ip.ranges WHERE subnet = '"+subnet.get_subnet()+"' AND use='RESV') ORDER BY address") res = dbconn.execute("SELECT name,first_ip,last_ip FROM ip.ranges WHERE use='RESV' AND subnet='"+subnet.get_subnet()+"' ORDER BY first_ip") ips = [] for r in res.fetchall(): r = dict(r) range = IpRangeList((r['first_ip'],r['last_ip'])) ips += list(range.__iter__()) #if r['address'] not in ips: # ips.append(r['address']) return ips
def get_reserved_hosts(subnet): #res = dbconn.execute("SELECT address FROM ip.addresses WHERE api.get_address_range(address) IN (SELECT name FROM ip.ranges WHERE subnet = '"+subnet.get_subnet()+"' AND use='RESV') ORDER BY address") res = dbconn.execute( "SELECT name,first_ip,last_ip FROM ip.ranges WHERE use='RESV' AND subnet='" + subnet.get_subnet() + "' ORDER BY first_ip") ips = [] for r in res.fetchall(): r = dict(r) range = IpRangeList((r['first_ip'], r['last_ip'])) ips += list(range.__iter__()) #if r['address'] not in ips: # ips.append(r['address']) return ips
def _cidr_to_iplist(cidr): try: ip_range = IpRangeList(cidr) return list(ip_range) except TypeError: print("[!] That's not a valid IP address or CIDR") return False
class Subnet: def __init__(self, subnet, vlan): if APP_DEBUG is True: print "subnet:init subnet" self.subnet = subnet self.vlan = vlan self.r = IpRangeList(self.subnet) def get_subnet(self): if APP_DEBUG is True: print "subnet:get_subnet" return self.subnet def get_vlan(self): if APP_DEBUG is True: print "subnet:get_vlan" return self.vlan def get_ips(self): if APP_DEBUG is True: print "subnet:get_ips" return list(self.r.__iter__()) def get_ip_count(self): if APP_DEBUG is True: print "subnet:get_ip_count" return len(self.get_ips()) def get_display_columns(self): if APP_DEBUG is True: print "subnet:get_display_columns" return int(ceil(self.get_ip_count()/float(DISPLAY_HOSTS_PER_COL))) def get_subnet_url(self): return self.subnet.replace('/','%2F')
def process_request(self, request): user = request.user if not user.is_authenticated(): for groupname, ranges in settings.IP_AUTH_GROUPS.iteritems(): ip_range = IpRangeList(*ranges) if request.META['REMOTE_ADDR'] in ip_range: request.user = users[groupname]
def main(input): ips = sorted( set(ip_re.match(line).group(0) for line in input if ip_re.match(line))) ips = filter(lambda ip: ip not in private_nets, ips) public_nets = IpRangeList() whois = cymruwhois.Client() nets = (iprangelistappend(whois.lookup(ip), public_nets) for ip in ips if ip not in public_nets) for prefix, owner in nets: print prefix, owner
def log_network(project, resource, log_id): try: zones = get_zones(project) except HttpError as e: code, reason = error_info(e) # skip projects for which compute API is not enabled if reason == 'accessNotConfigured': return else: raise rules = get_fw_rules(project) routes = get_routes(project) api = get_api('compute').instances() for zone in zones: req = api.list(project=project, zone=zone) paging_batch( lambda batch: log_write( map( lambda i: to_network_entry( i, filter(lambda r: rule_applies(i, r), rules), filter( lambda r: instance_ip_in_range( i, IpRangeList(r['destRange'])), routes)), batch), resource, log_id), api, req) break
#!/usr/bin/env python # -*- coding: utf-8 -*- # https://habrahabr.ru/post/127822/ import re, cymruwhois from sys import argv, stdin from iptools import IpRange, IpRangeList private_nets = IpRangeList("10/8", "172.16/12", "192.168/16") ip_re = re.compile( r'^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])' ) def iprangelistappend(match, rangelist): rangelist.ips += (IpRange(match.prefix), ) return match.prefix, match.owner def main(input): ips = sorted( set(ip_re.match(line).group(0) for line in input if ip_re.match(line))) ips = filter(lambda ip: ip not in private_nets, ips) public_nets = IpRangeList() whois = cymruwhois.Client() nets = (iprangelistappend(whois.lookup(ip), public_nets) for ip in ips if ip not in public_nets) for prefix, owner in nets: print prefix, owner
def return_ips(conf, conf_tag): return set(k for k in IpRangeList(*[ x.strip() for x in conf.get(conf_tag, '').split(',') if x.strip() ]))
def __init__(self, subnet, vlan): if APP_DEBUG is True: print "subnet:init subnet" self.subnet = subnet self.vlan = vlan self.r = IpRangeList(self.subnet)
def load_whitelist(self): logging.debug("loading whitelist from {}".format(self.whitelist_path)) self.whitelist_timestamp = os.path.getmtime(self.whitelist_path) self.whitelist = {} with open(self.whitelist_path, 'r') as fp: for line in fp: line = line.strip() if line == '' or line.startswith('#'): continue try: key, value = line.split(':', 1) except Exception as e: logging.error( "unable to parse whitelist item {}: {}".format( line, str(e))) continue if key not in VALID_WHITELIST_KEYS: logging.error("invalid whitelist key {}".format(key)) continue # a little sanity check on the input if value is None or value.strip() == '': logging.error( "empty or all whitelist value for {}".format(key)) continue if key not in self.whitelist: self.whitelist[key] = set() self.whitelist[key].add(value.strip()) # translate all the IP addresses to iptools.IpRange if WHITELIST_TYPE_HTTP_SRC_IP in self.whitelist: temp = set() for value in self.whitelist[WHITELIST_TYPE_HTTP_SRC_IP]: try: temp.add(IpRangeList(value)) except Exception as e: logging.error( "unable to translate {} to an IPv4 in brotex whitelist: {}" .format(value, e)) continue self.whitelist[WHITELIST_TYPE_HTTP_SRC_IP] = temp # translate all the IP addresses to iptools.IpRange if WHITELIST_TYPE_HTTP_DEST_IP in self.whitelist: temp = set() for value in self.whitelist[WHITELIST_TYPE_HTTP_DEST_IP]: try: temp.add(IpRangeList(value)) except Exception as e: logging.error( "unable to translate {} to an IPv4 in brotex whitelist: {}" .format(value, e)) continue self.whitelist[WHITELIST_TYPE_HTTP_DEST_IP] = temp
def rule_range_apply(instance, rule, rule_key='sourceRanges'): instance_ip_in_range(instance, IpRangeList(*rule.get(rule_key, [])))
from iptools import IpRangeList ip_private = IpRangeList('0.0.0.0/8', '10.0.0.0/8', '100.64.0.0/10', '127.0.0.0/8', '169.254.0.0/16', '172.16.0.0/12', '192.0.0.0/24', '192.0.2.0/24', '192.88.99.0/24', '192.168.0.0/16', '198.18.0.0/15', '198.51.100.0/24', '203.0.113.0/24', '224.0.0.0/4', '240.0.0.0/4', '255.255.255.255/32')