Showing posts with label Network Scan. Show all posts
Showing posts with label Network Scan. Show all posts

Friday, February 13, 2026

Python Script to Audit /etc/hosts and Generate PDF Reports in Kali Linux

Automating Network Intelligence: Python PDF Reporting | Kali Linux

Automating Network Intelligence: Python Script to Generate PDF Reports in Kali Linux

Ethical Hacking Disclaimer: This tutorial and script are for educational purposes and authorized security auditing only[cite: 184]. Never use these tools on networks or systems without explicit permission from the owner[cite: 185].

In the world of penetration testing and system administration, documentation is just as important as execution[cite: 186]. In Kali Linux, we often interact with the /etc/hosts file to manage local DNS or map target IPs[cite: 187]. Today, we’ll build a Python automation tool that audits your host configurations and generates a professional PDF report using network resolution[cite: 188].

Why Use Python for Kali Linux Reporting?

While Bash scripts are great for quick tasks, Python offers robust libraries like ReportLab for document generation and Socket for deep network interaction[cite: 189]. This script allows you to bridge the gap between raw system data and a shareable, executive-style report[cite: 190].

The Python Script: Network Host Auditor

import socket
import os
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas

def get_host_info(ip):
    """Attempt to resolve an IP address to a hostname."""
    try:
        return socket.gethostbyaddr(ip)[0]
    except (socket.herror, socket.gaierror):
        return "Unresolved"

def network_scan_to_pdf(output_path='network_scan_report.pdf'):
    report_data = []
    hosts_path = '/etc/hosts'

    if not os.path.exists(hosts_path):
        print(f"Error: {hosts_path} not found.")
        return

    try:
        with open(hosts_path, 'r') as f:
            for line in f:
                line = line.strip()
                if not line or line.startswith('#'):
                    continue
                parts = line.split()
                if len(parts) >= 2:
                    ip = parts[0]
                    dec_name = parts[1]
                    res_name = get_host_info(ip)
                    report_data.append((ip, dec_name, res_name))
    except Exception as e:
        print(f"Error: {e}")
        return

    c = canvas.Canvas(output_path, pagesize=letter)
    # PDF generation logic continues...
    c.save()

Execution Guide

Follow these steps to deploy the script in your terminal:

Step 1: Install Dependencies

  1. Run the following command to update and install ReportLab: sudo apt update && pip3 install reportlab [cite: 194]

Step 2: Run the Audit

Since the script reads /etc/hosts, use sudo:

sudo python3 host_audit.py
#PythonProgramming #PythonScripts #Automation #DevSecOps #KaliLinux

Generated for Kali Linux Power Users - 2026
© 2026 Kali AI Scripts | Professional CyberSecurity Documentation [cite: 127]