def in_subnet(cidr): """ Returns True if host is within specified subnet, otherwise False """ try: netstart, netsize = cidr.split("/") netsize = int(netsize) except: log.error("Invalid CIDR '{0}'".format(cidr)) return False ifaces = interfaces() netstart_bin = _ipv4_to_bits(netstart) if netsize < 32 and len(netstart_bin.rstrip("0")) > netsize: log.error("Invalid network starting IP '{0}' in CIDR " "'{1}'".format(netstart, cidr)) return False netstart_leftbits = netstart_bin[0:netsize] for ipv4_info in ifaces.values(): for ipv4 in ipv4_info.get("inet", []): if ipv4["address"] == "127.0.0.1": continue if netsize == 32: if netstart == ipv4["address"]: return True else: ip_leftbits = _ipv4_to_bits(ipv4["address"])[0:netsize] if netstart_leftbits == ip_leftbits: return True return False
def in_subnet(cidr): ''' Returns True if host is within specified subnet, otherwise False ''' try: netstart,netsize = cidr.split('/') netsize = int(netsize) except: log.error('Invalid CIDR \'{0}\''.format(cidr)) return False ifaces = interfaces() netstart_bin = _ipv4_to_bits(netstart) if netsize < 32 and len(netstart_bin.rstrip('0')) > netsize: log.error('Invalid network starting IP \'{0}\' in CIDR ' '\'{1}\''.format(netstart,cidr)) return False netstart_leftbits = netstart_bin[0:netsize] for ipv4_info in ifaces.values(): for ipv4 in ipv4_info.get('inet',[]): if ipv4['address'] == '127.0.0.1': continue if netsize == 32: if netstart == ipv4['address']: return True else: ip_leftbits = _ipv4_to_bits(ipv4['address'])[0:netsize] if netstart_leftbits == ip_leftbits: return True return False
def in_subnet(cidr): ''' Returns True if host is within specified subnet, otherwise False ''' try: netstart, netsize = cidr.split('/') netsize = int(netsize) except: log.error('Invalid CIDR \'{0}\''.format(cidr)) return False ifaces = interfaces() netstart_bin = _ipv4_to_bits(netstart) if netsize < 32 and len(netstart_bin.rstrip('0')) > netsize: log.error('Invalid network starting IP \'{0}\' in CIDR ' '\'{1}\''.format(netstart, cidr)) return False netstart_leftbits = netstart_bin[0:netsize] for ipv4_info in ifaces.values(): for ipv4 in ipv4_info.get('inet', []): if ipv4['address'] == '127.0.0.1': continue if netsize == 32: if netstart == ipv4['address']: return True else: ip_leftbits = _ipv4_to_bits(ipv4['address'])[0:netsize] if netstart_leftbits == ip_leftbits: return True return False
def subnets(): ''' Returns a list of subnets to which the host belongs ''' ifaces = interfaces() subnets = [] for ipv4_info in ifaces.values(): for ipv4 in ipv4_info.get('inet',[]): if ipv4['address'] == '127.0.0.1': continue network = _calculate_subnet(ipv4['address'],ipv4['netmask']) subnets.append(network) return subnets
def subnets(): ''' Returns a list of subnets to which the host belongs ''' ifaces = interfaces() subnets = [] for ipv4_info in ifaces.values(): for ipv4 in ipv4_info.get('inet', []): if ipv4['address'] == '127.0.0.1': continue network = _calculate_subnet(ipv4['address'], ipv4['netmask']) subnets.append(network) return subnets
def subnets(): """ Returns a list of subnets to which the host belongs """ ifaces = interfaces() subnets = [] for ipv4_info in ifaces.values(): for ipv4 in ipv4_info.get("inet", []): if ipv4["address"] == "127.0.0.1": continue network = _calculate_subnet(ipv4["address"], ipv4["netmask"]) subnets.append(network) return subnets