30
Python For the Network Nerd By Matt Bynum, A Network Nerd

Python for the Network Nerd

Embed Size (px)

Citation preview

Page 1: Python for the Network Nerd

PythonFor the Network Nerd

By Matt Bynum, A Network Nerd

Page 2: Python for the 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.

Page 3: Python for the Network Nerd

APIsSDN Automation

Open Source

Page 4: Python for the Network Nerd
Page 5: Python for the Network Nerd

Python BasicsWorking through SSHImporting Devices from CSVBuilding Config TemplatesWhat to do Next

Page 6: Python for the Network Nerd

Python Basics

Page 7: Python for the Network Nerd

● 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

Page 8: Python for the Network Nerd

● 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.)

Page 9: Python for the Network Nerd

What version of Python?

Stick with Python 2 for now.

Page 10: Python for the Network Nerd

Recommended Python Tutorials

Codecademy.comUdacity.comLearnPythonTheHardWay.orgedX.org Intro to CompSci

Page 11: Python for the Network Nerd

Working through SSH

Page 12: Python for the Network Nerd

“But why not Telnet, Mr. Presenter?”

Page 13: Python for the Network Nerd

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

Page 14: Python for the Network Nerd

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)

Page 15: Python for the Network Nerd

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

Page 16: Python for the Network Nerd

Netmiko - Configsconfig = [“interface gig 0/0/1”, “description Uplink to Core”, “switchport mode trunk”]

net_connect.send_config_set(config)

Page 17: Python for the Network Nerd

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)

Page 18: Python for the Network Nerd

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

Page 19: Python for the Network Nerd

Importing Devices from CSV

Page 20: Python for the Network Nerd

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)

Page 21: Python for the Network Nerd

CSV Input

Page 22: Python for the Network Nerd

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'},

...]

Page 23: Python for the Network Nerd

Building Config Templates

Page 24: Python for the Network Nerd

Jinja2 Templates{% if interface %}

int {{ interface }}

{% endif %}

{% if description %}

description Uplink from {{ hostname }} to {{ description }}

{% endif %}

Page 25: Python for the Network Nerd

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)

Page 26: Python for the Network Nerd

Rendered Configurationint gig 0/0/1

description Uplink from DIST01 to CORE SWITCH 1

Page 27: Python for the Network Nerd

What to do Next

“The future belongs to those who see possibilities before they become obvious.”

- John Sculley

Page 28: Python for the Network Nerd

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

Page 29: Python for the Network Nerd

Where your job is headingDevOpsSDNAutomation and Orchestration

Page 30: Python for the Network Nerd

The End