Scripting Playground

Exploring practical Python scripts for network automation

Scripting Playground

Automation is at the core of modern network operations, and Python provides powerful capabilities for streamlining network management. Below are some of the practical scripts I work with to enhance network efficiency, monitor device health, and automate configuration tasks.

Cisco Network Health Checks

1. Cisco Switch Health Check

Perform a quick health assessment of a Cisco switch, including CPU utilization, memory usage, and interface errors.

from netmiko import ConnectHandler

switch = {
    "device_type": "cisco_ios",
    "host": "192.168.1.1",
    "username": "admin",
    "password": "password",
}

conn = ConnectHandler(**switch)
output = conn.send_command("show processes cpu | include CPU utilization")
print("CPU Utilization:", output)
conn.disconnect()

2. Cisco WLC Health Check

Retrieve the number of active APs, client associations, and system uptime from a Cisco Wireless LAN Controller.

from netmiko import ConnectHandler

wlc = {
    "device_type": "cisco_wlc",
    "host": "192.168.1.2",
    "username": "admin",
    "password": "password",
}

conn = ConnectHandler(**wlc)
output = conn.send_command("show ap summary")
print(output)
conn.disconnect()

3. Cisco ISE Health Check

Check the status of key services running on Cisco ISE.

from netmiko import ConnectHandler

ise = {
    "device_type": "cisco_ios",
    "host": "192.168.1.3",
    "username": "admin",
    "password": "password",
}

conn = ConnectHandler(**ise)
output = conn.send_command("show application status ise")
print(output)
conn.disconnect()

Network Data Collection

4. Collect MAC Address Table

Gather MAC address table details from a switch.

output = conn.send_command("show mac address-table")
print(output)

5. Interface Status & Descriptions

Retrieve interface descriptions and statuses to monitor connectivity.

output = conn.send_command("show interfaces description")
print(output)

6. Check Cisco AP Link Speed

Verify if a Cisco AP is connected at an optimal interface speed.

output = conn.send_command("show interface GigabitEthernet1/0/1 | include input rate")
print("AP Interface Speed:", output)

Network Security & Optimization

7. Remove Outdated Security Vulnerabilities

Identify weak security settings such as outdated ciphers or Telnet usage.

commands = [
    "show running-config | include enable secret",
    "show running-config | include telnet"
]

for cmd in commands:
    print(conn.send_command(cmd))

8. Generate Jinja2 Configurations

Use Jinja2 templating to create consistent switch configurations.

from jinja2 import Template

template = Template("""
hostname {{ hostname }}
interface GigabitEthernet1/0/1
 description Uplink
 switchport mode trunk
""")

config = template.render(hostname="SW1")
print(config)

Bulk Deployment & Configuration

9. Deploy Minimal Configurations on Multiple Switches

Push SNMP configurations to hundreds of devices.

commands = [
    "snmp-server community public RO",
    "snmp-server community private RW"
]

for cmd in commands:
    print(conn.send_config_set(cmd))

10. Deploy Large-Scale Network Configurations with Ansible

Automate major configuration changes across the network.

- name: Configure EIGRP on Cisco Switches
  hosts: switches
  gather_facts: no
  tasks:
    - name: Configure EIGRP
      ios_config:
        lines:
          - router eigrp 100
          - network 10.1.1.0 0.0.0.255
        save_when: changed

Network Mapping & Analysis

11. Map Network Using CDP Neighbors

Identify neighboring devices and map network topology.

output = conn.send_command("show cdp neighbors detail")
print(output)

12. Collect Fiber Cable Alerts & Errors

Check fiber link quality and identify alarm conditions.

output = conn.send_command("show interfaces transceiver detail")
print(output)

VLAN & Logging Automation

13. Fetch VLAN Database Information

Retrieve and analyze VLAN configurations from a switch.

output = conn.send_command("show vlan brief")
print(output)

14. Deploy Log Filters in Bulk

Automate syslog filter configuration across multiple devices.

commands = [
    "logging buffered 100000",
    "logging trap informational"
]

for cmd in commands:
    print(conn.send_config_set(cmd))

Conclusion

The scripts above showcase how Python can be leveraged for network automation, data collection, security enforcement, and large-scale deployments. By continuously refining these scripts, I aim to enhance efficiency and reliability in network operations.

If you're interested in collaborating or learning more, feel free to reach out!

All rights reserved.