7. Network Device Data Collection: Your Automated Log Collection Solution

Sooryah PrasathSooryah Prasath
4 min read

๐ŸŒ Network Automation Made Easy

A simple Python script to:

  • Read device information from JSON

  • Connect to network devices

  • Run commands

  • Save logs automatically

๐Ÿ“ฆ Required Libraries

import json
from netmiko import ConnectHandler
from datetime import datetime
import os
import logging

๐Ÿ› ๏ธ Full Script Implementation

import json
from netmiko import ConnectHandler
from datetime import datetime
import os
import logging

# Configure logging
def setup_logging():
    """
    Set up logging configuration
    Creates a logs directory and configures log file
    """
    # Create logs directory if it doesn't exist
    os.makedirs('logs', exist_ok=True)

    # Configure logging format and file
    timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
    log_filename = f'logs/network_collection_{timestamp}.log'

    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(levelname)s: %(message)s',
        handlers=[
            logging.FileHandler(log_filename),
            logging.StreamHandler()  # Also print to console
        ]
    )

    return log_filename

def connect_to_device(device):
    """
    Connect to a single network device and run commands

    Args:
    - device (dict): Device connection information

    Returns:
    - Tuple of (success_flag, output_text)
    """
    try:
        # Prepare device connection parameters
        device_params = {
            'device_type': device.get('device_type', 'cisco_ios'),
            'host': device['ip'],
            'username': device['username'],
            'password': device['password'],
            'timeout': 60,  # Connection timeout in seconds
            'global_delay_factor': 2  # Adjust for slower devices
        }

        # Establish connection
        logging.info(f"Connecting to device: {device['hostname']} ({device['ip']})")
        connection = ConnectHandler(**device_params)

        # Disable paging to ensure full command output
        connection.send_command("terminal length 0")

        # Commands to run (can be customized)
        commands = device.get('commands', [
            'show version',
            'show running-config',
            'show interfaces'
        ])

        # Collect output for each command
        device_output = f"Device: {device['hostname']} ({device['ip']})\n"
        device_output += "=" * 50 + "\n\n"

        for command in commands:
            logging.info(f"Running command: {command}")
            try:
                output = connection.send_command(command)
                device_output += f"Command: {command}\n"
                device_output += "-" * 30 + "\n"
                device_output += output + "\n\n"
            except Exception as cmd_error:
                logging.error(f"Error running command {command}: {cmd_error}")
                device_output += f"Error running command {command}: {cmd_error}\n\n"

        # Close connection
        connection.disconnect()
        logging.info(f"Disconnected from {device['hostname']}")

        return True, device_output

    except Exception as e:
        error_message = f"Connection error for {device['hostname']}: {str(e)}"
        logging.error(error_message)
        return False, error_message

def process_devices(devices_file='devices.json'):
    """
    Process all devices in the JSON file

    Args:
    - devices_file (str): Path to JSON file with device information
    """
    # Set up logging
    log_filename = setup_logging()
    logging.info(f"Starting network device data collection")

    # Create output directory
    os.makedirs('device_outputs', exist_ok=True)

    try:
        # Read device information from JSON
        with open(devices_file, 'r') as f:
            devices = json.load(f)

        # Track processing statistics
        total_devices = len(devices)
        successful_devices = 0
        failed_devices = 0

        # Process each device
        for device in devices:
            # Connect and collect data
            success, output = connect_to_device(device)

            # Save device output
            if success:
                successful_devices += 1
                output_filename = f"device_outputs/{device['hostname']}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt"
                with open(output_filename, 'w') as f:
                    f.write(output)
                logging.info(f"Output saved for {device['hostname']}: {output_filename}")
            else:
                failed_devices += 1

        # Log summary
        logging.info(f"Device Processing Summary:")
        logging.info(f"Total Devices: {total_devices}")
        logging.info(f"Successful Connections: {successful_devices}")
        logging.info(f"Failed Connections: {failed_devices}")
        logging.info(f"Full log available at: {log_filename}")

    except FileNotFoundError:
        logging.error(f"Devices file not found: {devices_file}")
    except json.JSONDecodeError:
        logging.error(f"Invalid JSON in {devices_file}")
    except Exception as e:
        logging.error(f"Unexpected error: {str(e)}")

# Main execution
if __name__ == "__main__":
    process_devices()

๐Ÿ“‹ Sample Devices JSON (devices.json)

[
    {
        "hostname": "Core-Switch-01",
        "ip": "192.168.1.1",
        "device_type": "cisco_ios",
        "username": "admin",
        "password": "your_password",
        "commands": [
            "show version",
            "show running-config",
            "show interfaces"
        ]
    },
    {
        "hostname": "Router-01",
        "ip": "192.168.1.2",
        "device_type": "cisco_ios",
        "username": "admin",
        "password": "another_password",
        "commands": [
            "show version",
            "show ip route"
        ]
    }
]

๐Ÿš€ Quick Start Guide

1. Prerequisites

# Install required library
pip install netmiko

2. Prepare Device File

  • Edit devices.json

  • Add your network devices

  • Include connection details

3. Run the Script

python network_device_script.py

๐Ÿ” What Happens When You Run the Script

  1. Creates logs/ directory

  2. Generates timestamped log file

  3. Connects to each device

  4. Runs specified commands

  5. Saves output to device_outputs/

  6. Logs all activities

๐Ÿ’ก Key Features

  • Automatic logging

  • Flexible device configuration

  • Error handling

  • Customizable commands

  • Simple, readable output

๐Ÿ”’ Security Notes

  • Never share your devices.json

  • Use secure, temporary credentials

  • Limit script access

  • Regularly update device information

Call to Action

๐Ÿ‘‰ Ready to automate your network data collection?

  • Download the script

  • Customize your device list

  • Run and explore!

What network insights are you looking to gain? Share below!

#NetworkAutomation #NetworkMonitoring #ITManagement

0
Subscribe to my newsletter

Read articles from Sooryah Prasath directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sooryah Prasath
Sooryah Prasath