def write_full_ip4_addresses(writer, ip4_network, ip4_addresses): iter_address = iter(ip4_addresses) ip4_address = None try: ip4_address = next(iter_address) except StopIteration: pass cidr = ip4_network.get_property('CIDR') network = ipaddress.IPv4Network(cidr) start_address = util.ip42int(str(network.network_address)) end_address = util.ip42int(str(network.broadcast_address)) mask = int(cidr.split('/')[1]) if mask <= 30: write_blunk_address(writer, start_address, NETWORKID_DISPLAY_NAME) start_address += 1 end_address -= 1 for address in range(start_address, end_address + 1): if ip4_address is not None and address == util.ip42int( ip4_address.get_address()): write_ip4_address(writer, ip4_address) try: ip4_address = next(iter_address) except StopIteration: ip4_address = None pass else: write_blunk_address(writer, address, '') if mask <= 30: write_blunk_address(writer, end_address + 1, BROADCAST_DISPLAY_NAME)
def get_order(entity): order = 0 range = entity.get_property('CIDR') if range is None: order = util.ip42int(entity.get_property('start')) else: val = range.split('/') order = util.ip42int(val[0]) return order
def is_in_dhcp_range(dhcp_ranges, address): result = False for dhcp_range in dhcp_ranges: start_ip = util.ip42int(dhcp_range.get_property('start')) end_ip = util.ip42int(dhcp_range.get_property('end')) ip4_address = util.int2ip4(address) if start_ip <= address and address <= end_ip: result = True break return result
def get_order(entity): order = 0 etype = entity.get_type() if etype in [Entity.IP4Block, Entity.IP4Network]: range = entity.get_property('CIDR') if range is not None: val = range.split('/') order = util.ip42int(val[0]) else: order = util.ip42int(entity.get_property('start')) elif etype == Entity.DHCP4Range: order = util.ip42int(entity.get_property('start')) elif etype == Entity.IP4Address: order = util.ip42int(entity.get_address()) return order
def _collect_clients(self, configuration, meraki_api, network_id): clients = [] now = datetime.now() meraki_clients = meraki_api.get_clients(network_id) for mc in meraki_clients: client = {} client['id'] = mc['id'] client['network_id'] = network_id client['order'] = util.ip42int(mc['ip']) client['name'] = mc['description'] if mc['description'] is not None else '-' client['system'] = mc['manufacturer'] if mc['manufacturer'] is not None else '' if mc['os'] is not None and 0 < len(mc['os']): if 0 < len(client['system']): client['system'] += ' - ' client['system'] += mc['os'] client['ipaddr'] = mc['ip'] client['macaddr'] = self._convert_mac(mc['mac']) client['detail_link'] = self._construct_meraki_url(meraki_api, client) client['linked_name'] = self._construct_linked_name(meraki_api, client) client['last_found'] = mc['lastSeen'] lastfound = parser.parse(mc['lastSeen']) lastfound = lastfound.replace(tzinfo=None) if (now - lastfound) > timedelta(days=30): client['state'] = 'RECLAIM' else: client['state'] = 'UNKNOWN' clients.append(client) clients.sort(key = lambda client: client['order']) return clients
def _collect_ip4_networks(self, configuration, ip4_networks, ipaddr): pack_address = util.ip42int(ipaddr) for ip4_network in ip4_networks: cidr = ip4_network.get_property('CIDR') network = ipaddress.IPv4Network(cidr) start_address = util.ip42int(str(network.network_address)) end_address = util.ip42int(str(network.broadcast_address)) if start_address <= pack_address <= end_address: return try: found = configuration.get_ip_range_by_ip(Entity.IP4Network, ipaddr) if found is not None: ip4_networks.append(found) except PortalException as e: print(safe_str(e))
def write_full_ip4_addresses_for_excel(sheet, start_column, start_row, ip4_network, ip4_addresses): index = start_row + 1 iter_address = iter(ip4_addresses) ip4_address = None try: ip4_address = next(iter_address) except StopIteration: pass cidr = ip4_network.get_property('CIDR') network = ipaddress.IPv4Network(cidr) start_address = util.ip42int(str(network.network_address)) end_address = util.ip42int(str(network.broadcast_address)) dhcp_ranges = list(ip4_network.get_children_of_type(Entity.DHCP4Range)) mask = int(cidr.split('/')[1]) if mask <= 30: write_blunk_address_for_excel(sheet, start_column, index, start_address, NETWORKID_DISPLAY_NAME) start_address += 1 end_address -= 1 index += 1 for address in range(start_address, end_address + 1): if ip4_address is not None and address == util.ip42int( ip4_address.get_address()): write_ip4_address_for_excel(sheet, start_column, index, ip4_address) try: ip4_address = next(iter_address) except StopIteration: pass elif is_in_dhcp_range(dhcp_ranges, address): write_dhcp_blunk_address_for_excel(sheet, start_column, index, address) else: write_blunk_address_for_excel(sheet, start_column, index, address, '') index += 1 if mask <= 30: write_blunk_address_for_excel(sheet, start_column, index, end_address + 1, BROADCAST_DISPLAY_NAME)
def write_ip4_network(writer, ip4_network, full): children = ip4_network.get_children_of_type(Entity.IP4Address) ip4_addresses = list(children) ip4_addresses.sort(key=lambda e: util.ip42int(e.get_address())) write_header(writer) if full == True: write_full_ip4_addresses(writer, ip4_network, ip4_addresses) else: write_ip4_addresses(writer, ip4_network, ip4_addresses)
def _compare_clients(self, configuration, clients): ip4_networks = [] include_matches = self.get_value('include_matches') include_ipam_only = self.get_value('include_ipam_only') for client in clients: self._collect_ip4_networks(configuration, ip4_networks, client['ipaddr']) for ip4_network in ip4_networks: ip4_addresses = ip4_network.get_children_of_type(Entity.IP4Address) for ip4_address in ip4_addresses: founds = [ client for client in clients if client['ipaddr'] == ip4_address.get_address() ] if 0 == len(founds): if include_ipam_only == True and ip4_address.get_state( ) != 'DHCP_FREE': new_client = {} new_client['id'] = 'BlueCat_' + str( ip4_address.get_id()) new_client['network_id'] = '' new_client['order'] = util.ip42int( ip4_address.get_address()) new_client['name'] = ip4_address.get_name() new_client['system'] = '' new_client['ipaddr'] = ip4_address.get_address() macaddr = ip4_address.get_property('macAddress') new_client[ 'macaddr'] = macaddr if macaddr is not None else '' new_client['detail_link'] = '' new_client['linked_name'] = ip4_address.get_name() new_client['last_found'] = '-' new_client['state'] = 'RECLAIM' clients.append(new_client) else: macaddress = ip4_address.get_property('macAddress') found = founds[0] matched_macs = [ mac for mac in found['macaddr'].split(',') if macaddress == mac.strip() ] print("found['mcaddr'] = ", found['macaddr'], ', maccaddress = ', macaddress) if 0 < len(matched_macs): found['state'] = 'MATCH' found['macaddr'] = matched_macs[0].strip() else: found['state'] = 'MISMATCH' if include_matches == False and found['state'] == 'MATCH': clients.remove(found)
def write_ip4_network_for_excel(workbook, start_column, start_row, ip4_network, full, linked_cells): children = ip4_network.get_children_of_type(Entity.IP4Address) ip4_addresses = list(children) ip4_addresses.sort(key = lambda e: util.ip42int(e.get_address())) sheet = workbook.create_sheet(title=get_sheet_name(ip4_network)) write_title_for_excel(sheet, ip4_network, linked_cells) write_header_for_excel(sheet, start_column, start_row) add_auto_filters(sheet, start_column, start_row, len(col_config['address'])) if full == True: write_full_ip4_addresses_for_excel(sheet, start_column, start_row, ip4_network, ip4_addresses) else: write_ip4_addresses_for_excel(sheet, start_column, start_row, ip4_network, ip4_addresses)
def _collect_clients(self, configuration, mist_api, site_id): clients = [] now = datetime.now() mist_clients = mist_api.get_clients(site_id) for mc in mist_clients: client = {} if self._debug: print('mc:', mc) client['id'] = mc['_id'] client['site_id'] = mc['site_id'] client['order'] = util.ip42int(mc['ip']) client['name'] = mc['hostname'] if 'hostname' in mc.keys() else '' client['system'] = mc[ 'manufacture'] if mc['manufacture'] != 'Unknown' else '' if mc['family'] is not None and 0 < len(mc['family']): if 0 < len(client['system']): client['system'] += ' - ' client['system'] += mc['family'] if mc['os'] is not None and 0 < len(mc['os']): if 0 < len(client['system']): client[ 'system'] += ' ' if '-' in client['system'] else ' - ' client['system'] += mc['os'] client['ipaddr'] = mc['ip'] client['macaddr'] = self._convert_mac(mc['mac']) client['detail_link'] = self._construct_mist_url(mist_api, client) client['linked_name'] = self._construct_linked_name( mist_api, client) lastfound = datetime.fromtimestamp(mc['last_seen']) lastfound = lastfound.replace(tzinfo=None) client['last_found'] = lastfound.strftime('%Y-%m-%dT%H:%M:%S') if (now - lastfound) > timedelta(days=30): client['state'] = 'RECLAIM' else: client['state'] = 'UNKNOWN' clients.append(client) clients.sort(key=lambda client: client['order']) return clients
def _compare_clients(self, configuration, clients): ip4_networks = [] include_matches = self.get_value('include_matches') include_ipam_only = self.get_value('include_ipam_only') for client in clients: self._collect_ip4_networks(configuration, ip4_networks, client['ipaddr']) for ip4_network in ip4_networks: ip4_addresses = ip4_network.get_children_of_type(Entity.IP4Address) for ip4_address in ip4_addresses: founds = [ client for client in clients if client['ipaddr'] == ip4_address.get_address() ] if 0 == len(founds): if include_ipam_only == True and ip4_address.get_state( ) != 'DHCP_FREE': new_client = {} new_client['id'] = '' new_client['site_id'] = '' new_client['order'] = util.ip42int( ip4_address.get_address()) new_client['name'] = ip4_address.get_name() new_client['system'] = '' new_client['ipaddr'] = ip4_address.get_address() macaddr = ip4_address.get_property('macAddress') new_client[ 'macaddr'] = macaddr if macaddr is not None else '' new_client['detail_link'] = '' new_client['linked_name'] = ip4_address.get_name() new_client['last_found'] = '-' new_client['state'] = 'RECLAIM' clients.append(new_client) else: macaddress = ip4_address.get_property('macAddress') found = founds[0] found['state'] = 'MATCH' if found[ 'macaddr'] == macaddress else 'MISMATCH' if include_matches == False and found['state'] == 'MATCH': clients.remove(found) clients.sort(key=lambda client: client['order'])
def _collect_nodes(self, configuration, sonar_api, network_id): nodes = [] now = datetime.now() sonar_nodes = sonar_api.get_nodes(network_id) for sn in sonar_nodes: for ad in sn['addresses']: node = {} node['id'] = sn['managedNodeId'] node['network_id'] = network_id node['order'] = util.ip42int(ad['addr']) node['name'] = sn['displayName'] node['system'] = sn['system']['family'] if node['system'] == '': try: node['system'] = ad['extraFields']['macaddr'][ 'organizationName'] except: pass node['ipaddr'] = ad['addr'] node['macaddr'] = ad['macaddr'].upper().replace(':', '-') node['detail_link'] = self._construct_sonar_url( sonar_api, node) node['linked_name'] = self._construct_linked_name( sonar_api, node) node['last_found'] = sn['lastScanSucceededAt'] lastfound = parser.parse(sn['lastScanSucceededAt']) lastfound = lastfound.replace(tzinfo=None) if (now - lastfound) > timedelta(days=30): node['state'] = 'RECLAIM' else: node['state'] = 'UNKNOWN' nodes.append(node) nodes.sort(key=lambda node: node['order']) return nodes
def _collect_clients(self, configuration, tanium_api, target_networks, include_discovery): retry_count = self.get_value('retry_count') interval = self.get_value('interval') clients = [] subnets = self._to_subnets_list(target_networks) result = tanium_api.get_managed_assets(subnets, retry_count, interval) tanium_clients = result['clients'] if include_discovery: result = tanium_api.get_unmanaged_assets(subnets, retry_count, interval) tanium_clients.extend(result['clients']) for tc in tanium_clients: client = {} client['managed'] = 'MANAGED' if tc['managed'] else 'UNKNOWN' client['id'] = tc['id'] client['order'] = util.ip42int(tc['ip_address']) client['name'] = tc['computer_name'] client['system'] = tc['manufacturer'] if tc['os'] is not None and 0 < len(tc['os']): if 0 < len(client['system']): client['system'] += ' - ' client['system'] += tc['os'] client['ipaddr'] = tc['ip_address'] client['macaddr'] = self._convert_mac(tc['mac_address']) client['detail_link'] = '' client['linked_name'] = tc['computer_name'] client['last_found'] = tc['last_found'] client['state'] = 'UNKNOWN' clients.append(client) print(client) return clients