You are on page 1of 129

Scripting the Catalyst:

Beyond the Basics

Jeff McLaughlin, Principal TME


BRKCRS-2451
Cisco Spark
Questions?
Use Cisco Spark to chat with the
speaker after the session

How
1. Find this session in the Cisco Live Mobile App
2. Click “Join the Discussion”
3. Install Spark or go directly to the space
4. Enter messages/questions in the space

Cisco Spark spaces will be cs.co/ciscolivebot#BRKCRS-2451


available until July 3, 2017.

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
Agenda

• Why programmability?
• Planning a script
• Python and Tools
• Finding and using Models
• NCClient and YDK-Py
• REST APIs
• Completing the script
• On-Box Python
5
BRKCRS-2451
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
6
BRKCRS-2451
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
Why Programmability?
Why automation and programmability?

hostname switch1
int g0/0
ip address 10.1.1.11/24
vlan 100,200,300

.
Needs to configure
Administrator
.
.
hostname switch6
int g0/0
ip address 10.1.1.16/24
vlan 100,200,300

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 8
Notepad is the most common automation tool. It’s just a very bad automation tool.

...

Programmability Reason #1 Do repetitive and tedious tasks more easily

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 9
52037606 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored

if error counters too high:


then shutdown interface*

* pseudo-code

Programmability Reason #2 Programmatic Control of network devices

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 10
party apps

Cisco ISE

NETCONF REST API

Catalyst switches

APIC-EM

Programmability Reason #3 Interaction between network devices and other systems

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 11
Transactionality

int g0/0
ip address 10.1.1.0/24
no shutdown
router bgp 65001
router-id 172.17.1.99
bgp log-neighbor-changes
neighbor 192.168.1.2 remote-as 40000
neighbor 192.168.3.2 remote-as 50000
address-family ipv4 unicast
neighbor 192.168.1.2 activate
network 172.17.1.0 mask 255.255.255.0
exit-address-family

Programmability Reason #4 Stop bad configuration being committed to devices

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 12
Operational Simplification

How to find the red


user's switch/port?

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 13
Operational Simplification
# ping 172.16.100.101
# show arp | i 172.16.100.101
# show mac address-table address 001a.a24d.5141
# show cdp neighbor g0/1 detail

How to find the red


user's switch/port?

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 14
Operational Simplification
# show mac address-table address 001a.a24d.5141
# show cdp neighbor g0/10 detail

How to find the red


user's switch/port?

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 15
Operational Simplification

How to find the red


user's switch/port?

# show mac address-table address 001a.a24d.5141


# show cdp neighbor g0/10 detail
Vlan Mac Address Type Ports
---- ----------- -------- -----
244 001a.a24d.5141 DYNAMIC Gi0/15

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 16
Operational Simplification

Programmability Reason #5 Automate complex troubleshooting tasks

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 17
Time for a demo
REST
1 User types command into Spark
2 Command pulled down by script

5 Data posted back to Spark room

3 Script sends NETCONF request 4 Switch replies via NETCONF with data

NETCONF

Catalyst 3850

19
BRKCRS-2451
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
Planning Tools Models APIs

Development

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 20
Planning your script
What do I want to do?

Catalyst 3850

1. Display the routing table


2. Graph the routing table

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 22
What tool to use?

CLI CFG MGMT TOOLS PYTHON SCRIPT

Advantages: Advantages: Advantages:


• Easy to use • Device roles • Numerous libraries
• Well documented • Powerful templating • Programmable intfs
• Immediate result • Easy to learn • Well supported

Disadvantages: Disadvantages: Disadvantages:


• Time consuming • Less functionality • Development time
• Tedious
• Error-prone

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 23
Python: Why not C, Ruby, Perl, TCL, Go, etc.?

Easy to Learn
 Interactive shell
 Easy to begin with simple scripts

Libraries!
 Extensive libraries, like an “app store” for developers
 Read Excel, output PDF, draw graphs, etc.

And it’s well supported in the networking community!


BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 24
Which tools do I use?

Catalyst 3850

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 25
Which tools do I use?

NETCONF? Catalyst 3850


REST! CLI?
SNMP?

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 26
NETCONF, CLI, SNMP?

CLI SNMP NETCONF

Venerable/Ancient?

Structured Data?

Tooling?

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 27
Which tools do I use?

NETCONF! Catalyst 3850


REST! CLI?
SNMP?

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 28
How do I build it?

Catalyst 3850

• Read Spark messages • Poll Spark room • Read the routing table
• Post Spark messages • Parse Spark command
• Post PNG image • Format data from switch
• Graph data from switch

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 29
Setting up Python environment
Getting Python

Mac Python is probably


already installed.

Unix/Linux

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 31
Getting Python

Download and install


Python 2.7 or 3

Download and install


PIP package manager
PC/Windows

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 32
Python 2 vs 3
Python 2.7.10 Python 3.6.0a4
>>> print "Hello World!” >>> print "Hello World!"
Hello World! SyntaxError: Missing parentheses
>>> print ("Hello World")
Hello World

Python 2 Python 3

• Most common version • Several language enhancements


• Default installation on Linux • Often separate installation
• Most libraries • Not all libraries supported
• Losing popularity • Commonly used in books/courses

Python 2 and 3 are not mutually compatible!

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 33
$ pip install netaddr
PIP Install Downloading/unpacking netaddr
Downloading netaddr-0.7.19-py2.py3-none-any.whl
(1.6MB): 1.6MB downloaded
Installing collected packages: netaddr
Successfully installed netaddr
Cleaning up...

PDFrw
YDK-Py

OpenPy
XL

Python
NCClient

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 34
What libraries do we need?

1 Something to do REST (requests)

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 35
What libraries do we need?

NETCONF

Catalyst 3850

2 Something to do NETCONF (ncclient)

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 36
What libraries do we need?

2 Something to do graphing (graphviz)

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 37
Virtual Environments

V-Env1 V-Env2 V-Env3

Python 2.5 Python 2.7 Python 2.7

Requests = 1.2.0 Jinja2 = 2.7 Requests = 1.2.0


NCClient = 0.4.5 NCClient = 0.4.5 NCClient = 0.5.3
Jinja2 = 2.8.1 paramiko = 2.1.1 openpyxl = 2.4.1

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 38
Installing Virtual Environment

$ pip install virtualenv


Collecting virtualenv
Downloading virtualenv-15.1.0-py2.py3-none-any.whl
(1.8MB)
100% |████████████████████████████████| 1.8MB
658kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-15.1.0

Install Virtualenv with PIP like any Python library.

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 39
Using VirtualEnvironments

$ mkdir spark_app Make a directory for your project


$ cd spark_app
$ virtualenv env Create a virtual environment
New python executable in
/Users/jemclaug/Documents/Projects/CL LV
2017/BRKCRS-2451/env/bin/python
Installing setuptools, pip, wheel...done.
$ source env/bin/activate Activate the virtual environment
(env) $
... Do Pythonic stuff like PIP installs
(env) $
(env) $ deactivate Deactivate
$

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 40
Git and version control...

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 41
Create code Modify code

Save code Save code again

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 42
Added oper data
Initial Commit

BGP over NC

Fixed nc_get
Version control scenario 1:
You make changes and need to track them

Added oper data


Initial Commit

BGP over NC

Fixed nc_get
Version control scenario 2:
You work with collaborator(s) and need to track
and merge changes to code.

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 43
Git is a version control system.

GitHub is an online source code repository.

GitHub provides an easy way to share code and


collaborate. It works together with Git.

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 44
Installing git
Install Git using the standard installers on Mac/Windows
Use the usual methods on Linux (apt-get, yum, rpm, etc.)

Highly Recommended: Install GitHub Desktop!

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 45
Tracking your project with Git
1 Create .gitignore file

$ cat .gitignore
.gitignore (Sometimes)
*.py[co]
env/

Tells git, don't track these files!

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 46
Tracking your project with Git
2 Initialize the repo

JEMCLAUG-M-34E1:spark_app jemclaug$ git init


Initialized empty Git repository in /Users/jemclaug/Documents/Projects/CL
LV 2017/BRKCRS-2451/spark_app/.git/

3 Add file(s) to be tracked


$ echo "My First File" > file.txt
$ git add file.txt

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 47
Tracking your project with Git
4 Commit your changes
$ git commit
Aborting commit due to empty commit message.
$ git commit -m "Added my first file"
[master (root-commit) cba52b5] Added my first file
1 file changed, 1 insertion(+)
create mode 100644 file.txt

5 View changes in GH Desktop!

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 48
Cloning repo's from GitHub

$ git clone https://github.com/ccie14023/pyfabric


Cloning into 'pyfabric'...
remote: Counting objects: 89, done.
remote: Total 89 (delta 0), reused 0 (delta 0), pack-reused 89
Unpacking objects: 100% (89/89), done.
$ cd pyfabric/
$ virtualenv env
New python executable in /Users/jemclaug/pyfabric/env/bin/python
Installing setuptools, pip, wheel...done.
$ source env/bin/activate
(env) $ pip install -r requirements.txt
Collecting cffi==1.9.1 (from -r requirements.txt (line 1))
Using cached cffi-1.9.1-cp27-cp27m-macosx_10_10_intel.whl
Collecting cryptography==1.7.1 (from -r requirements.txt (line 2))
Using cached cryptography-1.7.1-cp27-cp27m-macosx_10_10_intel.whl

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 49
Planning Tools Models APIs

Development

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 50
Data Models
Human-Oriented Interface

Machine-Oriented Interface

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 52
Machines using human-oriented interfaces can be highly inefficient!
53
BRKCRS-2451
© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
CLI YANG Models

Human Oriented Interface Machine Oriented Interface

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 54
Structured vs Unstructured Data
Un-structured
Structured

John Smith 42 14155551212 Name: John Smith


Age: 42
Phone: +1-415-555-1212
What is this?

• His age? Keys Values


• The year he graduated college?
• Meaning of life, the universe & everything?

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 55
Hierarchical Structured Data (XML-like)

<user1>

First User
{ <name>John Smith</name>
<age>42</age>
<phone>+1-415-555-1212</phone>
</user1>

<user2>

Second User
{ <name>Sarah Kim</name>
<age>27</age>
<phone>+1-718-555-1212</phone>
</user2>

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 56
Ancient unstructured data

COULDYOUREADITIFWEWROTELIKETHIS
WITHNOPUNCTUATIONITISHARDTOFIGUR
EOUTWHEREONEWORDORSENTENCEBE
GINSORENDSANDITISNOTEASYTOSEPAR
ATEOUTALLTHEELEMENTSOFTHETEXTTH
ANKGOODNESSSOMEONECAMEUPWITHA
BETTERWAY

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 57
Note inconsistent “key” format!
switch1# sh int e1/10
Ethernet1/10 is up
Hardware: 1000/10000 Ethernet, address: 0005.73d0.9331 (bia 0005.73d0.9331)
Description: To UCS-11
MTU 1500 bytes, BW 1000000 Kbit, DLY 10 usec,
reliability 255/255, txload 1/255, rxload 1/255
Switchport monitor is off
EtherType is 0x8100
Last link flapped 8week(s) 2day(s)
Last clearing of "show interface" counters 1d02h
30 seconds input rate 944 bits/sec, 118 bytes/sec, 0 packets/sec
30 seconds output rate 3110376 bits/sec, 388797 bytes/sec, 5221 packets/sec

CLI = Unstructured Data

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 58
<ipv4 xmlns="http://openconfig.net/yang/interfaces/ip">
<addresses>
<address>
<ip>172.26.194.212</ip>
What we need:
<config>
Standard, structured way to represent <ip>172.26.194.212</ip>
configuration and operational data. <prefix-length>24</prefix-length>
</config>
</address>
</addresses>
</ipv4>

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 59
The train to Paris
leaves at 11:30.

Le train à Paris part à


11:30.

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 60
XML vs JSON

{
<interfaces xmlns:=“[…]yang:ietf-interfaces”> "ietf-interfaces:interfaces": {
<interface> "interface": [
{
<name>eth0</name> "name": "eth0”,
<type>ethernetCsmacd</type> "type": "ethernetCsmacd”,
<location>0</location> "location": "0”,
<enabled>true</enabled> "enabled": true,
<if-index>2</if-index> "if-index": 2
}
</interface> ]
</interfaces> }
}

NETCONF RESTCONF
BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 61
Error!
<interface>Gigabit 1/0</interface>
<ifaddr>10.0.0.1/24</ifaddr>
Sends
Expecting

Expecting:
<interface>
<name>Gigabit 1/0</name>
<address>10.0.0.1/24</address>
</interface>

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 62
So why do we need YANG?

<interface>Ethernet 0/0</interface>
<name>Switch1 to UCS1</name>
<ipaddr>1.1.1.1/24</ipaddr>

<name>Ethernet 0/0</name> Question: Which of these is correct?


<descr>Switch1 to UCS1</descr>
Answer: They all are!
<ip>1.1.1.1/24</ip>

<ifname>Ethernet 0/0</ifname>
<ifalias>Switch1 to UCS1</ifalias>
<ifaddr>1.1.1.1/24</ifaddr>

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 63
YANG Data Models

container ip {
list vrf { red_vrf
rd 65001:1 <vrf>red</vrf>
leaf rd
<rd>1:1</rd>
}
}

YANG Data XML


Model Data

YANG models do not contain data or XML.


YANG models are like templates used to generate consistent XML.

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 64
YANG Data Models
<vrf>red</vrf>
<rd>1:1</rd>

container ip {
list vrf { red_vrf
leaf rd rd 65001:1 XML
}
}

{“vrf”: “red”
YANG Data “rd”: “1:1”}
Model

JSON

YANG models can be used as a template for generating structured


data in many different formats.

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 65
YANG Configuration Model Example*
container ip { YANG <ip> XML
list vrf { <vrf>
description <name>vrf_red</name>
"Configure an IP VPN Routing/Forwarding <rd>65000:1</rd>
instance"; </vrf>
<vrf>
leaf name { <name>vrf_green</name>
type string; <rd>65000:2</rd>
} </vrf>
</ip>
leaf rd {
description ip vrf vrf_red CLI
"Specify Route Distinguisher"; rd 65001:1
type rd-type; !
} ip vrf vrf_green
} rd 65001:2
} !

* Note: YANG model simplified for clarity BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 66
Where are YANG models?

Models installed on device automatically with IOS-XE.


On some devices/versions, can be updated independently

https://github.com/YangModels/yang/tree/master/vendor/cisco

Also can be downloaded from GitHub.

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 67
Who defines the YANG models?
Vendors Standards Bodies

• Only work on specific vendor devices • Multi-vendor support


• Greater feature coverage • More limited feature coverage
• Can be OS-unique (IOS-XE, XR, etc.) • Allow vendor-specific extensions

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 68
Important Point!
Cisco’s data models and IETF/OpenConfig data models are just two ways of doing the same thing.

<interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"> IETF-defined model


<interface>
<name>GigabitEthernet 1/0/24</name>
<description>Configured by NETCONF!</description>
</interface>
</interfaces>

Both of these do exactly the same thing!

<native xmlns="http://cisco.com/ns/yang/ned/ios"> Cisco-defined “NED” model


<interface>
<GigabitEthernet>
<name>1/0/24</name>
<description>Configured by NETCONF!</description>
</GigabitEthernet>
</interface>
</native>

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 69
Important Point!
Cisco’s data models and IETF/OpenConfig data models are just two ways of doing the same thing.

<interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"> IETF-defined model


<interface>
<name>GigabitEthernet 1/0/24</name>
<description>Configured by NETCONF!</description>
</interface>
</interfaces>
switch# show run interface g1/0/24
interface GigabitEthernet
Both of these do exactly the 1/0/24
same thing!
description Configured by NETCONF!
<native xmlns="http://cisco.com/ns/yang/ned/ios"> Cisco-defined “NED” model
<interface>
<GigabitEthernet>
<name>1/0/24</name>
<description>Configured by NETCONF!</description>
</GigabitEthernet>
</interface>
</native>

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 70
Models and structured data are particularly important
for efficiently reading operational data...

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 71
Configuration vs. Operational data
Configuration data tells the device what to do. It is Operational data tells us how a device is operating,
data that you see in a “show run”. from show commands other than “show run”.

# sh run int mgmt0 # sh int mgmt0


mgmt0 is up
interface mgmt0 admin state is up
description N7K_ToR_Mgmt Description: N7K_ToR_Mgmt
vrf member management Internet Address is 172.26.244.162/24
ip address 172.26.244.162/24 110380 input packets

We can write configuration data (think “conf t”), Operational data is read-only.
and we can read configuration data (think “show
run”).

Some data can be read either as config data or


operational data!
BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 72
PID Runtime(ms) Invoked uSecs 5Sec 1Min 5Min TTY Process
1 3 31 96 0.00% 0.00% 0.00% 0 Chunk Manager
2 3687 4786 770 0.07% 0.01% 0.00% 0 Load Meter

Challenge: Write a Python script to go through the list of nearly 500 running processes
and print the names of only those with runtime of 10 seconds or greater.

Regex hard to understand

Tied directly to table layout

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 73
Regular Expressions

-Stackexchange user

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 74
PID Runtime(ms) Invoked uSecs 5Sec 1Min 5Min TTY Process
1 3 31 96 0.00% 0.00% 0.00% 0 Chunk Manager
2 3687 4786 770 0.07% 0.01% 0.00% 0 Load Meter

Challenge: Write a Python script to go through the list of nearly 500 running processes
and print the names of only those with runtime of 10 seconds or greater.

XML easily rendered as Python dict


Uses YANG data models
Intuitive nomenclature

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 75
NETCONF vs. YANG

Communication
Protocol Data Description

NETCONF YANG

SNMP MIB/ASN.1

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 76
NETCONF protocol stack

CONTENT XML (based on YANG)

OPERATIONS GET, EDIT-CONFIG, ETC

MESSAGES RPC

SECURE TRANSPORT SSH

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 77
Enabling NETCONF: 3 Steps

C3850-1#conf t
Enter configuration commands, one per line. End with CNTL/Z.
C3850-1(config)#aaa new-model
C3850-1(config)#aaa authentication login default local Enable AAA
C3850-1(config)#aaa authorization exec default local
C3850-1(config)#username admin password cisco

C3850-1(config)#line vty 0 15 Enable SSH


C3850-1(config-line)#transport input all

C3850-1(config)#netconf-yang Enable NETCONF


C3850-1(config)#

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 78
Finding and using YANG models
What are we looking for?

Extract route and next hop interface or IP from routing table


NO regular expressions!!!
Open model if possible
BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 80
YangExplorer: A Cisco tool for exploring models

Available at:
https://github.com/CiscoDevNet/yang
-explorer

Or just Google: "cisco yangexplorer"

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 81
YangExplorer: A Cisco tool for exploring models

Models on device

Models subscribed on YangExplorer

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 82
BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 83
Finding a model, method 1

Either scroll through the


model list or search for a
specific model.

Click “RPC”.

The XML is displayed.

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 84
Finding a model, method 2

Pull the config from an


existing device.

Select “get-config” for the


hierarchy you need.

Click Run RPC.

The XML is displayed.

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 85
Planning Tools Models APIs

Development

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 86
Python NETCONF Libraries
NCClient Python Library

Raw XML

NETCONF

NCClient

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 88
NCClient "Get" Example

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 89
YANG Developer Kit (YDK)

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 90
<interface>
<GigabitEthernet>
<name>1/0/14</name>
<description>To_Core_Switch</description>
<ip>
<address>
<primary>
NCClient: <address>15.10.1.1</address>
<mask>255.255.255.0</mask>
</primary>
</address>
</ip>
</GigabitEthernet>
</interface>

gigabitethernet= interface.Gigabitethernet()
gigabitethernet.name = "1/0/14"
gigabitethernet.description = "To_Core_Switch"
YDK:
gigabitethernet.ip.address.primary.address = "15.10.1.1"
gigabitethernet.ip.address.primary.mask = "255.255.255.0"
ip_add.gigabitethernet.append(gigabitethernet)
BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 91
REST APIs
Consider some of the things you can do with an app like Spark...

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 93
Add a user to a room

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 94
Read a message

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 95
API’s allow you to do same things with a script instead of by clicking..

Create a space Add a user to a space Read a message

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 96
REST API calls use HTTP methods like GET, PUT and POST...

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 97
...and like a web page, REST APIs use URLs.

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 98
REST API URLs determine what resource is being accessed.

POST https://api.ciscospark.com/v1/rooms

POST https://api.ciscospark.com/v1/messages

POST https://api.ciscospark.com/v1/team/memberships

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 99
The headers of the request contain basic information such as an authentication token,
while the body of the request contains the data to send.

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 100
How do I find REST APIs?
REST APIs are useless unless they are documented.

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 101
Headers for call

Content needed

API Test Button!

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 102
Often you don’t need to know REST to use REST!

Libraries make the calls easy...

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 103
Our script will use a library I created, spark.py...

Example: Post a message:

Easier than...

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 104
Pulling it all together
Planning Tools Models APIs

Development

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 106
Script Flow

What How Example


Pull Spark
messages library
from Spark

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 107
Script Flow

What How Example*


Parse Python
message conditions

* simple, but can be improved using NLP

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 108
Script Flow

What How Example


Collect NETCONF
routes w/ NCClient

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 109
Script Flow

4 (Optional)

What How Example


Graph Graphviz
Routes Library

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 110
Script Flow

What How
Post Spark
response to library
Spark

Example (With graph)

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 111
On-Box Scripting
Off-box Python Scripting

NETCONF*

Off-box scripts run on an external server and communicate with the


switch over the network using NETCONF or other protocols.

* or other protocol BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 113
On-box Python Scripting
CLI
NETCONF
Syslog

On-box Python scripts run in a container on


the device itself. They can communicate with
the network or the device itself.

Guestshell
Container

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 114
Advantages

On-Box
• Access CLI directly on device
• Trigger syslog messages
• Interact with Embedded Event Manager
• Access device bootflash
• Zero Touch Provisioning
• Use interactive Python shell

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 115
Embedded Event Manager
• EEM takes certain actions based on triggering events.

Events: Actions:
• cli • cli
• temperature • python
• IPSLA Trigger • reload
• Etc… • syslog
• Etc…

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 116
3 Spark posts diff to room

4 Python script diffs configs and sends diff to Spark


Catalyst 3850

1 User changes device config

EEM
2 Change detected by EEM

3 EEM Triggers on-box Python script BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 117
Wrapping Up
How do I learn Python?
Automate the Boring Stuff with Python, Al Sweigart
Great introduction to Python focused on automation. (Not specifically network
automation.) Covers Python 3.0 only. Assumes zero knowledge. Read Excel
docs, generate PDFs, etc. Highly recommended.

Real Python. http://realpython.com


Three-part course. Begins with basics assuming no knowledge. Covers Python 2.7
and 3.0. Parts II and III focus on web development with Python. Covers flask,
Django, jinja2 templates. Many resources on the web site for free.

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 119
Cisco DevNet

• Learning Labs
• Sandboxes
• API Documentation
• Python, YDK, REST
• And More!

http://developer.cisco.com
BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 120
"If a thing is worth doing, it is worth doing badly."
- G.K. Chesterton

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 121
• Work in the USB lab
• Identify one problem you can solve with a script
• Start small
• Copy and mod scripts from DevNet
• (developer.cisco.com)

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 122
Lab on a stick (USB, that is)

• Ubuntu VM:
• YangExplorer Installed
• NCClient Scripts
• YDK-Py Scripts
• CSR1kv with IOS XE 16.5
• Functionally similar to Cat3k/9k for
YANG/NETCONF

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 123
Complete Your Online
Session Evaluation
• Give us your feedback to be
entered into a Daily Survey
Drawing. A daily winner will
receive a $750 gift card.
• Complete your session surveys
through the Cisco Live mobile
app or on www.CiscoLive.com/us.

Don’t forget: Cisco Live sessions will be


available for viewing on demand after the
event at www.CiscoLive.com/Online.

© 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public
Continue Your Education
• Demos in the Cisco campus
• Walk-in Self-Paced Labs
• Lunch & Learn
• Meet the Engineer 1:1 meetings
• Related sessions

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 125
Thank you
R&S Related Cisco Education Offerings
Course Description Cisco Certification
CCIE R&S Advanced Workshops (CIERS-1 & Expert level trainings including: instructor led workshops, self CCIE® Routing & Switching
CIERS-2) plus assessments, practice labs and CCIE Lab Builder to prepare candidates
Self Assessments, Workbooks & Labs for the CCIE R&S practical exam.

• Implementing Cisco IP Routing v2.0 Professional level instructor led trainings to prepare candidates for the CCNP® Routing & Switching
• Implementing Cisco IP Switched CCNP R&S exams (ROUTE, SWITCH and TSHOOT). Also available in
Networks V2.0 self study eLearning formats with Cisco Learning Labs.
• Troubleshooting and Maintaining
Cisco IP Networks v2.0

Interconnecting Cisco Networking Devices: Configure, implement and troubleshoot local and wide-area IPv4 and IPv6 CCNA® Routing & Switching
Part 2 (or combined) networks. Also available in self study eLearning format with Cisco Learning
Lab.

Interconnecting Cisco Networking Devices: Installation, configuration, and basic support of a branch network. Also CCENT® Routing & Switching
Part 1 available in self study eLearning format with Cisco Learning Lab.

For more details, please visit: http://learningnetwork.cisco.com


Questions? Visit the Learning@Cisco Booth

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 128
Network Programmability Cisco Education Offerings
Course Description Cisco Certification
Developing with Cisco Network Programmability Provides Application Developers with comprehensive curriculum to Cisco Network Programmability
(NPDEV) develop infrastructure programming skills; Developer (NPDEV) Specialist
Addresses needs of software engineers who automate network Certification
infrastructure and/or utilize APIs and toolkits to interface with SDN
controllers and individual devices

Designing and Implementing Cisco Network Provides network engineers with comprehensive soup-to-nuts curriculum Cisco Network Programmability
Programmability (NPDESI) to develop and validate automation and programming skills; Design and Implementation
Directly addresses the evolving role of network engineers towards more (NPDESI) Specialist Certification
programmability, automation and orchestration

Programming for Network Engineers (PRNE) Learn the fundamentals of Python programming – within the context of Recommended pre-requisite for
performing functions relevant to network engineers. Use Network NPDESI and NPDEV Specialist
Programming to simplify or automate tasks Certifications

Cisco Digital Network Architecture This training provides students with the guiding principles and core None
Implementation Essentials (DNAIE) elements of Cisco’s Digital Network Architecture (DNA) architecture and its
solution components including; APIC-EM, NFV, Analytics, Security and
Fabric.

For more details, please visit: http://learningnetwork.cisco.com


Questions? Visit the Learning@Cisco Booth

BRKCRS-2451 © 2017 Cisco and/or its affiliates. All rights reserved. Cisco Public 129

You might also like