Scripting the Catalyst: Beyond the Basics

Similar documents
IOS XE Architecture and Programmability

Introduction to Catalyst Programmability

Introduction to OpenConfig

Using the YANG Development Kit (YDK) with Cisco IOS XE

Coding Getting Started with Python

NXOS in the Real World Using NX-API REST

Empower your testing with Cisco Test Automation Solution Featuring pyats & Genie

Cisco IOS XR Programmability for Cloud-Scale Networking

Coding Intro to APIs and REST

DEVNET Introduction to Git. Ashley Roach Principal Engineer Evangelist

NETCONF Protocol. Restrictions for the NETCONF Protocol. Information About the NETCONF Protocol

Optimizing the Usability of YANG Models for Network Automation

Get Hands On With DNA Center APIs for Managing Intent

Catalyst 9K High Availability Lab

Hands On Exploration of NETCONF and YANG

Cisco Network Programmability for the Enterprise NPEN v1.0

PnP Deep Dive Hands-on with APIC-EM and Prime Infrastructure

Insights into your WLC with Wireless Streaming Telemetry

Your API Toolbelt Tools and techniques for testing, monitoring, and troubleshooting REST API requests

Automation and Programmability using Cisco Open NXOS and DevOps Tools

Using the Management Ethernet Interface

NetDevOps for the Network Dude How to get started with API's, Ansible and Python

Using the Management Ethernet Interface

CCNA Semester 2 labs. Labs for chapters 2 10

Consuming Model-Driven Telemetry

Basic Router Configuration

NSO in Brownfield: Fully Automated One-Click Reconciliation

Lab - Troubleshooting Connectivity Issues

NetDevOps Style Configuration Management for the Network

Application Hosting in IOS-XE

Technologies for the future of Network Insight and Automation

Automation with Meraki Provisioning API

Getting Started With Containers

Maximizing Network Programmability and Automation with Open NX-OS

DevNet Workshop-Hands-on with CloudCenter and Jenkins

Components to Use Data Models

Lab Configuring Basic Switch Settings (Solution)

Configuring Management Interfaces on Cisco IOS XR Software

Cisco UCS Agentless Configuration Management Ansible or Microsoft DSC

Using the Management Interfaces

Lab Configuring Switch Security Features Topology

Components to Use Data Models

VXLAN EVPN Fabric and automation using Ansible

Finesse APIs: Getting started with the REST APIs and XMPP events

Programmability and Automation on Cisco Nexus Platforms

CS IT ports switch basic configuration. Lecture (01) Introduction to 24 ports managed switches By: Dr. Ahmed ElShafee.

1 of :22

Managing Cisco UCS with the Python SDK

CloudCenter for Developers

BGP in the Enterprise for Fun and (fake) Profit: A Hands-On Lab

Designing and Implementing Cisco Network Programmability (NPDESI) v1.0

Using Setup Mode to Configure a Cisco Networking Device

Configuring IEEE 802.3ad Link Bundling and Load Balancing

Using the Cisco NX-OS Setup Utility

WORKSHOP: from Zero to a Network Application with #golang

Implementing the Network Configuration Protocol

Using the Cisco NX-OS Setup Utility

Hands-On with IoT Standards & Protocols

This guide presents the instructions and other information concerning the lab activities for this course.

Git, Atom, virtualenv, oh my! Learn about dev tools to live by!

Cisco WAN Automation Engine (WAE) Network Programmability with Segment Routing

2018 Cisco and/or its affiliates. All rights reserved. Cisco Public

Lab Exploring Cisco IOS and Configuring Basic Switch Settings

GRE Tunnel with VRF Configuration Example

Cisco SD-Access Hands-on Lab

APIs Assist Troubleshooting in Manufacturing

Configuring Ethernet Management Port

Cisco Plug and Play Feature Guide Cisco Services. Cisco Plug and Play Feature Guide Cisco and/or its affiliates.

Address Resolution Protocol

Internet of Things Field Network Director

RESTCONF Programmable Interface

Laboration 2 Troubleshooting Switching and First-Hop Redundancy

Configuring EtherChannel and 802.1Q Trunking Between Catalyst L2 Fixed Configuration Switches and a Router (InterVLAN Routing)

Using Setup Mode to Configure a Cisco Networking Device

Serviceability of SD-WAN

Lab Viewing Network Device MAC Addresses

Cisco Spark Messaging APIs - Integration Platforms as a Service Real World Use-Cases

Guest Shell. Finding Feature Information. Information About Guest Shell. Guest Shell Overview

Software Development I

The objective of this lab is to become familiar with Cisco switches as well as the Spanning Tree Protocol.

Configuring Gigabit Ethernet Interfaces

If this is your first time configuring the switch, you will notice that the Switch IOS is almost identical to Router IOS.

UniNets MPLS LAB MANUAL MPLS. UNiNets Multiprotocol label Switching MPLS LAB MANUAL. UniNets MPLS LAB MANUAL

IPv6 Tunnel through an IPv4 Network

Management IP Interface

TRex Realistic Traffic Generator

APIs and API Design with Python

The NX-API CLI also supports JSON/CLI Execution in Cisco Nexus 3500 Series devices.

An Introduction to Monitoring Encrypted Network Traffic with "Joy"

2 Initialize a git repository on your machine, add a README file, commit and push

Multi-domain Service Orchestration with Cisco Network Service Orchestrator. Gabor Szabo Version 1.2

Switch configuration. By the end of this session, you will be able to: Describe basic switch configuration methods. Configure a switch.

nacelle Documentation

Guest Shell. Finding Feature Information. Information About Guest Shell. Guest Shell Overview

Configuring Interfaces

CCNA Practice test. 2. Which protocol can cause high CPU usage? A. NTP B. WCCP C. Telnet D. SNMP Answer: D

Cisco SD-Access Building the Routed Underlay

Chapter 10 Configure Clientless Remote Access SSL VPNs Using ASDM

Model-Driven Telemetry. Shelly Cadora Principal Engineer, Technical Marketing

Programming Device APIs

Transcription:

Scripting the Catalyst: Beyond the Basics Jeff McLaughlin, Principal TME

Cisco Spark How Questions? Use Cisco Spark to communicate with the speaker after the session 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 cs.co/ciscolivebot# 2018 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

2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 5

2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 6

Why Programmability?

Why automation and programmability? Administrator Needs to configure hostname switch1 int g0/0 ip address 10.1.1.11/24 vlan 100,200,300... hostname switch6 int g0/0 ip address 10.1.1.16/24 vlan 100,200,300 2018 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 2018 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 2018 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 2018 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 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 12

Operational Simplification How to find the red user's switch/port? 2018 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? 2018 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? 2018 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 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 16

Operational Simplification Programmability Reason #5 Automate complex troubleshooting tasks 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 17

Time for a demo 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public

1 User types command into Spark 2 Command pulled down by script REST 5 Data posted back to Spark room 3 Script sends NETCONF request 4 Switch replies via NETCONF with data NETCONF Catalyst 3850 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 19

Planning Tools Models APIs Development 2018 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 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 22

What tool to use? CLI CFG MGMT TOOLS PYTHON SCRIPT Advantages: Easy to use Well documented Immediate result Advantages: Device roles Powerful templating Easy to learn Advantages: Numerous libraries Programmable intfs Well supported Disadvantages: Time consuming Tedious Error-prone Disadvantages: Less functionality Disadvantages: Development time 2018 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! 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 24

Which tools do I use? Catalyst 3850 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 25

Which tools do I use? REST! NETCONF? CLI? SNMP? Catalyst 3850 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 26

NETCONF, CLI, SNMP? CLI SNMP NETCONF Venerable/Ancient? Structured Data? Tooling? 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 27

Which tools do I use? REST! NETCONF! CLI? SNMP? Catalyst 3850 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 28

How do I build it? Catalyst 3850 Read Spark messages Post Spark messages Post PNG image Poll Spark room Parse Spark command Format data from switch Graph data from switch Read the routing table 2018 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 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 31

Getting Python 1 Download and install Python 2.7 or 3 2 PC/Windows Download and install PIP package manager 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 32

Python 2 vs 3 Python 2.7.10 >>> print "Hello World! Hello World! Python 2 Python 3.6.0a4 >>> print "Hello World!" SyntaxError: Missing parentheses >>> print ("Hello World") Hello World Python 3 Most common version Default installation on Linux Most libraries Losing popularity Several language enhancements Often separate installation Not all libraries supported Commonly used in books/courses Python 2 and 3 are not mutually compatible! 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 33

PIP Install $ pip install netaddr 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 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 34

What libraries do we need? 1 Something to do REST (requests) 2018 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) 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 36

What libraries do we need? 3 Something to do graphing (graphviz) 2018 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 NCClient = 0.4.5 Jinja2 = 2.8.1 Jinja2 = 2.7 NCClient = 0.4.5 paramiko = 2.1.1 Requests = 1.2.0 NCClient = 0.5.3 openpyxl = 2.4.1 2018 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. 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 39

Using VirtualEnvironments $ mkdir spark_app $ cd spark_app $ virtualenv env New python executable in /Users/jemclaug/Documents/Projects/CL LV 2017//env/bin/python Installing setuptools, pip, wheel...done. $ source env/bin/activate (env) $... (env) $ (env) $ deactivate $ Make a directory for your project Create a virtual environment Activate the virtual environment Do Pythonic stuff like PIP installs Deactivate 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 40

Git and version control... 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 41

Create code Modify code Save code Save code again 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 42

Initial Commit BGP over NC Added oper data Fixed nc_get Initial Commit BGP over NC Added oper data Fixed nc_get Version control scenario 1: You make changes and need to track them Version control scenario 2: You work with collaborator(s) and need to track and merge changes to code. 2018 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. 2018 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! 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 45

Tracking your project with Git 1 Create.gitignore file $ cat.gitignore.gitignore *.py[co] env/ (Sometimes) Tells git, don't track these files! 2018 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//spark_app/.git/ 3 Add file(s) to be tracked $ echo "My First File" > file.txt $ git add file.txt 2018 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! 2018 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 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 49

Planning Tools Models APIs Development 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 50

Data Models

Human-Oriented Interface Machine-Oriented Interface 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 52

Machines using human-oriented interfaces can be highly inefficient! 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 53

CLI YANG Models Human Oriented Interface Machine Oriented Interface 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 54

Structured vs Unstructured Data Un-structured Structured John Smith 42 14155551212 What is this? His age? The year he graduated college? Meaning of life, the universe & everything? Name: Age: Phone: Keys John Smith 42 +1-415-555-1212 Values 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 55

Hierarchical Structured Data (XML-like) First User Second User { { <user1> <name>john Smith</name> <age>42</age> <phone>+1-415-555-1212</phone> </user1> <user2> <name>sarah Kim</name> <age>27</age> <phone>+1-718-555-1212</phone> </user2> 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 56

Ancient unstructured data COULDYOUREADITIFWEWROTELIKETHIS WITHNOPUNCTUATIONITISHARDTOFIGUR EOUTWHEREONEWORDORSENTENCEBE GINSORENDSANDITISNOTEASYTOSEPAR ATEOUTALLTHEELEMENTSOFTHETEXTTH ANKGOODNESSSOMEONECAMEUPWITHA BETTERWAY 2018 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 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 58

What we need: Standard, structured way to represent configuration and operational data. <ipv4 xmlns="http://openconfig.net/yang/interfaces/ip"> <addresses> <address> <ip>172.26.194.212</ip> <config> <ip>172.26.194.212</ip> <prefix-length>24</prefix-length> </config> </address> </addresses> </ipv4> 2018 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. 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 60

XML vs JSON <interfaces xmlns:= [ ]yang:ietf-interfaces > <interface> <name>eth0</name> <type>ethernetcsmacd</type> <location>0</location> <enabled>true</enabled> <if-index>2</if-index> </interface> </interfaces> NETCONF/RESTCONF { } "ietf-interfaces:interfaces": { "interface": [ { "name": "eth0, "type": "ethernetcsmacd, "location": "0, "enabled": true, "if-index": 2 } ] } RESTCONF 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 61

Error! Sends <interface>gigabit 1/0</interface> <ifaddr>10.0.0.1/24</ifaddr> Expecting Expecting: <interface> <name>gigabit 1/0</name> <address>10.0.0.1/24</address> </interface> 2018 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> <descr>switch1 to UCS1</descr> <ip>1.1.1.1/24</ip> Question: Which of these is correct? Answer: They all are! <ifname>ethernet 0/0</ifname> <ifalias>switch1 to UCS1</ifalias> <ifaddr>1.1.1.1/24</ifaddr> 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 63

YANG Data Models container ip { list vrf { leaf rd } } vrf red rd 1:1 <vrf>red</vrf> <rd>1:1</rd> YANG Model Data XML Data YANG models do not contain data or XML. YANG models are like templates used to generate consistent XML. 2018 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 { leaf rd } } vrf red rd 1:1 XML YANG Model Data { vrf : red rd : 1:1 } JSON YANG models can be used as a template for generating structured data in many different formats. 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 65

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

So why is this: <ip> <vrf> <name>vrf_red</name> <rd>65000:1</rd> </vrf> <vrf> <name>vrf_green</name> <rd>65000:2</rd> </vrf> </ip>...better than this? ip vrf vrf_red rd 65001:1! ip vrf vrf_green rd 65001:2! 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 67

CLI ip vrf vrf_red rd 65001:1! ip vrf vrf_green rd 65001:2! Good for human consumption Unstructured from a machine perspective 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 68

YANG-structured data <ip> <vrf> <name>vrf_red</name> <rd>65000:1</rd> </vrf> <vrf> <name>vrf_green</name> <rd>65000:2</rd> </vrf> </ip> Designed for machine consumption Directly convertible to/from Python dicts! 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 69

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. 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 70

Who defines the YANG models? Vendors Standards Bodies Only work on specific vendor devices Greater feature coverage Can be OS-unique (IOS-XE, XR, etc.) Multi-vendor support More limited feature coverage Allow vendor-specific extensions 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 71

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"> <interface> <name>gigabitethernet 1/0/24</name> <description>configured by NETCONF!</description> </interface> </interfaces> IETF-defined model Both of these do exactly the same thing! <native xmlns="http://cisco.com/ns/yang/cisco-ios-xe-native"> <interface> <GigabitEthernet> <name>1/0/24</name> <description>configured by NETCONF!</description> </GigabitEthernet> </interface> </native> Cisco-defined native model 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 72

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"> <interface> <name>gigabitethernet 1/0/24</name> <description>configured by NETCONF!</description> </interface> </interfaces> switch# show run interface g1/0/24 interface Both of these GigabitEthernet do exactly the 1/0/24 same thing! description Configured by NETCONF! <native xmlns="http://cisco.com/ns/yang/cisco-ios-xe-native"> <interface> <GigabitEthernet> <name>1/0/24</name> <description>configured by NETCONF!</description> </GigabitEthernet> </interface> </native> IETF-defined model Cisco-defined native model 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 73

Configuration vs. Operational data Configuration data tells the device what to do. It is data that you see in a show run. Operational data tells us how a device is operating, from show commands other than show run. # sh run int g0/0 interface GigabitEthernet0/0 description Management Interface vrf forwarding Mgmt-vrf ip address 172.26.244.49 255.255.255.0 # sh int g0/0 GigabitEthernet0/0 is up, line protocol up Hardware is RP management port Description: Management Interface We can write configuration data (think conf t ), and we can read configuration data (think show run ). Operational data is read-only. Some data can be read either as config data or operational data! 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 74

Models and structured data are particularly important for efficiently reading operational data... 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 75

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 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 76

Regular Expressions -Stackexchange user 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 77

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 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 78

NETCONF vs. YANG Communication Protocol NETCONF Data Description YANG SNMP MIB/ASN.1 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 79

NETCONF protocol stack CONTENT XML (based on YANG) OPERATIONS GET, EDIT-CONFIG, ETC MESSAGES RPC SECURE TRANSPORT SSH 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 80

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 C3850-1(config)#aaa authorization exec default local C3850-1(config)#username admin password cisco C3850-1(config)#line vty 0 15 C3850-1(config-line)#transport input all C3850-1(config)#netconf-yang C3850-1(config)# Enable AAA Enable SSH Enable NETCONF 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 81

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 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 83

YangExplorer: A Cisco tool for exploring models Available at: https://github.com/ciscodevnet/yang -explorer Or just Google: "cisco yangexplorer" 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 84

YangExplorer: A Cisco tool for exploring models Models on device Models subscribed on YangExplorer 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 85

2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 86

Finding a model, method 1 Either scroll through the model list or search for a specific model. Click RPC. The XML is displayed. 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 87

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. 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 88

Planning Tools Models APIs Development 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 89

Python NETCONF Libraries

NCClient Python Library Raw XML NETCONF NCClient 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 91

NCClient "Get" Example 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 92

YANG Developer Kit (YDK) 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 93

NCClient: YDK: <interface> <GigabitEthernet> <name>1/0/14</name> <description>to_core_switch</description> <ip> <address> <primary> <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" gigabitethernet.ip.address.primary.address = "15.10.1.1" gigabitethernet.ip.address.primary.mask = "255.255.255.0" ip_add.gigabitethernet.append(gigabitethernet) 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 94

REST APIs

Consider some of the things you can do with an app like Spark... 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 96

Add a user to a room 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 97

Read a message 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 98

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 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 99

REST API calls use HTTP methods like GET, PUT and POST... 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 100

...and like a web page, REST APIs use URLs. 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 101

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 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 102

The headers of the request contain basic information such as an authentication token, while the body of the request contains the data to send. 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 103

How do I find REST APIs? REST APIs are useless unless they are documented. 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 104

Headers for call Content needed API Test Button! 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 105

Often you don t need to know REST to use REST! Libraries make the calls easy... 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 106

Our script will use a library I created, spark.py... Example: Post a message: Easier than... 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 107

Pulling it all together

Planning Tools Models APIs Development 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 109

Script Flow 1 What Pull messages from Spark How Spark library Example 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 110

Script Flow 2 What Parse message How Python conditions Example* * simple, but can be improved using NLP 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 111

Script Flow 3 What Collect routes How NETCONF w/ NCClient Example 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 112

Script Flow 4 (Optional) What Graph Routes How Graphviz Library Example 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 113

Script Flow 5 What Post response to Spark How Spark library Example (With graph) 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 114

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 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 116

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 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 117

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 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 118

Embedded Event Manager EEM takes certain actions based on triggering events. Events: cli temperature IPSLA Etc Trigger Actions: cli python reload syslog Etc 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 119

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 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 120

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. 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 122

Cisco DevNet Learning Labs Sandboxes API Documentation Python, YDK, REST And More! http://developer.cisco.com 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 123

"If a thing is worth doing, it is worth doing badly." - G.K. Chesterton 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 124

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) 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 125

Cisco Spark How Questions? Use Cisco Spark to communicate with the speaker after the session 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 cs.co/ciscolivebot# 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public

Please complete your Online Session Evaluations after each session Complete 4 Session Evaluations & the Overall Conference Evaluation (available from Thursday) to receive your Cisco Live T-shirt All surveys can be completed via the Cisco Live Mobile App or the Communication Stations Complete Your Online Session Evaluation Don t forget: Cisco Live sessions will be available for viewing on-demand after the event at www.ciscolive.com/global/on-demand-library/. 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public

Continue Your Education Demos in the Cisco campus Walk-in Self-Paced Labs Tech Circle Meet the Engineer 1:1 meetings Related sessions 2018 Cisco and/or its affiliates. All rights reserved. Cisco Public 128

Thank you