Scan a single host
()
| 8896 | logger.error(f"Error updating vulnerability output for {ip}: {e}") |
| 8897 | |
| 8898 | |
| 8899 | def update_netkb_entry(ip, hostname, mac, is_alive): |
| 8900 | try: |
| 8901 | # Update vulnerability output files |
| 8902 | update_vulnerability_output(ip, hostname, mac, is_alive) |
| 8903 | |
| 8904 | netkb_path = shared_data.netkbfile |
| 8905 | os.makedirs(os.path.dirname(netkb_path), exist_ok=True) |
| 8906 | |
| 8907 | rows = [] |
| 8908 | headers: list[str] = [] |
| 8909 | updated_row = None |
| 8910 | |
| 8911 | if os.path.exists(netkb_path) and os.path.getsize(netkb_path) > 0: |
| 8912 | with open(netkb_path, 'r', encoding='utf-8', errors='ignore') as f: |
| 8913 | reader = csv.DictReader(f) |
| 8914 | headers = list(reader.fieldnames) if reader.fieldnames else [] |
| 8915 | for row in reader: |
| 8916 | rows.append(row) |
| 8917 | else: |
| 8918 | headers = ['MAC Address', 'IPs', 'Hostnames', 'Alive', 'Ports'] |
| 8919 | |
| 8920 | for column in ['MAC Address', 'IPs', 'Hostnames', 'Alive', 'Ports']: |
| 8921 | if column not in headers: |
| 8922 | headers.append(column) |
| 8923 | |
| 8924 | alive_value = '1' if is_alive else '0' |
| 8925 | mac_to_store = _normalize_mac(mac) if mac else '' |
| 8926 | |
| 8927 | for row in rows: |
| 8928 | existing_ips = [entry.strip() for entry in row.get('IPs', '').split(';') if entry.strip()] |
| 8929 | if ip in existing_ips: |
| 8930 | if mac_to_store: |
| 8931 | row['MAC Address'] = mac_to_store |
| 8932 | # Don't generate pseudo MAC - let ARP cache provide real MAC addresses |
| 8933 | |
| 8934 | if hostname: |
| 8935 | existing_hostnames = [h.strip() for h in row.get('Hostnames', '').split(';') if h.strip()] |
| 8936 | existing_hostnames.append(hostname) |
| 8937 | row['Hostnames'] = ';'.join(sorted(set(existing_hostnames))) |
| 8938 | |
| 8939 | row['Alive'] = alive_value |
| 8940 | # CRITICAL: NEVER clear the Ports field here! |
| 8941 | # Ports are managed by scanning.py - we only update Alive/MAC/Hostname |
| 8942 | # Preserve existing ports to prevent data loss |
| 8943 | updated_row = row |
| 8944 | break |
| 8945 | |
| 8946 | if updated_row is None: |
| 8947 | new_row = {header: '' for header in headers} |
| 8948 | new_row['IPs'] = ip |
| 8949 | new_row['Hostnames'] = hostname or '' |
| 8950 | if mac_to_store: |
| 8951 | new_row['MAC Address'] = mac_to_store |
| 8952 | else: |
| 8953 | # Don't generate pseudo MAC - let ARP cache provide real MAC addresses |
| 8954 | new_row['MAC Address'] = '' |
| 8955 | new_row['Alive'] = alive_value |