def _get_available_ips(self, context, amount): """Returns IP addresses from allowed IP range if there are unused IPs. :returns: IP addresses as list of text types :raises: exception.NetworkBadConfigurationException """ ips = [] if amount < 1: return ips iterator = netaddr.iter_unique_ips(*self.allowed_cidrs) for ip in iterator: ip = six.text_type(ip) if (ip in self.reserved_addresses or self.db.network_allocations_get_by_ip_address( context, ip)): continue else: ips.append(ip) if len(ips) == amount: return ips msg = _("No available IP addresses left in CIDRs %(cidrs)s. " "Requested amount of IPs to be provided '%(amount)s', " "available only '%(available)s'.") % { 'cidrs': self.allowed_cidrs, 'amount': amount, 'available': len(ips) } raise exception.NetworkBadConfigurationException(reason=msg)
def _get_available_ips(self, context, amount): """Returns IP addresses from allowed IP range if there are unused IPs. :returns: IP addresses as list of text types :raises: exception.NetworkBadConfigurationException """ ips = [] if amount < 1: return ips iterator = netaddr.iter_unique_ips(*self.allowed_cidrs) for ip in iterator: ip = six.text_type(ip) if (ip in self.reserved_addresses or self.db.network_allocations_get_by_ip_address(context, ip)): continue else: ips.append(ip) if len(ips) == amount: return ips msg = _("No available IP addresses left in CIDRs %(cidrs)s. " "Requested amount of IPs to be provided '%(amount)s', " "available only '%(available)s'.") % { 'cidrs': self.allowed_cidrs, 'amount': amount, 'available': len(ips)} raise exception.NetworkBadConfigurationException(reason=msg)
def _format_ips(iptokens): ipaddrs = set() for iptoken in iptokens: ipn_list = _convert_ipstring_to_ipn(iptoken) for ipn in ipn_list: ips = [ip for ip in netaddr.iter_unique_ips(ipn)] ipaddrs = ipaddrs.union(ips) return ipaddrs
def parse_target_hosts(self, hosts, port): """Parses a list of hosts""" if netaddr: if netaddr.valid_nmap_range(hosts): for ip in netaddr.iter_nmap_range(hosts): yield (str(ip), port) else: for ip in netaddr.iter_unique_ips(hosts): yield (str(ip), port) else: yield (hosts, port)
def from_file(file): ips=[] OPENFILE=open(file,'r') for line in OPENFILE: if "/" in line: subnet_ips=iter_unique_ips(line) for ip in subnet_ips: ips.append(ip) else: tmp=line.rstrip() ips.append(tmp) return ips
def parse_target_hosts(target_hosts, target_ports): for port in parse_target_ports(target_ports): for host in target_hosts.split(','): if netaddr: if netaddr.valid_nmap_range(host): for ip in netaddr.iter_nmap_range(host): yield (ip, port) else: for ip in netaddr.iter_unique_ips(host): yield (ip, port) else: yield (host, port)
def parse_target_hosts(target_hosts, target_ports): for port in target_ports.split(','): for host in target_hosts.split(','): if netaddr: if netaddr.valid_nmap_range(host): for ip in netaddr.iter_nmap_range(host): yield (ip, port) else: for ip in netaddr.iter_unique_ips(host): yield (ip, port) else: yield(host, port)
def expand_cidr(plage, out="/dev/stdout"): """ Transforme une plage CIDR en une liste d'IP unique et concatene le resultat dans le fichier "out" """ regexp = re.compile("([0-9]{1,3}.){3}[0-9]{1,3}/[0-9]{0,2}") if not re.match(regexp, plage): return 0 with open(out, 'a') as f: for ip in netaddr.iter_unique_ips(plage): f.write("%s\n" % str(ip)) return 0
def format_access_list(access_string, deny_access=None): """Transform access string into a format ganesha understands.""" ipaddrs = set() deny_ipaddrs = set() # handle the case where there is an access string with a trailing comma access_string = access_string.strip(',') iptokens = access_string.split(',') if deny_access: for deny_token in deny_access.split(','): deny_ipns = _convert_ipstring_to_ipn(deny_token) for deny_ipn in deny_ipns: deny_ips = [ip for ip in netaddr.iter_unique_ips(deny_ipn)] deny_ipaddrs = deny_ipaddrs.union(deny_ips) for ipstring in iptokens: ipn_list = _convert_ipstring_to_ipn(ipstring) for ipn in ipn_list: ips = [ip for ip in netaddr.iter_unique_ips(ipn)] ipaddrs = ipaddrs.union(ips) ipaddrs = ipaddrs - deny_ipaddrs ipaddrlist = sorted(list(ipaddrs)) return ','.join([str(ip) for ip in ipaddrlist])
def _get_available_ips(self, context, nova_net, amount): """Returns unused IP addresses from provided Nova network. :param context: RequestContext instance :param nova_net: dict -- dictionary with data of nova network :param amount: int - amount of IP addresses to return :returns: IP addresses as list of text types :raises: exception.NetworkBadConfigurationException """ cidr = nova_net['cidr'] or nova_net['cidr_v6'] reserved = ( six.text_type(netaddr.IPNetwork(cidr).network), nova_net['gateway'], nova_net['gateway_v6'], nova_net['dhcp_server'], nova_net['broadcast'], nova_net['vpn_private_address'], nova_net['vpn_public_address'], nova_net['dns1'], nova_net['dns2']) ips = [] iterator = netaddr.iter_unique_ips(cidr) for ip in iterator: ip = six.text_type(ip) if ip in reserved: # This IP address is reserved for service needs continue elif self.db.network_allocations_get_by_ip_address(context, ip): # Some existing share server already uses this IP address continue fixed_ip = self.nova_api.fixed_ip_get(self.admin_context, ip) if fixed_ip.get('host') or fixed_ip.get('hostname'): # Some Nova VM already uses this IP address continue ips.append(ip) if len(ips) == amount: return ips msg = _("No available IP addresses left in network '%(net_id)s' with " "CIDR %(cidr)s. Requested amount of IPs to be provided " "'%(amount)s', available only '%(available)s'") % dict( net_id=nova_net['id'], cidr=cidr, amount=amount, available=len(ips)) LOG.error(msg) raise exception.NetworkBadConfigurationException(reason=msg)
def expand_cidr(plage, out="/dev/stdout", mode="a"): """ Expands a CIDR range and write result into output file. >>> expand_cidr("192.168.0.0/30", "/dev/null", "w") 0 >>> expand_cidr("783.128.0.0/13", "/dev/null", "w") Traceback (most recent call last): ... AddrFormatError: invalid IPNetwork 783.128.0.0/13 """ regexp = re.compile("([0-9]{1,3}.){3}[0-9]{1,3}/[0-9]{0,2}") if not re.match(regexp, plage): return -1 with open(out, mode) as f: for ip in netaddr.iter_unique_ips(plage): f.write("%s\n" % str(ip)) return 0
def expand_cidr(plage, out="/dev/stdout", mode='w'): """ Transforme une plage CIDR en une liste d'IP unique et concatene le resultat dans le fichier "out" """ try: from netaddr import iter_unique_ips except ImportError as ie : print("[-] %s" % ie) return -1 regexp = compile("([0-9]{1,3}.){3}[0-9]{1,3}/[0-9]{0,2}") if not match(regexp, plage): return 0 with open(out, mode) as fd: try: for ip in iter_unique_ips(plage): fd.write("%s\n" % str(ip)) except Exception as e: print("[-] %s" % e) return -1 return 0
def expand_cidr(plage, out="/dev/stdout", mode='w'): """ Transforme une plage CIDR en une liste d'IP unique et concatene le resultat dans le fichier "out" """ try: from netaddr import iter_unique_ips except ImportError as ie: print("[-] %s" % ie) return -1 regexp = compile("([0-9]{1,3}.){3}[0-9]{1,3}/[0-9]{0,2}") if not match(regexp, plage): return 0 with open(out, mode) as fd: try: for ip in iter_unique_ips(plage): fd.write("%s\n" % str(ip)) except Exception as e: print("[-] %s" % e) return -1 return 0
def expandCidr(plage, out="/dev/stdout"): """ Transforme une plage CIDR en une liste d'IP unique et concatene le resultat dans le fichier "out" """ try: from netaddr import iter_unique_ips except ImportError, ie : print("%s" % ie) return -1 regexp = compile("([0-9]{1,3}.){3}[0-9]{1,3}/[0-9]{0,2}") if not match(regexp, plage): return 0 ip_list = iter_unique_ips(plage) iterator = ip_list.__iter__() fd = open(out, 'a') while True: try: ip = iterator.next() fd.write("%s\n" % str(ip)) except: break fd.close() return 0 def ip2int(ip, base=10):
#!/usr/bin/env python from c2sdk import rest from netaddr import IPNetwork, iter_unique_ips import os import socket import fcntl import struct import subprocess local_ip = os.environ["LOCAL_IP"] netmask = os.environ["NETMASK"] cidr = IPNetwork(local_ip + "/" + netmask).cidr print str(cidr) alive_hosts = [] for ip in iter_unique_ips(cidr): res = subprocess.call(['ping', '-c', '1', '-t', '1', str(ip)]) if res == 0: alive_hosts.append(ip) print alive_hosts http = rest.Rester() http.post("PingSweepResult", { "subnet": str(cidr), "hosts_alive": ",".join(alive_hosts) })
def expandCidr(plage, out="/dev/stdout"): """ Transforme une plage CIDR en une liste d'IP unique et concatene le resultat dans le fichier "out" """ try: from netaddr import iter_unique_ips except ImportError, ie: print("%s" % ie) return -1 regexp = compile("([0-9]{1,3}.){3}[0-9]{1,3}/[0-9]{0,2}") if not match(regexp, plage): return 0 ip_list = iter_unique_ips(plage) iterator = ip_list.__iter__() fd = open(out, 'a') while True: try: ip = iterator.next() fd.write("%s\n" % str(ip)) except: break fd.close() return 0 def ip2int(ip, base=10):