Python For The Network Nerd
Python For The Network Nerd
Python For The Network Nerd
Automatio
SDN n
API
s
Python Basics
Working through SSH
Importing Devices from CSV
Building Config Templates
What to do Next
Python Basics
Python Fundies
● Variables are references to objects.
● Objects can be built-in like strings, or “things” that can
be manipulated or passed around.
● Lists contain objects.
● Dictionaries are containers that use key:value pairing to
store objects, and coincidentally, are also objects
themselves.
● Libraries are Python packages that can be used over
and over
Python Fundies (cont.)
● myVar = “this is a string”
● WAN01 = Router()
● [WAN01, WAN02, CORE01, CORE02]
● {‘ip’: ‘10.0.0.1’, ‘username’: ‘cisco’, ‘password’: ‘cisco’}
● import csv
What version of Python?
SSHClass = netmiko.ssh_dispatcher(device_type="cisco_ios")
net_connect = SSHClass(**device)
Netmiko - Commands
# Single Command
output = net_connect.send_command(“show run | i snmp”)
print output
# Multiple Commands
commands = [“show ip int brief | e unass”, “show ip route”, “show cdp neighbors”]
net_connect.send_config_set(config)
Netmiko - Multi-device
yourDevices = [{'ip': '192.168.0.1', 'username': 'admin', 'password': 'password123',
'secret': 'secret123', 'verbose': False},{'ip': '192.168.0.2', 'username': 'admin',
'password': 'password123', 'secret': 'secret123', 'verbose': False}]
SSHClass = netmiko.ssh_dispatcher(device_type="cisco_ios")
commands = [“show ip int brief | e unass”, “show ip route”, “show cdp neighbors”]
Netmiko - Multi-device cont.
for each in yourDevices:
net_connect = SSHClass(**each)
output = ''
for command in commands:
output += net_connect.send_command(command)
print output
Importing Devices from CSV
Comma Separated Values
import csv
devices = []
devicesDict = {}
with open(“c:\\routers.csv”) as devicesFile:
devicesDict = csv.DictReader(devicesFile, dialect='excel')
for row in devicesDict:
devices.append(row)
CSV Input
CSV Output
[{'username': 'mbyn', 'verbose': 'True', 'ip': '172.20.5.14', 'hostname':
'TEST_DEVICE_1', 'secret': '', 'password': 'fakepw'},
{'username': 'mbyn', 'verbose': 'True', 'ip': '172.20.5.15', 'hostname':
'TEST_DEVICE_2', 'secret': '', 'password': 'fakepw'},
...]
Building Config Templates
Jinja2 Templates
{% if interface %}
int {{ interface }}
{% endif %}
{% if description %}
description Uplink from {{ hostname }} to {{ description }}
{% endif %}
Python Jinja2
import jinja2
env = jinja2.Environment(loader=jinja2.FileSystemLoader('myTemplates'),trim_blocks=True)
commands = env.get_template(“template.cfg”).render(device)
Rendered Configuration
# int gig 0/0/1
# description Uplink from DIST01 to CORE SWITCH 1
config = output.splitlines()
# [“int gig 0/0/1\n”, “description Uplink from DIST01 to CORE SWITCH 1\n”]
What to do Next
“The future belongs to those who see possibilities before they become obvious.”
- John Sculley
Things to work on
● Scripting VLAN Moves/Adds/Changes, QoS
deployments, other changes or “show” cmds
● Configuration Auditing
● APIs
● SNMP collection and graphing
● Using YAML and JSON for templating config
elements
Where your job is heading
● DevOps
● SDN
● Automation and Orchestration
The End
slideshare.net/mbynum
linkedin.com/in/mattbynum