You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Netmiko and NAPALM are two of the most widely used Python libraries for network automation. Netmiko simplifies SSH connections to network devices, while NAPALM provides a unified, vendor-agnostic API for configuration management and operational data retrieval.
Netmiko is a multi-vendor SSH library built on top of Paramiko. It handles the painful details of interacting with network device CLIs:
| Feature | Description |
|---|---|
| Auto-detection of device type | Identifies prompts and adjusts behaviour |
| Handles pagination | Automatically sends terminal length 0 or equivalent |
| Enter enable mode | enable() method with password support |
| Enter config mode | config_mode() enters and exit_config_mode() leaves |
| Structured output | Integrates with TextFSM for parsed results |
| Multi-vendor | Supports 60+ device types (Cisco IOS/XE/XR/NXOS, Arista, Juniper, etc.) |
pip install netmiko
| Device Type String | Platform |
|---|---|
cisco_ios | Cisco IOS / IOS XE |
cisco_nxos | Cisco NX-OS |
cisco_xr | Cisco IOS XR |
arista_eos | Arista EOS |
juniper_junos | Juniper Junos |
paloalto_panos | Palo Alto PAN-OS |
linux | Linux servers |
from netmiko import ConnectHandler
device = {
"device_type": "cisco_ios",
"host": "192.168.1.1",
"username": "admin",
"password": "secret",
"secret": "enable_password", # Enable password
}
with ConnectHandler(**device) as conn:
# Enter enable mode
conn.enable()
# Send a show command
output = conn.send_command("show ip interface brief")
print(output)
from netmiko import ConnectHandler
device = {
"device_type": "cisco_ios",
"host": "192.168.1.1",
"username": "admin",
"password": "secret",
}
config_commands = [
"interface GigabitEthernet0/1",
"description Uplink to Core",
"ip address 10.0.0.1 255.255.255.252",
"no shutdown",
]
with ConnectHandler(**device) as conn:
output = conn.send_config_set(config_commands)
print(output)
# Save the configuration
conn.save_config()
from netmiko import ConnectHandler
device = {
"device_type": "cisco_ios",
"host": "192.168.1.1",
"username": "admin",
"password": "secret",
}
with ConnectHandler(**device) as conn:
# Raw text output
raw = conn.send_command("show ip interface brief")
print(type(raw)) # <class 'str'>
# Structured output using TextFSM
structured = conn.send_command("show ip interface brief", use_textfsm=True)
print(type(structured)) # <class 'list'>
for intf in structured:
print(f"{intf['intf']}: {intf['ipaddr']} ({intf['status']})")
from netmiko import ConnectHandler
from concurrent.futures import ThreadPoolExecutor
devices = [
{"device_type": "cisco_ios", "host": "192.168.1.1", "username": "admin", "password": "secret"},
{"device_type": "cisco_ios", "host": "192.168.1.2", "username": "admin", "password": "secret"},
{"device_type": "arista_eos", "host": "192.168.1.3", "username": "admin", "password": "secret"},
]
def get_version(device: dict) -> str:
with ConnectHandler(**device) as conn:
return conn.send_command("show version")
# Run in parallel using threads
with ThreadPoolExecutor(max_workers=5) as executor:
results = executor.map(get_version, devices)
for device, result in zip(devices, results):
print(f"--- {device['host']} ---")
print(result[:200]) # First 200 characters
NAPALM (Network Automation and Programmability Abstraction Layer with Multivendor support) provides a unified Python API across different network vendors. Instead of writing vendor-specific commands, you use the same methods regardless of the device type:
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.