You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Python is the dominant programming language for network automation. Its clear syntax, rich standard library, and thriving ecosystem of networking libraries make it the natural choice for engineers who need to automate repetitive tasks.
| Reason | Detail |
|---|---|
| Readable syntax | Looks like pseudocode, easy to learn for non-programmers |
| Rich ecosystem | Netmiko, NAPALM, Paramiko, pyATS, Nornir, and more |
| Cross-platform | Runs on Linux, macOS, and Windows |
| Community | Massive community with tutorials, forums, and open-source projects |
| Vendor support | Cisco, Arista, Juniper, and others provide Python SDKs and libraries |
| Rapid prototyping | Write a working script in minutes, not hours |
# Strings — device hostnames, IP addresses, commands
hostname = "switch01"
ip_address = "192.168.1.1"
# Integers — port numbers, VLAN IDs
vlan_id = 100
ssh_port = 22
# Booleans — feature flags
is_enabled = True
# Lists — collections of items (ordered, mutable)
interfaces = ["GigabitEthernet0/1", "GigabitEthernet0/2", "GigabitEthernet0/3"]
# Dictionaries — key-value pairs (like a device inventory)
device = {
"hostname": "switch01",
"ip": "192.168.1.1",
"platform": "cisco_ios",
"username": "admin",
"password": "secret123",
}
# Loop through a list of devices
devices = ["192.168.1.1", "192.168.1.2", "192.168.1.3"]
for ip in devices:
print(f"Connecting to {ip}...")
# Loop through a dictionary
device = {"hostname": "R1", "ip": "10.0.0.1", "os": "ios"}
for key, value in device.items():
print(f"{key}: {value}")
# Check device type and choose the right driver
platform = "cisco_ios"
if platform == "cisco_ios":
print("Using Netmiko cisco_ios driver")
elif platform == "arista_eos":
print("Using Netmiko arista_eos driver")
else:
print(f"Unsupported platform: {platform}")
def get_device_config(ip: str, username: str, password: str) -> str:
"""Connect to a device and retrieve its running configuration."""
# Connection logic would go here
print(f"Retrieving config from {ip}")
return "! Running configuration..."
# Call the function
config = get_device_config("192.168.1.1", "admin", "secret")
Network automation frequently involves reading from and writing to files:
# Read a list of device IPs from a file
with open("devices.txt", "r") as f:
devices = [line.strip() for line in f if line.strip()]
print(f"Loaded {len(devices)} devices")
# Write a device configuration to a file
config = "hostname switch01\ninterface Gi0/1\n no shutdown"
with open("backups/switch01.cfg", "w") as f:
f.write(config)
print("Configuration saved")
import json
# Parse JSON from a string
json_string = '{"hostname": "R1", "interfaces": ["Gi0/0", "Gi0/1"]}'
data = json.loads(json_string)
print(data["hostname"]) # R1
# Read JSON from a file
with open("inventory.json", "r") as f:
inventory = json.load(f)
# Write JSON to a file
with open("output.json", "w") as f:
json.dump(data, f, indent=2)
import yaml
# Read YAML from a file
with open("inventory.yaml", "r") as f:
inventory = yaml.safe_load(f)
# Write YAML to a file
with open("output.yaml", "w") as f:
yaml.dump(inventory, f, default_flow_style=False)
Network operations are inherently unreliable — devices may be unreachable, credentials may fail, and timeouts happen:
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.