def is_match_ip_space(self, ip_addr, ip_space): """ Passed an IP address and an IP address space and check if the IP address belongs to the IP address space. If it does return 1 otherwise return 0 """ #*** Does ip_space look like a CIDR network?: if "/" in ip_space: try: ip_space_object = IPNetwork(ip_space) except: exc_type, exc_value, exc_traceback = sys.exc_info() self.logger.error("error=E1000015 " "Exception converting to IPNetwork object. " "Exception %s, %s, %s", exc_type, exc_value, exc_traceback) return 0 #*** Does it look like an IP range?: elif "-" in ip_space: ip_range = ip_space.split("-") if len(ip_range) != 2: self.logger.error("error=E1000016 " "Range split of ip_space %s on - was not len 2 but %s", ip_space, len(ip_range)) return 0 try: ip_space_object = list(iter_iprange(ip_range[0], ip_range[1])) except: exc_type, exc_value, exc_traceback = sys.exc_info() self.logger.error("error=E1000017 " "Exception on conversion of %s to iter_iprange " "Exception %s, %s, %s", ip_range, exc_type, exc_value, exc_traceback) return 0 else: #*** Or is it just a plain simple IP address?: try: ip_space_object = list(iter_iprange(ip_space, ip_space)) except: exc_type, exc_value, exc_traceback = sys.exc_info() self.logger.error("error=E1000019 " "Exception converting to IPAddress object. " "Exception %s, %s, %s", exc_type, exc_value, exc_traceback) return 0 #*** Convert the IP address to a netaddr IPAddress object: try: ip_addr_object = IPAddress(ip_addr) except: exc_type, exc_value, exc_traceback = sys.exc_info() self.logger.error("error=E1000021 " "Exception converting to IPAddress object. " "Exception %s, %s, %s", exc_type, exc_value, exc_traceback) return 0 #*** Now we have both in netaddr form, so do the match comparison: if ip_addr_object in ip_space_object: return 1 else: return 0
def generate_scope(scope_file): """Parse IP ranges inside the provided scope file to expand IP ranges. This supports ranges with hyphens, underscores, and CIDRs. Parameters: scope_file A file containing domain names and IP addresses/ranges """ scope = [] try: with open(scope_file,"r") as scope_file: for target in scope_file: target = target.rstrip() # Record individual IPs and expand CIDRs if is_ip(target): ip_list = list(IPNetwork(target)) for address in sorted(ip_list): str_address = str(address) scope.append(str_address) # Sort IP ranges from domain names and expand the ranges if not is_domain(target): # Check for hyphenated ranges like those accepted by Nmap # Ex: 192.168.1.1-50 will become 192.168.1.1 ... 192.168.1.50 if "-" in target: target = target.rstrip() parts = target.split("-") startrange = parts[0] b = parts[0] dot_split = b.split(".") temp = "." # Join the values using a "." so it makes a valid IP combine = dot_split[0],dot_split[1],dot_split[2],parts[1] endrange = temp.join(combine) # Calculate the IP range ip_list = list(iter_iprange(startrange,endrange)) # Iterate through the range and remove ip_list for x in ip_list: temp = str(x) scope.append(temp) # Check if range has an underscore because underscores are fine, I guess? # Ex: 192.168.1.2_192.168.1.155 elif "_" in target: target = target.rstrip() parts = target.split("_") startrange = parts[0] endrange = parts[1] ip_list = list(iter_iprange(startrange,endrange)) for address in ip_list: str_address = str(address) scope.append(str_address) else: scope.append(target.rstrip()) except IOError as error: click.secho("[!] Parsing of scope file failed!",fg="red") click.secho("L.. Details: {}".format(error),fg="red") return scope
def prepare_scope(scope_file,expanded_scope): """Parse IP ranges inside the provided scope file to expand IP ranges. This supports ranges with hyphens, underscores, and CIDRs. Parameters: scope_file A file containing domain name and IP addresses/ranges expanded_scope A list object for storing to expanded scope list """ try: with open(scope_file,"r") as scope_file: for target in scope_file: target = target.rstrip() # Record individual IPs and expand CIDRs if helpers.is_ip(target): ip_list = list(IPNetwork(target)) for address in sorted(ip_list): str_address = str(address) expanded_scope.append(str_address) # Sort IP ranges from domain names and expand the ranges if not helpers.is_domain(target): # Check for hyphenated ranges like those accepted by Nmap, e.g. 192.168.1.1-50 if "-" in target: target = target.rstrip() parts = target.split("-") startrange = parts[0] b = parts[0] dot_split = b.split(".") temp = "." # Join the values using a "." so it makes a valid IP combine = dot_split[0],dot_split[1],dot_split[2],parts[1] endrange = temp.join(combine) # Calculate the IP range ip_list = list(iter_iprange(startrange,endrange)) # Iterate through the range and remove ip_list for x in ip_list: temp = str(x) expanded_scope.append(temp) # Check if range has an underscore, e.g. 192.168.1.2_192.168.1.155 elif "_" in target: target = target.rstrip() parts = target.split("_") startrange = parts[0] endrange = parts[1] ip_list = list(iter_iprange(startrange,endrange)) for address in ip_list: str_address = str(address) expanded_scope.append(str_address) else: expanded_scope.append(target.rstrip()) click.secho("[+] Scope list expanded to {} items. Proceeding with verification \ checks.".format(len(expanded_scope)),fg="green") except IOError as error: click.secho("[!] Parsing of scope file failed!",fg="red") click.secho("L.. Details: {}".format(error),fg="red")
def get_subnet(self, start='10.10.1.0', end='10.10.255.0', step=256): subnet_gen = netaddr.iter_iprange(start, end, step=step) i = 1 while True: with self.lock: try: yield (i, str(subnet_gen.next())) except StopIteration: subnet_gen = netaddr.iter_iprange(start, end, step=step) yield (i, str(subnet_gen.next())) i += 1
def verify_ips(to_verify): """ Responsible for verifying that IP addresses are formatted properly. IPs must first be valid in the xxx.xxx.xxx.xxx format. Next, IP ranges or CIDR addresses also need to be valid i.e. xxx.xxx.xxx.xxx-xxx.xxx.xxx.xxx or xxx.xxx.xxx.xxx/xx. Also will take IP ranges or CIDR addresses and parse out each individual IP in the range and save it to a file for use by masscan. """ logging.debug("Verifying IPs") try: addresses = [] #Will try to open the string as a file, if it errors out it will be caught and the string will be interpreted as a list of IPs instead. try: with open(to_verify) as file: lines = file.read().splitlines() except FileNotFoundError: lines = to_verify.split(",") for i in lines: if "/" in i: for j in IPNetwork(i): addresses.append(str(j)) elif "-" in i: ip_range = i.split('-') for i in list(iter_iprange(ip_range[0], ip_range[1])): addresses.append(str(i)) else: addresses.append(str(IPAddress(i))) logging.debug("Finished Verifying IPs") write_ips(addresses) return addresses except AddrFormatError as err: logging.critical( "Error with verifying IPs. Exiting Program.\nError: %s" % err) exit()
def add_to_targetipaddr_db(instance): """Adds IP addresses as individual entries in TargetIpAddr database.""" if instance.target_type == Target.IPADDR: if '-' in instance.target: iprange = instance.target.split('-') iplist = list(netaddr.iter_iprange(iprange[0], iprange[1])) elif '/' in instance.target: iplist = list(netaddr.IPNetwork(instance.target)) else: iplist = [instance.target] for ipaddr in iplist: try: # grab pre-existing entry ip_entry = TargetIpAddr.objects.get( ipaddr=ipaddr, ) if ip_entry.ipaddr_action != instance.target_action: raise ValidationError({ 'target': [ 'Target "%s" is already marked for action "%s"' % ( ip_entry.ipaddr, ip_entry.ipaddr_action) ] }) except TargetIpAddr.DoesNotExist: # create new entry ip_entry = TargetIpAddr( ipaddr=ipaddr, ipaddr_action=instance.target_action, method=instance.method, ) ip_entry.save() # associate IP address to target ip_entry.target.add(instance)
def expand_ips(start_ip, end_ip): list_ip = [] ips = netaddr.cidr_merge(list(netaddr.iter_iprange(start_ip, end_ip))) for i in ips: ip = str(i.ip) + '/' + str(i.netmask) list_ip.append(ip) return list_ip
def export_network_file(dha, network, output_path): install_network_env = {} host_network_env = {} ip_settings = network['ip_settings'] mgmt_net = [item for item in ip_settings if item['name'] == 'mgmt'][0] mgmt_gw = mgmt_net['gw'] mgmt_cidr = mgmt_net['cidr'] prefix = int(mgmt_cidr.split('/')[1]) mgmt_netmask = '.'.join([ str((0xffffffff << (32 - prefix) >> i) & 0xff) for i in [24, 16, 8, 0] ]) dhcp_ip_range = ' '.join(mgmt_net['dhcp_ranges'][0]) internal_vip = network['internal_vip']['ip'] install_network_env.update({'INSTALL_GW': mgmt_gw}) install_network_env.update({'INSTALL_CIDR': mgmt_cidr}) install_network_env.update({'INSTALL_NETMASK': mgmt_netmask}) install_network_env.update({'INSTALL_IP_RANGE': dhcp_ip_range}) install_network_env.update({'VIP': internal_vip}) export_env_dict(install_network_env, output_path) pxe_nic = os.environ['PXE_NIC'] host_ip_range = mgmt_net['ip_ranges'][0] host_ips = netaddr.iter_iprange(host_ip_range[0], host_ip_range[1]) host_networks = [] for host in dha['hosts']: host_name = host['name'] host_ip = str(host_ips.next()) host_networks.append('{0}:{1}={2}|is_mgmt'.format( host_name, pxe_nic, host_ip)) host_subnets = [item['cidr'] for item in ip_settings] host_network_env.update({'NETWORK_MAPPING': "install=" + pxe_nic}) host_network_env.update({'HOST_NETWORKS': ';'.join(host_networks)}) host_network_env.update({'SUBNETS': ','.join(host_subnets)}) export_env_dict(host_network_env, output_path, True)
def run (self, tmp = None, task_vars = dict ()): database_location = self._task.args.get ("database_location") taken_addresses = set () for ip_path, ip_target \ in self.client.get_tree (database_location): ip_name = ip_path [1:] taken_addresses.add (ip_name) address_range = ( netaddr.iter_iprange ( self._task.args.get ("start_address"), self._task.args.get ("end_address"))) ip_address = next ( ip_address for ip_address in address_range if not str (ip_address) in taken_addresses) allocation_name = ( self._task.args.get ("name")) self.client.create_raw ( "%s/%s" % (database_location, str (ip_address)), allocation_name) return dict ( changed = True, address = ip_address)
def test_ip_range(): ip_list = list(iter_iprange('192.0.2.1', '192.0.2.14')) assert len(ip_list) == 14 assert ip_list == [ IPAddress('192.0.2.1'), IPAddress('192.0.2.2'), IPAddress('192.0.2.3'), IPAddress('192.0.2.4'), IPAddress('192.0.2.5'), IPAddress('192.0.2.6'), IPAddress('192.0.2.7'), IPAddress('192.0.2.8'), IPAddress('192.0.2.9'), IPAddress('192.0.2.10'), IPAddress('192.0.2.11'), IPAddress('192.0.2.12'), IPAddress('192.0.2.13'), IPAddress('192.0.2.14'), ] assert cidr_merge(ip_list) == [ IPNetwork('192.0.2.1/32'), IPNetwork('192.0.2.2/31'), IPNetwork('192.0.2.4/30'), IPNetwork('192.0.2.8/30'), IPNetwork('192.0.2.12/31'), IPNetwork('192.0.2.14/32'), ]
def checkIPRange(self,Ip1,Ip2): try: IPRange=list(netaddr.iter_iprange(Ip1, Ip2)) return IPAddress(self.ClientIp) in IPRange except: print self.ContentId+":An exception occured in checkIPRange function! Please check your ACL!" return False
def ip_range_to_cidr(ip_network_string): """Convert ip_network_string into CIDR notation.""" # Split string into list by ', ' delimiter. ip_network_cidr = [] ip_network_list = ip_network_string.split(',') for ip_object in ip_network_list: # For every ip range ('10.182.71.0-10.182.75.255'), convert to individual slash notation, 10.182.71.0/24, 10.182.72.0/22. if '-' in ip_object: # The object is a range. dash = ip_object.find('-') # First part of ip range. ip_start = ip_object[:dash] # Last part of ip range. ip_end = ip_object[dash + 1:] # Generate lists of IP addresses in range. ip_range = list(netaddr.iter_iprange(ip_start, ip_end)) # Convert start & finish range to CIDR. ip_range = netaddr.cidr_merge(ip_range) # May be one or more objects in list. # Example 1: '10.182.71.0-10.182.75.255' ==> ['10.182.71.0/24, 10.182.72.0/22'] # Example 2: '10.182.90.0-10.182.91.255' ==> ['10.182.90.0/23'] # Add each CIDR to ip_network. for ip_object in ip_range: ip_network_cidr.append(str(ip_object)) else: # The object is not a range, just add it. logging.debug('ip_object = %s' % (ip_object)) ip_network_cidr.append(str(netaddr.IPNetwork(ip_object).cidr)) # Return as a string with delimiter ', ' return ip_network_cidr
def export_network_file(dha, network, ofile): env = {} mgmt_net = [item for item in network['ip_settings'] if item['name'] == 'mgmt'][0] mgmt_gw = mgmt_net['gw'] mgmt_cidr = mgmt_net['cidr'] prefix = int(mgmt_cidr.split('/')[1]) mgmt_netmask = '.'.join([str((0xffffffff << (32 - prefix) >> i) & 0xff) for i in [24, 16, 8, 0]]) dhcp_ip_range = ' '.join(mgmt_net['dhcp_ranges'][0]) env.update({'INSTALL_GW': mgmt_gw}) env.update({'INSTALL_CIDR': mgmt_cidr}) env.update({'INSTALL_NETMASK': mgmt_netmask}) env.update({'INSTALL_IP_RANGE': dhcp_ip_range}) export_env_dict(env, ofile) host_ip_range = mgmt_net['ip_ranges'][0] host_ips = netaddr.iter_iprange(host_ip_range[0], host_ip_range[1]) host_networks = [] for host in dha['hosts']: host_name = host['name'] host_ip = str(host_ips.next()) host_networks.append( "{0}:{1}={2}|is_mgmt".format(host_name, PXE_INTF, host_ip)) host_network_env = {"HOST_NETWORKS": ';'.join(host_networks)} export_env_dict(host_network_env, ofile, True)
def main(): module = AnsibleModule( argument_spec=dict( start_cidr=dict(required=True), group_size=dict(required=False, default="null"), num_ip=dict(required=True) ), supports_check_mode=True ) start_cidr = module.params['start_cidr'] group_size = module.params['group_size'] num_ip = module.params['num_ip'] sandbox_cidr = netaddr.IPNetwork(start_cidr) sandbox_hosts = netaddr.iter_iprange(sandbox_cidr.ip, sandbox_cidr.last) ip_data = t_ip_data() for i in range(0, int(num_ip)): ''' cidr = start_cidr_ip.split('.')[0] + "." + \ start_cidr_ip.split('.')[1] + "." + \ start_cidr_ip.split('.')[2] + "." + \ str(int(start_cidr_ip.split('.')[3]) + i) ''' ip_data.index.append(i % int(group_size)) ip_data.ip_list.append(str(sandbox_hosts.next())) module.exit_json(changed=True,ip_index=ip_data.index, \ ip_index_list=str(ip_data.ip_list), \ prefixlen=str(sandbox_cidr.prefixlen))
def _create_host_endpoint_file(self, ipm, nh): ips = [] for s, e in [(nh.ip_start, nh.ip_end), (nh.ip6_start, nh.ip6_end)]: if s: ips.extend(list(netaddr.iter_iprange(s, e or s))) ep_dict = { "attributes": { "vm-name": ( "snat|" + self.host + "|" + ipm["external_segment_name"] ) }, "policy-space-name": (ipm.get('next_hop_ep_tenant') or ipm['nat_epg_tenant']), "endpoint-group-name": (ipm.get('next_hop_ep_epg') or ipm['nat_epg_name']), "interface-name": nh.next_hop_iface, "ip": [str(x) for x in ips], "mac": nh.next_hop_mac, "uuid": nh.uuid, "promiscuous-mode": True, } epfile = self._write_endpoint_file(nh.es_name, ep_dict) # SNAT EP is no longer stale self._stale_endpoints.discard(os.path.basename(epfile))
def create_lswitch_port(self, lswitch=None, lport_create_args={}, iteration=0, ext_cidr=None): cidr = lswitch.get("cidr", None) if cidr: ip = str( next(netaddr.iter_iprange(cidr.ip + iteration + 1, cidr.last))) ip_mask = '{}/{}'.format(ip, cidr.prefixlen) gw = str(netaddr.IPAddress(cidr.last - 1)) name = "lp_{}".format(ip) else: name = "lp_".join( random.choice(string.ascii_letters) for i in range(10)) ip_mask = "" ip = "" gw = "" if ext_cidr: ext_gw = str(netaddr.IPAddress(ext_cidr.last - 2)) else: ext_gw = "" print("***** creating lport {} *****".format(name)) lswitch_port = self.nbctl.ls_port_add(lswitch["name"], name, mac=str(RandMac()), ip=ip_mask, gw=gw, ext_gw=ext_gw) return lswitch_port
def _set_used_ips(user_defined_config, inventory): """Set all of the used ips into a global list. :param user_defined_config: ``dict`` User defined configuration :param inventory: ``dict`` Living inventory of containers and hosts """ used_ips = user_defined_config.get('used_ips') if isinstance(used_ips, list): for ip in used_ips: split_ip = ip.split(',') if len(split_ip) >= 2: ip_range = list(netaddr.iter_iprange(split_ip[0], split_ip[-1])) USED_IPS.update([str(i) for i in ip_range]) else: logger.debug("IP %s set as used", split_ip[0]) USED_IPS.add(split_ip[0]) # Find all used IP addresses and ensure that they are not used again for host_entry in inventory['_meta']['hostvars'].values(): networks = host_entry.get('container_networks', dict()) for network_entry in networks.values(): address = network_entry.get('address') if address: logger.debug("IP %s set as used", address) USED_IPS.add(address)
def _set_used_ips(user_defined_config, inventory): """Set all of the used ips into a global list. :param user_defined_config: ``dict`` User defined configuration :param inventory: ``dict`` Living inventory of containers and hosts """ used_ips = user_defined_config.get('used_ips') if isinstance(used_ips, list): for ip in used_ips: split_ip = ip.split(',') if len(split_ip) >= 2: ip_range = list(netaddr.iter_iprange(split_ip[0], split_ip[-1])) USED_IPS.extend([str(i) for i in ip_range]) else: append_if(array=USED_IPS, item=split_ip[0]) # Find all used IP addresses and ensure that they are not used again for host_entry in inventory['_meta']['hostvars'].values(): if 'ansible_ssh_host' in host_entry: append_if(array=USED_IPS, item=host_entry['ansible_ssh_host']) for key, value in host_entry.iteritems(): if key.endswith('address'): append_if(array=USED_IPS, item=value)
def get_available_ip(name): ''' Gets the next available IP in the subnet range. NOTE: This could be improved! It is slow and creates a list of all the IPs in the range as well as all of the used IPs Runs a while loop through the generated IP list until it finds an address that is not used Returns next available IP arguments: name: subnet_name ''' subnet_info_obj = Subnet.objects.get(subnet_name=name) mapping_objs = Mapping.objects.filter(subnet=name) ip_start = subnet_info_obj.ip_range_start ip_end = subnet_info_obj.ip_range_end used_ip_list = [] for obj in mapping_objs: #Create list of used IPs used_ip_list.append(obj.ip_or_cname) index = 0 ip_range = list(netaddr.iter_iprange(ip_start, ip_end)) #Create the actual list of all IPs in the range next_ip = str(ip_range[index]) while next_ip in used_ip_list: index += 1 next_ip = str(ip_range[index]) return next_ip
def _gen_interface_ip_lists(cfg): interface_ip_lists = {} interfaces = cfg.get_interfaces() for interface in interfaces: label = interface.label ip_list_prelim = [] ip_list = [] if 'address_list' in interface.keys(): ip_list_prelim = interface.address_list elif 'IPADDR_list' in interface.keys(): ip_list_prelim = interface.IPADDR_list for ip in ip_list_prelim: if '-' in ip: ip_range = ip.split('-') for _ip in iter_iprange(ip_range[0], ip_range[1]): ip_list.append(str(_ip)) else: ip_list.append(ip) if 'address_start' in interface.keys(): ip_list = [IPAddress(interface.address_start)] elif 'IPADDR_start' in interface.keys(): ip_list = [IPAddress(interface.IPADDR_start)] interface_ip_lists[label] = ip_list return interface_ip_lists
def _set_used_ips(user_defined_config, inventory): """Set all of the used ips into a global list. :param user_defined_config: ``dict`` User defined configuration :param inventory: ``dict`` Living inventory of containers and hosts """ used_ips = user_defined_config.get('used_ips') if isinstance(used_ips, list): for ip in used_ips: split_ip = ip.split(',') if len(split_ip) >= 2: ip_range = list( netaddr.iter_iprange( split_ip[0], split_ip[-1] ) ) USED_IPS.extend([str(i) for i in ip_range]) else: append_if(array=USED_IPS, item=split_ip[0]) # Find all used IP addresses and ensure that they are not used again for host_entry in inventory['_meta']['hostvars'].values(): networks = host_entry.get('container_networks', dict()) for network_entry in networks.values(): address = network_entry.get('address') if address: append_if(array=USED_IPS, item=address)
def main(): module = AnsibleModule(argument_spec=dict(start_cidr=dict(required=True), group_size=dict(required=False, default="null"), num_ip=dict(required=True)), supports_check_mode=True) start_cidr = module.params['start_cidr'] group_size = module.params['group_size'] num_ip = module.params['num_ip'] sandbox_cidr = netaddr.IPNetwork(start_cidr) sandbox_hosts = netaddr.iter_iprange(sandbox_cidr.ip, sandbox_cidr.last) ip_data = t_ip_data() for i in range(0, int(num_ip)): ''' cidr = start_cidr_ip.split('.')[0] + "." + \ start_cidr_ip.split('.')[1] + "." + \ start_cidr_ip.split('.')[2] + "." + \ str(int(start_cidr_ip.split('.')[3]) + i) ''' ip_data.index.append(i % int(group_size)) ip_data.ip_list.append(str(sandbox_hosts.next())) module.exit_json(changed=True,ip_index=ip_data.index, \ ip_index_list=str(ip_data.ip_list), \ prefixlen=str(sandbox_cidr.prefixlen))
def _create_lports(self, lswitch, lport_create_args=[], lport_amount=1, lport_ip_shift=1): LOG.info("create %d lports on lswitch %s" % \ (lport_amount, lswitch["name"])) self.RESOURCE_NAME_FORMAT = "lport_XXXXXX_XXXXXX" batch = lport_create_args.get("batch", lport_amount) port_security = lport_create_args.get("port_security", True) LOG.info("Create lports method: %s" % self.install_method) install_method = self.install_method network_cidr = lswitch.get("cidr", None) ip_addrs = None if network_cidr: end_ip = network_cidr.ip + lport_amount + lport_ip_shift if not end_ip in network_cidr: message = _( "Network %s's size is not big enough for %d lports.") raise exceptions.InvalidConfigException( message % (network_cidr, lport_amount)) ip_addrs = netaddr.iter_iprange(network_cidr.ip + lport_ip_shift, network_cidr.last) ovn_nbctl = self.controller_client("ovn-nbctl") ovn_nbctl.set_sandbox("controller-sandbox", install_method) ovn_nbctl.enable_batch_mode() base_mac = [i[:2] for i in self.task["uuid"].split('-')] base_mac[0] = str(hex(int(base_mac[0], 16) & 254)) base_mac[3:] = ['00'] * 3 flush_count = batch lports = [] for i in range(lport_amount): name = self.generate_random_name() lport = ovn_nbctl.lswitch_port_add(lswitch["name"], name) ip = str(ip_addrs.next()) if ip_addrs else "" mac = utils.get_random_mac(base_mac) ovn_nbctl.lport_set_addresses(name, [mac, ip]) if port_security: ovn_nbctl.lport_set_port_security(name, mac) lports.append(lport) flush_count -= 1 if flush_count < 1: ovn_nbctl.flush() flush_count = batch ovn_nbctl.flush() # ensure all commands be run ovn_nbctl.enable_batch_mode(False) return lports
def _scan_handler(self, args): """ Handles 'scan' command-line argument (Re)scans for hosts on the network """ if args.iprange: try: if '-' in args.iprange: iprange = list( netaddr.iter_iprange(*args.iprange.split('-'))) else: iprange = list(netaddr.IPNetwork(args.iprange)) except netaddr.core.AddrFormatError: IO.error('ip range invalid.') return else: iprange = None for host in self.hosts: self._free_host(host) IO.spacer() self.hosts = self.host_scanner.scan(iprange) IO.ok('{}{}{} hosts discovered.'.format(IO.Fore.LIGHTYELLOW_EX, len(self.hosts), IO.Style.RESET_ALL)) IO.spacer()
def _parse_iprange(self, range): try: if '-' in range: return list(netaddr.iter_iprange(*range.split('-'))) else: return list(netaddr.IPNetwork(range)) except netaddr.core.AddrFormatError: return
def handler_ip_range(string): step = 1 if len(string.split(':')) > 1: step = int(string.split(':')[-1]) ip_range = string.split(':')[0] p_boundary = ip_range.split('-') ip_begin = p_boundary[0] ip_end = p_boundary[-1] return map(lambda x: str(x), list(netaddr.iter_iprange(ip_begin, ip_end))[0::step])
def get_addr_from_int (int_d, pool_d, pool): for x, p in enumerate (pool_d['pools']): if pool_d['pools'][x]['id'] == pool: for y, r in enumerate (pool_d['pools'][x]['ranges']): ip_range = list (netaddr.iter_iprange(pool_d['pools'][x]['ranges'][y]['low'],pool_d['pools'][x]['ranges'][y]['high'])) for z, s in enumerate (int_d['ip_addrs']): ip_a = netaddr.IPAddress(int_d['ip_addrs'][z]) if (ip_a in ip_range): return (int_d['ip_addrs'][z])
def get_ip_range(start, end): generator = iter_iprange(start, end, step=1) ips = [] while True: try: ips.append(str(generator.next())) except StopIteration: break return ips
def expand_it(iplist): results = [] for ip in iplist: if type(ip) is list: #take care of range [results.append(address) for address in iter_iprange(sorted(ip)[0], sorted(ip)[1])] else: #expand cidr for address in ip: results.append(address) return results
def __init__(self, definition): self.properties = ['start', 'end'] super(HostDefParserRange, self).__init__(definition) self.hosts = [ Host(ip) for ip in list( netaddr.iter_iprange(self.definition.get('start'), self.definition.get('end'))) ]
def _add_range(self, start, end): """ Takes a start and end range, and creates individual host addresses. :param start: A string containing the start of the range. :param end: A string containing the end of the range. """ ip_list = list(netaddr.iter_iprange(start, end)) for ip in ip_list: self._add_floating(ip)
def get_ip_list(ip: str) -> list: ip_list = [] if "-" in ip: ips = ip.split("-") if len(ips) != 2: raise Exception("IP地址格式错误,请使用:x.x.x.x-x.x.x.x或x.x.x.x/x") ip_list = [str(addr) for addr in netaddr.iter_iprange(ips[0], ips[1], step=1)] elif "/" in ip: ip_list = [addr for addr in netaddr.IPNetwork(ip)] return ip_list
def nova_add_range(self, start, end): """ Takes a start and end range, and creates individual host addresses. :param start: A string containing the start of the range. :param end: A string containing the end of the range. """ ip_list = list(netaddr.iter_iprange(start, end)) for ip in ip_list: self.nova_add_floating(ip)
def _setup_routes(self, if_dev, ver, ip_start, ip_end, gw_ip): gw_ip_net = netaddr.IPNetwork(gw_ip) if_dev.addr.add(ver, "%s/%s" % (ip_start, gw_ip_net.prefixlen), gw_ip_net.broadcast) if_dev.route.add_gateway(str(gw_ip_net.ip)) if ip_start != ip_end: local_nets = netaddr.cidr_merge(netaddr.iter_iprange(ip_start, ip_end)) max_pfx_len = ver == 4 and 32 or 128 for l in local_nets: if l.prefixlen < max_pfx_len or str(l.ip) != ip_start: if_dev.route._as_root("add", "local", str(l), "dev", if_dev.name, options=[ver])
def get_pool_from_ip (data, addr): pool = "" addr_o = netaddr.IPAddress (addr) for i , inf in enumerate (data['pools']): for j, rng in enumerate (data['pools'][i]['ranges']): ip_range = list (netaddr.iter_iprange(data['pools'][i]['ranges'][j]['low'], data['pools'][i]['ranges'][j]['high'])) if (addr_o in ip_range): pool = data['pools'][i]['id'] return (pool) return (pool)
class Przelewy24Id(object): PRZELEWY24_URL_QA = 'https://sandbox.przelewy24.pl' PRZELEWY24_URL_PROD = 'https://secure.przelewy24.pl' PRZELEWY24_ID = None # TODO: uzuepłnij PRZELEWY24_CRC = None # TODO: uzuepłnij PRZELEWY24_ADDRS = [ list(netaddr.iter_iprange('217.168.139.48', '217.168.139.55')), # 217.168.139.48/29 list(netaddr.iter_iprange('217.168.128.198', '217.168.128.202') ), # 217.168.128.198/31 217.168.128.200/31 217.168.128.202/32 list(netaddr.iter_iprange('91.216.191.181', '91.216.191.185') ) # 91.216.191.181/32 91.216.191.182/31 91.216.191.184/31 ] def __init__(self, merchant_id, crc, qa=True): self.PRZELEWY24_ID = merchant_id self.PRZELEWY24_CRC = crc self.PRZELEWY24_URL = self.PRZELEWY24_URL_QA if qa else self.PRZELEWY24_URL_PROD def CRC(self, *fields): # print '|'.join(map(str, fields+(self.PRZELEWY24_CRC,) )) return hashlib.md5(('|'.join(map( str, fields + (self.PRZELEWY24_CRC, ))).encode('utf8'))).hexdigest() @classmethod def VerifyRemoteAddr(cls, addr): addr = netaddr.IPAddress(addr) for i in cls.PRZELEWY24_ADDRS: if addr in i: return True return False def PaymentForm(self, *args, **kwargs): return PaymentForm(self, *args, **kwargs) def SuccessResponse(self, *args, **kwargs): return SuccessResponse(self, *args, **kwargs) def ErrorResponse(self, *args, **kwargs): return ErrorResponse(self, *args, **kwargs)
def setUp(self): self.apiclient = self.testClient.getApiClient() self.dbclient = self.testClient.getDbConnection() self.cleanup = [] # Deploy guest vm try: self.virtual_machine = VirtualMachine.create( self.apiclient, self.testdata["server_without_disk"], templateid=self.template.id, accountid=self.account.name, domainid=self.testdata["domainid"], zoneid=self.testdata["zoneid"], serviceofferingid=self.service_offering.id, mode=self.testdata["mode"], ) except Exception as e: raise Exception( "Warning: Exception during vm deployment: {}".format(e)) self.vm_response = VirtualMachine.list( self.apiclient, id=self.virtual_machine.id ) self.assertEqual( isinstance(self.vm_response, list), True, "Check VM list response returned a valid list" ) self.ip_range = list( netaddr.iter_iprange( unicode( self.testdata["vlan_ip_range"]["startip"]), unicode( self.testdata["vlan_ip_range"]["endip"]))) self.nic_ip = netaddr.IPAddress( unicode( self.vm_response[0].nic[0].ipaddress)) self.debug("vm got {} as ip address".format(self.nic_ip)) self.assertIn( self.nic_ip, self.ip_range, "VM did not get the ip address from the new ip range" ) ip_alias = self.dbclient.execute( "select ip4_address from nic_ip_alias;" ) self.alias_ip = str(ip_alias[0][0]) self.debug("alias ip : %s" % self.alias_ip) self.assertNotEqual( self.alias_ip, None, "Error in creating ip alias. Please check MS logs" ) self.cleanup.append(self.virtual_machine) return
class DotPay(object): class Statuses: New = "new" Processing = "processing" Completed = "completed" Rejected = "rejected" ProcessingRealizationWaiting = "processing_realization_waiting" ProcessingRealization = "processing_realization" names = { New: "nowa", Processing: "przetwarzana", Completed: "wykonana", Rejected: "odrzucona", ProcessingRealizationWaiting: "oczekuje na realizację", ProcessingRealization: "realizowana", } DOTPAY_URL = 'https://ssl.dotpay.pl/t2/' DOTPAY_URL_TEST = 'https://ssl.dotpay.pl/test_payment/' DOTPAY_ID = '' DOTPAY_CRC = '' DOTPAY_ADDRS = [ list(netaddr.iter_iprange('195.150.9.37', '195.150.9.37')) ] DOTPAY_ERRORS = { "PAYMENT_EXPIRED": "przekroczona data ważności wygenerowanego linku płatniczego lub przekazana w parametrze expiration_date", "UNKNOWN_CHANNEL": "nieznany kanał", "DISABLED_CHANNEL": "wyłączony kanał płatności", "BLOCKED_ACCOUNT": "zablokowane konto", "INACTIVE_SELLER": "brak możliwości dokonania płatności spowodowany brakiem aktywacja konta", "AMOUNT_TOO_LOW": "mniejsza kwota niż minimalna określona dla sklepu", "AMOUNT_TOO_HIGH": "większa kwota niż maksymalna określona dla sklepu", "BAD_DATA_FORMAT": "przesłano błędny format danych np. błędny format parametru expiration_date", "UNKNOWN_ERROR": "wartość zwracana w innym przypadku niż powyższe", } CRC_FIELDS = ("id,operation_number,operation_type,operation_status,operation_amount,operation_currency,"+ "operation_withdrawal_amount,operation_commission_amount,operation_original_amount,operation_original_currency,"+ "operation_datetime,operation_related_number,control,description,email,p_info,p_email,channel,channel_country,geoip_country").split(",") def CRC(self, pin, fields): return hashlib.sha256((pin + "".join(fields.get(i, '') for i in self.CRC_FIELDS)).encode('utf8')).hexdigest() @classmethod def VerifyRemoteAddr(cls, addr): addr = netaddr.IPAddress(addr) for i in cls.DOTPAY_ADDRS: if addr in i: return True return False
def _setup_routes(self, if_dev, ver, ip_start, ip_end, gw_ip): gw_ip_net = netaddr.IPNetwork(gw_ip) if_dev.addr.add("%s/%s" % (ip_start, gw_ip_net.prefixlen)) if_dev.route.add_gateway(str(gw_ip_net.ip)) if ip_start != ip_end: local_nets = netaddr.cidr_merge(netaddr.iter_iprange(ip_start, ip_end)) max_pfx_len = (ver == 4 and 32 or 128) for l in local_nets: if l.prefixlen < max_pfx_len or str(l.ip) != ip_start: if_dev.route._as_root('add', 'local', str(l), 'dev', if_dev.name, options=[ver])
def gen_network(st): st = st.strip() if st == '': return [] if '-' in st: dash = st.find('-') start = st[:dash] last_part = st[dash + 1:] point = len(st) - st[::-1].find('.') end = st[:point] + last_part return list(netaddr.iter_iprange(start, end)) return list(ipaddress.ip_network(st))
def _create_lports(self, lswitch, lport_create_args = [], lport_amount=1): LOG.info("create %d lports on lswitch %s" % \ (lport_amount, lswitch["name"])) self.RESOURCE_NAME_FORMAT = "lport_XXXXXX_XXXXXX" batch = lport_create_args.get("batch", lport_amount) LOG.info("Create lports method: %s" % self.install_method) install_method = self.install_method network_cidr = lswitch.get("cidr", None) ip_addrs = None if network_cidr: end_ip = network_cidr.ip + lport_amount if not end_ip in network_cidr: message = _("Network %s's size is not big enough for %d lports.") raise exceptions.InvalidConfigException( message % (network_cidr, lport_amount)) ip_addrs = netaddr.iter_iprange(network_cidr.ip, network_cidr.last) ovn_nbctl = self.controller_client("ovn-nbctl") ovn_nbctl.set_sandbox("controller-sandbox", install_method) ovn_nbctl.enable_batch_mode() base_mac = [i[:2] for i in self.task["uuid"].split('-')] base_mac[3:] = ['00']*3 flush_count = batch lports = [] for i in range(lport_amount): name = self.generate_random_name() lport = ovn_nbctl.lport_add(lswitch["name"], name) ip = str(ip_addrs.next()) if ip_addrs else "" mac = utils.get_random_mac(base_mac) ovn_nbctl.lport_set_addresses(name, [mac, ip]) ovn_nbctl.lport_set_port_security(name, mac) lports.append(lport) flush_count -= 1 if flush_count < 1: ovn_nbctl.flush() flush_count = batch ovn_nbctl.flush() # ensure all commands be run ovn_nbctl.enable_batch_mode(False) return lports
def is_ipaddr_in_range(ip, range): # only support range for last digits if not is_valid_ip_range(range): return False start_range, end_range = range.split("-") if '.' not in end_range: end_range = "{}.{}".format('.'.join(start_range.split('.')[:3]), end_range) try: if IPAddress(ip) in list(iter_iprange(start_range, end_range)): return True except (TypeError, ValueError, AddrFormatError): pass return False
def expand_it(iplist): results = [] for ip in iplist: if type(ip) is list: #take care of range [ results.append(address) for address in iter_iprange(sorted(ip)[0], sorted(ip)[1]) ] else: #expand cidr for address in ip: results.append(address) return results
def test_iprange_boundaries(): assert list(iter_iprange('192.0.2.0', '192.0.2.7')) == [ IPAddress('192.0.2.0'), IPAddress('192.0.2.1'), IPAddress('192.0.2.2'), IPAddress('192.0.2.3'), IPAddress('192.0.2.4'), IPAddress('192.0.2.5'), IPAddress('192.0.2.6'), IPAddress('192.0.2.7'), ] assert list(iter_iprange('::ffff:192.0.2.0', '::ffff:192.0.2.7')) == [ IPAddress('::ffff:192.0.2.0'), IPAddress('::ffff:192.0.2.1'), IPAddress('::ffff:192.0.2.2'), IPAddress('::ffff:192.0.2.3'), IPAddress('::ffff:192.0.2.4'), IPAddress('::ffff:192.0.2.5'), IPAddress('::ffff:192.0.2.6'), IPAddress('::ffff:192.0.2.7'), ]
def test_01_deploy_vm_in_new_cidr(self): """Deploy guest vm after adding guest IP range in new CIDR 1.Deploy guest vm 2.Verify vm gets the ip address from new cidr """ #Deploy guest vm try : self.virtual_machine = VirtualMachine.create( self.apiclient, self.services["server_without_disk"], templateid = self.template.id, accountid = self.account.name, domainid = self.services["domainid"], zoneid = self.services["zoneid"], serviceofferingid = self.service_offering.id, mode = self.services["mode"], ) except Exception as e : raise Exception("Warning: Exception during vm deployment: {}".format(e)) self.vm_response = VirtualMachine.list( self.apiclient, id = self.virtual_machine.id ) self.assertEqual( isinstance(self.vm_response, list), True, "Check VM list response returned a valid list" ) self.ip_range = list(netaddr.iter_iprange(unicode(self.services["vlan_ip_range"]["startip"]), unicode(self.services["vlan_ip_range"]["endip"]))) self.nic_ip = netaddr.IPAddress(unicode(self.vm_response[0].nic[0].ipaddress)) self.debug("vm got {} as ip address".format(self.nic_ip)) self.assertIn( self.nic_ip, self.ip_range, "VM did not get the ip address from the new ip range" ) self.virtual_machine.delete(self.apiclient) expunge_del = Configurations.list( self.apiclient, name = 'expunge.delay' ) expunge_int = Configurations.list( self.apiclient, name = 'expunge.interval' ) wait_time = int(expunge_del[0].value) + int(expunge_int[0].value) + int(30) self.debug("Waiting for {} seconds for the vm to expunge".format(wait_time)) #wait for the vm to expunge time.sleep(wait_time) return
def test_01_deploy_vm_in_new_cidr(self): """Deploy guest vm after adding guest IP range in new CIDR 1.Deploy guest vm 2.Verify vm gets the ip address from new cidr """ self.ip_range = list(netaddr.iter_iprange(unicode(self.services["vlan_ip_range"]["startip"]), unicode(self.services["vlan_ip_range"]["endip"]))) self.nic_ip = netaddr.IPAddress(unicode(self.vm_response[0].nic[0].ipaddress)) self.debug("vm got {} as ip address".format(self.nic_ip)) self.assertIn( self.nic_ip, self.ip_range, "VM did not get the ip address from the new ip range" ) return
def valid_hosts(formcls, field): """Validate a list of IPs (Ipv4) or hostnames using python stdlib. This is more robust than the WTForm version as it also considers hostnames. Comma separated values: e.g. '10.7.223.101,10.7.12.0' Space separated values: e.g. '10.223.101 10.7.223.102' Ranges: e.g. '10.7.223.200-10.7.224.10' Hostnames: e.g. foo.x.y.com, baz.bar.z.com :param formcls (object): The form class. :param field (str): The list of ips. """ ip_re = re.compile(r'[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}') data = field.data if ',' in data: ips = [ip for ip in data.split(',') if ip] elif ' ' in data: ips = [ip for ip in data.split(' ') if ip] elif '-' in data and re.match(ip_re, data): try: start, end = data.split('-') ips = iter_iprange(start, end) ips = [str(ip) for ip in list(ips)] except ValueError: raise ValueError( 'Invalid range specified. Format should be: ' 'XXX.XXX.XXX.XXX-XXX.XXX.XXX.XXX ' '(e.g. 10.7.223.200-10.7.224.10)') except AddrFormatError as e: raise ValueError(e) else: # Just use the single ip ips = [data] # If any fails conversion, it is invalid. for ip in ips: # Skip hostnames if not is_ip(ip): if not is_hostname(ip): raise ValueError('Invalid hostname: "{}"'.format(ip)) else: continue try: socket.inet_aton(ip) except socket.error: raise ValueError('Invalid IP: {}'.format(ip))
def delIfMatchedAddr(ipv4Addresses_, fIpv4Addresses_): """ Delete from list any addresses that are also found in the whitelist file. The netaddr module is used to allow cidr and ranges. """ s1 = netaddr.IPSet(ipv4Addresses_) l2 = [] for i in fIpv4Addresses_[:]: m = re.search(r'(.*) \.\.\. (.*)', i) if not m: l2.append(i) else: l2 += netaddr.IPSet(netaddr.iter_iprange(m.group(1), m.group(2))) s2 = netaddr.IPSet(l2) return map(str, list(s1 - s2))
def detect(self, url): cdn_list = {'ips_cdn_cloudflare':['199.27.128.0/21','173.245.48.0/20','103.21.244.0/22','103.22.200.0/22','103.31.4.0/22','141.101.64.0/18','108.162.192.0/18','190.93.240.0/20','188.114.96.0/20','197.234.240.0/22','198.41.128.0/17','162.158.0.0/15','104.16.0.0/12'], 'ips_cdn_360': ['183.136.133.0-183.136.133.255','220.181.55.0-220.181.55.255','101.226.4.0-101.226.4.255','180.153.235.0-180.153.235.255','122.143.15.0-122.143.15.255','27.221.20.0-27.221.20.255','202.102.85.0-202.102.85.255','61.160.224.0-61.160.224.255','112.25.60.0-112.25.60.255','182.140.227.0-182.140.227.255','221.204.14.0-221.204.14.255','222.73.144.0-222.73.144.255','61.240.144.0-61.240.144.255','113.17.174.0-113.17.174.255','125.88.189.0-125.88.189.255','125.88.190.0-125.88.190.255','120.52.18.1-120.52.18.255'], 'ips_cdn_jiasule':['119.188.35.0-119.188.35.255','61.155.222.0-61.155.222.255','218.65.212.0-218.65.212.255','116.211.121.0-116.211.121.255','103.15.194.0-103.15.194.255','61.240.149.0-61.240.149.255','222.240.184.0-222.240.184.255','112.25.16.0-112.25.16.255','59.52.28.0-59.52.28.255','211.162.64.0-211.162.64.255','180.96.20.0-180.96.20.255','103.1.65.0-103.1.65.255'], 'ips_cdn_anquanbao' :['220.181.135.1-220.181.135.255','115.231.110.1-115.231.110.255','124.202.164.1-124.202.164.255','58.30.212.1-58.30.212.255','117.25.156.1-117.25.156.255','36.250.5.1-36.250.5.255','183.60.136.1-183.60.136.255','183.61.185.1-183.61.185.255','14.17.69.1-14.17.69.255','120.197.85.1-120.197.85.255','183.232.29.1-183.232.29.255','61.182.141.1-61.182.141.255','182.118.12.1-182.118.12.255','182.118.38.1-182.118.38.255','61.158.240.1-61.158.240.255','42.51.25.1-42.51.25.255','119.97.151.1-119.97.151.255','58.49.105.1-58.49.105.255','61.147.92.1-61.147.92.255','69.28.58.1-69.28.58.255','176.34.28.1-176.34.28.255','54.178.75.1-54.178.75.255','112.253.3.1-112.253.3.255','119.167.147.1-119.167.147.255','123.129.220.1-123.129.220.255','223.99.255.1-223.99.255.255','117.34.72.1-117.34.72.255','117.34.91.1-117.34.91.255','123.150.187.1-123.150.187.255','221.238.22.1-221.238.22.255','125.39.32.1-125.39.32.255','125.39.191.1-125.39.191.255','125.39.18.1-125.39.18.255','14.136.130.1-14.136.130.255','210.209.122.1-210.209.122.255','111.161.66.1-111.161.66.255'], 'ips_cdn_incapsula':['199.83.128.0/21','198.143.32.0/19','149.126.72.0/21','103.28.248.0/22','45.64.64.0/22','185.11.124.0/22 ','192.230.64.0/18'], 'ips_cdn_yunjiasu':['222.216.190.0-222.216.190.255','61.155.149.0-61.155.149.255','119.188.14.0-119.188.14.255','61.182.137.0-61.182.137.255','117.34.28.0-117.34.28.255','119.188.132.0-119.188.132.255','42.236.7.0-42.236.7.255','183.60.235.0-183.60.235.255','117.27.149.0-117.27.149.255','216.15.172.0/24']} cc = [] try: ips = socket.gethostbyname_ex(url.split('/')[2]) except socket.gaierror: ips=[] for ip_addr in ips[2]: for cdn in cdn_list: for cidr in cdn_list[cdn]: if '-' in cidr: l = cidr.split('-') ip_range = netaddr.iter_iprange(l[0],l[1]) ip_range = netaddr.cidr_merge(ip_range) for i in ip_range: if ip_addr in i: cc.append(cdn) else: pass else: if ip_addr in cidr: cc.append(cdn) print '------------cidr--------------' print cc resp = self.s.get(url, allow_redirects=False, verify=False) if resp.history: headers = resp.history[0].headers cookies = resp.history[0].cookies else: headers = resp.headers cookies = resp.cookies result = [] for prop in dir(self): if prop.startswith('finger_'): func = getattr(self, prop) r = func(headers, cookies) if r: result.append(r) print '------------header------------' print result return result
def get_multicast_ip(network_profile): """ Retreive a multicast ip from the defined pool. :params network_profile: object of type network profile :returns: string representing multicast IP """ # Round robin multicast ip allocation min_ip, max_ip = _get_multicast_ip_range(network_profile) addr_list = list((netaddr.iter_iprange(min_ip, max_ip))) mul_ip_str = str(addr_list[network_profile.multicast_ip_index]) network_profile.multicast_ip_index += 1 if network_profile.multicast_ip_index == len(addr_list): network_profile.multicast_ip_index = 0 return mul_ip_str
def _init_subnet_addr_generator(self, subnet_id, subnet): def ip_generator(ip_list): for ip in ip_list: yield ip if not subnet: self._subnets[subnet_id] = ip_generator([]) allocation_pools = subnet.get('allocation_pools', None) for pool in allocation_pools: start = pool['start'] end = pool['end'] ip_list = list(str(ip) for ip in netaddr.iter_iprange(start, end)) self._subnets[subnet_id] = ip_generator( [ip for ip in ip_list])
def monitor_network(min_range, max_range): #get ip of this device net.ifaddresses('eth0') host_ip = net.ifaddresses('eth0')[2][0]['addr'] # use device address to determine subnet s_net,scrap = host_ip.rsplit(".", 1) ##### Iterate the process 4 times # TODO need to make this iterative for deamon while True: # use subnet of device and defined range to create address list for iteration addresses = list(iter_iprange(str(s_net) +"."+ str(min_range), str(s_net) +"."+ str(max_range))) #set counter for active devices liveCounter = 0 deadCounter = 0 i=0 active_addr = {} dead_addr = {} for host in addresses: resp = sr1(IP(dst=str(host))/ICMP(), timeout=2, verbose=0) if (str(host) == str(host_ip)): print str(host) + " is active(host)" active_addr[i]=str(host) liveCounter +=1 i+=1 elif(str(type(resp)) == "<type 'NoneType'>"): print str(host) + " is dead" deadCounter += 1 dead_addr[i]=str(host) i+=1 elif (int(resp.getlayer(ICMP).type)==3 and int(resp.getlayer(ICMP).code) in [1,2,3,9,10,13]): print str(host) + " is blocking ICMP" else: print str(host) + " is active" active_addr[i]=str(host) i+=1 liveCounter += 1 print active_addr rec = threading.Thread(target=recon.recon_address(active_addr)) rec.start() gen = threading.Thread(target=map_gen.gen_lense(liveCounter, deadCounter, active_addr, dead_addr)) gen.start()
def _validate_range_with_network(self): ip_networks = [] if self.range: start, end = self.range.split('-') ip_addresses = list(netapi.iter_iprange(start, end)) ip_networks.extend(netapi.cidr_merge(ip_addresses)) else: ip_addresses = get_ips_from_cidr(self.cidr) ip_networks.extend(netapi.cidr_merge(ip_addresses)) address_lst = [] net = netapi.IPNetwork(self.cidr) for network in ip_networks: for address in network: address_lst.append(str(address)) if address not in net: excep_msg = 'given dhcp range:%s not a subset of network:%s' % (self.range, net) LOGGER.error(excep_msg) raise Exception(excep_msg) self.iplist = address_lst
def NewSite(): iplist = [] global SITE SITE = Site(raw_input("Site Code: ").upper()) SITE.community_string = raw_input("SNMP Community String: ") SITE.username = raw_input("SSH/Telnet Username: "******"SSH/Telnet Pre-shared key: ") SITE.enable = raw_input("Enable Password: "******"TFTP Address: ") print "\nTarget Devices:\n" "1. Single IP address (x.x.x.x)\n" "2. IP file (Must be in working directory)\n" "3. Subnet (x.x.x.x/y)\n" "4. Network Range (x.x.x.x - y.y.y.y)\n" choice = int(raw_input("Choose an option: ")) if choice == 1: arg = raw_input("IP Address (x.x.x.x): ") ip = re.findall(r"[0-9]+(?:\.[0-9]+){3}", arg) ip = IPAddress(ip[0]) iplist.append(ip) if choice == 2: ip_file = raw_input("IP File name: ") with open(ip_file) as f: buff = f.read().splitlines() buff = " ".join(buff) ip = re.findall(r"[0-9]+(?:\.[0-9]+){3}", buff) for i in ip: iplist.append(IPAddress(i)) if choice == 3: network = raw_input("IP Network: ") subnet = IPNetwork(network) for ip in subnet: iplist.append(ip) if choice == 4: ip_range = raw_input("Network Range: ") ip = re.findall(r"[0-9]+(?:\.[0-9]+){3}", ip_range) iplist = list(iter_iprange(ip[0], ip[1])) SITE.iplist = iplist exec_menu("0")
def spool_update_site_config(scanner, alist): if len(alist) == 0: return foundlist = {} for x in alist: foundlist[x] = False debug.printd('updating site configuration for site %s' % spoolconf.siteid) siteconf = scanner.conn.site_config(spoolconf.siteid) root = ET.fromstring(siteconf) siteentry = root.find('Site') if siteentry == None: raise Exception('no Site subelement in response') hostlist = siteentry.find('Hosts') if hostlist == None: raise Exception('no Hosts subelement in response') for s in hostlist: if s.tag != 'range': continue low = s.get('from') high = s.get('to') plist = [] if high != None: ipl = list(netaddr.iter_iprange(low, high)) plist = [str(x) for x in ipl] else: plist = [low,] for a in plist: if a in foundlist: foundlist[a] = True for i in foundlist: if foundlist[i]: debug.printd('site already has %s' % i) continue debug.printd('adding %s' % i) newsub = ET.SubElement(hostlist, 'range') newsub.set('from', i) scanner.conn.site_save((ET.tostring(siteentry),))