KEMBAR78
Python for the Network Nerd | PDF
Python
For 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.
APIs
SDN
Automation
Open Source
Python Basics
Working through SSH
Importing Devices from CSV
Building Config Templates
What 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.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")
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 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('templates'),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
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 heading
DevOps
SDN
Automation and Orchestration
The End

Python for the Network Nerd

  • 1.
    Python For the NetworkNerd By Matt Bynum, A Network Nerd
  • 2.
    About Me ● InIT 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.
  • 3.
  • 5.
    Python Basics Working throughSSH Importing Devices from CSV Building Config Templates What to do Next
  • 6.
  • 7.
    ● Variables arereferences 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
  • 8.
    ● 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.)
  • 9.
    What version ofPython? Stick with Python 2 for now.
  • 10.
  • 11.
  • 12.
    “But why notTelnet, Mr. Presenter?”
  • 13.
    Netmiko ● Based onParamiko ● Created by Kirk Byers, a CCIE R/S Emeritus ● Found on GitHub: https://github. com/ktbyers/netmiko ● Designed for Network Devices
  • 14.
    Netmiko - Setup importnetmiko 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)
  • 15.
    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
  • 16.
    Netmiko - Configs config= [“interface gig 0/0/1”, “description Uplink to Core”, “switchport mode trunk”] net_connect.send_config_set(config)
  • 17.
    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") net_connect = SSHClass(**yourDevice)
  • 18.
    Netmiko - Multi-devicecont. 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
  • 19.
  • 20.
    Comma Separated Values importcsv devices = [] devicesDict = {} with open(“c:routers.csv”) as devicesFile: devicesDict = csv.DictReader(devicesFile, dialect='excel') for row in devicesDict: devices.append(row)
  • 21.
  • 22.
    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'}, ...]
  • 23.
  • 24.
    Jinja2 Templates {% ifinterface %} int {{ interface }} {% endif %} {% if description %} description Uplink from {{ hostname }} to {{ description }} {% endif %}
  • 25.
    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('templates'),trim_blocks=True) commands = env.get_template(“template.cfg”).render(device)
  • 26.
    Rendered Configuration int gig0/0/1 description Uplink from DIST01 to CORE SWITCH 1
  • 27.
    What to doNext “The future belongs to those who see possibilities before they become obvious.” - John Sculley
  • 28.
    Things to workon ● 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
  • 29.
    Where your jobis heading DevOps SDN Automation and Orchestration
  • 30.