Python For The Network Nerd

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Python

For the Network Nerd


By Matt Bynum, A Network Nerd
● 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

About Me Disclaimer: I am not a Python expert.


Open Source

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?

Stick with Python 2 for now.


But by all means, try these libraries in Python 3.
Recommended Python Tutorials
Codecademy.com
Udacity.com
LearnPythonTheHardWay.org
edX.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 - Setup
import 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 - Configs
config = [“interface gig 0/0/1”, “description Uplink to Core”, “switchport mode trunk”]

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

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('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

You might also like