Python for the Network Nerd
-
Upload
matt-bynum -
Category
Technology
-
view
866 -
download
1
Transcript of Python for the Network Nerd
PythonFor the Network Nerd
By Matt Bynum, A Network Nerd
About Me
● In IT for a total of 14 years● Doing Networking and VoIP for 13 of those● Consulting for 8 years● Leading a consulting practice for 2 years● Programming in Python for 3 Months
Disclaimer: I am not a Python expert.
APIsSDN Automation
Open Source
Python BasicsWorking through SSHImporting Devices from CSVBuilding Config TemplatesWhat to do Next
Python Basics
● 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
● myVar = “this is a string”● WAN01 = Router()● [WAN01, WAN02, CORE01, CORE02]● {‘ip’: ‘10.0.0.1’, ‘username’: ‘cisco’, ‘password’: ‘cisco’}● import csv
Python Fundies (cont.)
What version of Python?
Stick with Python 2 for now.
Recommended Python Tutorials
Codecademy.comUdacity.comLearnPythonTheHardWay.orgedX.org Intro to CompSci
Working through SSH
“But why not Telnet, Mr. Presenter?”
Netmiko● Based on Paramiko● Created by Kirk Byers, a CCIE R/S Emeritus● Found on GitHub: https://github.
com/ktbyers/netmiko ● Designed for Network Devices
Netmiko - Setupimport netmiko
device = {'ip': '192.168.0.1', 'username': 'admin', 'password': 'password123', 'secret': 'secret123', 'verbose': False}
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”]
for command in commands:
output += net_connect.send_command(command)
print output
Netmiko - Configsconfig = [“interface gig 0/0/1”, “description Uplink to Core”, “switchport mode trunk”]
net_connect.send_config_set(config)
Netmiko - Multi-deviceyourDevices = [{'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")
net_connect = SSHClass(**yourDevice)
Netmiko - Multi-device cont.commands = [“show ip int brief | e unass”, “show ip route”, “show cdp neighbors”]
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 Valuesimport 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 Jinja2import jinja2
device = {'hostname': 'DIST01', 'ip': '192.168.0.2', 'username': 'admin', 'password': 'password123', 'secret': 'secret123', 'verbose': False, 'interface': 'gig 0/0/1', 'description': 'CORE SWITCH 1'}
env = jinja2.Environment(loader=jinja2.FileSystemLoader('templates'),trim_blocks=True)
commands = env.get_template(“template.cfg”).render(device)
Rendered Configurationint gig 0/0/1
description Uplink from DIST01 to CORE SWITCH 1
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, any other bulk changes● Configuration Auditing● APIs● SNMP collection and graphing● Using YAML and JSON for templating config
elements
Where your job is headingDevOpsSDNAutomation and Orchestration
The End