netops coding 101 - nanog archive · netops coding 101 …building your first robot! nanog 66...

251
NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford

Upload: others

Post on 23-Aug-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

NetOps Coding 101…building your first robot!

NANOG 66

02.09.2016

david swafford

Page 2: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

what we’ll

accomplish

2

Page 3: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

automating

3

Page 4: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

the remediation of network faults

4

Page 5: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

using the examples of

link flaps &

line card failures

5

Page 6: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

based on parsing of

syslog messages

6

Page 7: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

focusing today

7

Page 8: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

coding in

Python

8

Page 9: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

interacting with

network devices

through software

9

Page 10: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

regular

expressions

10

Page 11: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

keeping track of

what happened

11

Page 12: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

12

Page 13: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

how it works

13

Page 14: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

14

device

syslog server

sendsmessage

parser

tails messages

1

23

remediationruns

remediates4

system design

Page 15: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

15

device

syslog server

sendsmessage

parser

tails messages

1

23

remediationruns

remediates4

system design

Page 16: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

16

device

syslog server

sendsmessage

parser

tails messages

1

23

remediationruns

remediates4

system design

Page 17: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

17

device

syslog server

sendsmessage

parser

tails messages

1

23

remediationruns

remediates4

system design

Page 18: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

live demo!

18

Page 19: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

19

Page 20: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

20

Page 21: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

21

Page 22: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

22

Page 23: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

23

Page 24: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

24

Page 25: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

25

Page 26: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

26

Page 27: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

27

syslog message

problem identified

solution suggested

Page 28: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

28

Page 29: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

the lab

environment

29

Page 30: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

a virtual machine of

Ubuntu desktop

30

Page 31: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

31

customized with

iPython!

and extras

Python 2.7.11 & 3.5.1

MySQL server & Python libs

Page 32: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

32

importing the

virtual appliance

Page 33: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

33

download it from netengcode.com!

Page 34: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

34

Page 35: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

35

Page 36: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

36

Page 37: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

37

Page 38: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

38

TERMINAL

log-in details

user:demopass:demo

Page 39: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

39

keyboard shortcuts to break out of the VM

f enter or leave full-screen

Page 40: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

40

Page 41: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

41

introducing

IPython!

http://ipython.org/ipython-doc/3/install/install.html

Page 42: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

42

Page 43: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

43

Page 44: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

44

simple prototyping

Page 45: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

45

simple prototyping

Page 46: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

46

scrolling back

Page 47: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

47

scrolling back

Page 48: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

48

leaving

Page 49: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

a few

Python basics

49

Page 50: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

the basic data

structures

50

Page 51: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

51

the basic data structures

• strings

• lists

• tuples

• dictionaries

Page 52: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

52

strings in Python

any sequence of characters enclosed by single or double-quotes

https://docs.python.org/3/tutorial/introduction.html#strings

Page 53: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

53

lists in Python

“comma-separated values (items)

between square brackets"https://docs.python.org/3/tutorial/introduction.html#lists

Page 54: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

54

tuples in Python

comma-separated values

https://docs.python.org/3/library/stdtypes.html#tuples

Page 55: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

lists vs tuples?

55

Page 56: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

56

lists vs tuples

the contents of a list

may be changed

the contents of a tuple

may not be changed

Page 57: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

57

quick caveat!

"...it is actually the comma

which makes a tuple,

not the parentheses."

https://docs.python.org/3/library/stdtypes.html#tuple

Page 58: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

dictionaries

(“hash tables”)

58

Page 59: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

59

dictionaries in Python

“an unordered set of key: value pairs” — keys must be unique

https://docs.python.org/3/tutorial/datastructures.html#dictionaries

Page 60: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

60

dictionaries in Python

66 is the key and ‘San Diego’ the value

https://docs.python.org/3/tutorial/datastructures.html#dictionaries

Page 61: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

61

dictionaries in Python

values are accessed using the

notation of [key]https://docs.python.org/3/tutorial/datastructures.html#dictionaries

Page 62: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

try it out!

62

Page 63: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

63

try it out!• create a variable with your neighbor's name

• create a list of your adjacent neighbor's names

• create a dictionary mapping those last names

to first names

Page 64: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

64

try it out!• create a variable with your neighbor's name

name = ''

• create a list of your adjacent neighbor's names

names = []

• create a dictionary mapping those last names

to first names

names = {'last':'first'}

Page 65: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

questions?

65

Page 66: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

66

Page 67: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

working with

modules

67

Page 68: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

what is a module?

68

Page 69: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

69

imports,

or makes available,

the “re” module

part of the standard libraryhttps://docs.python.org/3/reference/simple_stmts.html#import

Page 70: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

70

“re” is the

module’s

“namespace”

Page 71: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

71

“as” imports the module

under a different

“namespace”

Page 72: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

72

methods from a

module are accessed by

namespace.method()

Page 73: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

73

we will be using

re.match()

Page 74: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

74

two inputs — “a regex” and

“thing to match against”

using re.match()

Page 75: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

75

it returns —

None or a match object

using re.match()

Page 76: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

try it out!

76

Page 77: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

77

try it out!

• import the "re" module

• type help(re)

• type re.match('eth', 'ethernet')

Page 78: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

questions?

78

Page 79: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

79

Page 80: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

reading syslog

messages

80

Page 81: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

81

reading in a file

assigning the

file path to a variable

Page 82: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

82

reading in a file

opening a file handle

Page 83: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

83

reading in a file

using .readlines() to get a list of lines

Page 84: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

84

reading in a file

Page 85: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

85

try it out!

Page 86: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

86

try it out!

• open the file "syslog.txt"

• try out ".readlines()"

Page 87: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

questions?

87

Page 88: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

88

Page 89: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

parsing

syslog messages

89

Page 90: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

2015Apr214:25:06switch1%ETHPORT-5-IF_DOWN_INTERFACE_REMOVED:InterfaceEthernet5/1isdown(Interfaceremoved)

a syslog message

90

Page 91: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

2015Apr214:25:06switch1%ETHPORT-5-IF_DOWN_INTERFACE_REMOVED:InterfaceEthernet5/1isdown(Interfaceremoved)

91

looking with

an abstract lens

Page 92: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

DATESTAMP14:25:06switch1%ETHPORT-5-IF_DOWN_INTERFACE_REMOVED:InterfaceEthernet5/1isdown(Interfaceremoved)

92

looking with

an abstract lens

Page 93: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

DATESTAMP14:25:06switch1%ETHPORT-5-IF_DOWN_INTERFACE_REMOVED:InterfaceEthernet5/1isdown(Interfaceremoved)

93

looking with

an abstract lens

Page 94: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

DATESTAMPTIMESTAMPswitch1%ETHPORT-5-IF_DOWN_INTERFACE_REMOVED:InterfaceEthernet5/1isdown(Interfaceremoved)

94

looking with

an abstract lens

Page 95: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

DATESTAMPTIMESTAMPDEVICE%ETHPORT-5-IF_DOWN_INTERFACE_REMOVED:InterfaceEthernet5/1isdown(Interfaceremoved)

95

looking with

an abstract lens

Page 96: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

DATESTAMPTIMESTAMPDEVICE%ETHPORT-5-IF_DOWN_INTERFACE_REMOVED:InterfaceEthernet5/1isdown(Interfaceremoved)

96

looking with

an abstract lens

Page 97: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

DATESTAMPTIMESTAMPDEVICEERROR_CODE:InterfaceEthernet5/1isdown(Interfaceremoved)

97

looking with

an abstract lens

Page 98: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

DATESTAMPTIMESTAMPDEVICEERROR_CODE:InterfaceEthernet5/1isdown(Interfaceremoved)

98

looking with

an abstract lens

Page 99: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

DATESTAMPTIMESTAMPDEVICEERROR_CODE:ERROR_MESSAGE

99

looking with

an abstract lens

Page 100: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

100

from log message to

structured data

Page 101: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

101

structured dataD

AT

ES

TA

MP

TIM

ES

TA

MP

DE

VIC

E

ER

RO

R

CO

DE

ER

RO

R

ME

SS

AG

E

L O G M E S S A G E

Page 102: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

102

Page 103: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

regular

expressions!

103

Page 104: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

a way to ask —

does this pattern exist in a

given block of text?

https://docs.python.org/3/howto/regex.html104

Page 105: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

where

“this pattern” is

created by you

105

Page 106: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

specifying the pattern

is often called

“building a regex”

106

Page 107: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

ipas-pathaccess-list1permit"^$"

107

an example with BGP

Page 108: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

"^$"

108

beginning of line

followed byend of line

(an empty line!)

Page 109: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

getting started with

regular expressions

in Python

109

Page 110: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

110

try it out!

Page 111: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

111

try it out!

a raw string,

with an opening “grouping” symbol

Page 112: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

112

try it out!

\ to indicate that a

pattern-matching character follows

Page 113: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

113

try it out!

d to match one digit (integer)

http://www.merriam-webster.com/dictionary/digit

Page 114: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

114

try it out!

instead of pattern-matching,

we could put exact text too --

"2015" for example

Page 115: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

115

try it out!

+ to indicate one or more (of the previous thing)

Page 116: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

116

try it out!

\d+ will match 2015

Page 117: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

117

try it out!

\s will match one space

Page 118: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

118

try it out!

\w+ will match one or more

characters (Jun)

Page 119: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

119

try it out!

assigning the

return of re.match()

to a variable (matched)

Page 120: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

120

try it out!

a match object

was returned (it matched)

Page 121: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

121

try it out!

accessing the matched

data using .group()

Page 122: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

questions?

122

Page 123: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

123

Page 124: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

parsing syslog

124

Page 125: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

125

2015Apr214:25:06switch1%ETHPORT-5-IF_DOWN_INTERFACE_REMOVED:InterfaceEthernet5/1isdown(Interfaceremoved)

DATESTAMP_RE=r'(\d+\s+\w+\s+\d+)'

\d+oneormoredigits

Page 126: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

126

2015Apr214:25:06switch1%ETHPORT-5-IF_DOWN_INTERFACE_REMOVED:InterfaceEthernet5/1isdown(Interfaceremoved)

DATESTAMP_RE=r'(\d+\s+\w+\s+\d+)'

\s+oneormorespaces

Page 127: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

127

2015Apr214:25:06switch1%ETHPORT-5-IF_DOWN_INTERFACE_REMOVED:InterfaceEthernet5/1isdown(Interfaceremoved)

DATESTAMP_RE=r'(\d+\s+\w+\s+\d+)'

\w+oneormorecharacters

Page 128: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

128

2015Apr214:25:06switch1%ETHPORT-5-IF_DOWN_INTERFACE_REMOVED:InterfaceEthernet5/1isdown(Interfaceremoved)

DEVICE_NAME_RE=r'(\S+)'

\S+oneormorenon-spacecharacters

Page 129: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

129

2015Apr214:25:06switch1%ETHPORT-5-IF_DOWN_INTERFACE_REMOVED:InterfaceEthernet5/1isdown(Interfaceremoved)

ERROR_CODE_RE=r'%(\S+):'

\S+oneormorenon-spacecharacters

Page 130: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

130

2015Apr214:25:06switch1%ETHPORT-5-IF_DOWN_INTERFACE_REMOVED:InterfaceEthernet5/1isdown(Interfaceremoved)

ERROR_MESSAGE_RE=r'(\.*)'

\.*oneormorenon-newlinecharacters

Page 131: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

131

2015Apr214:25:06switch1%ETHPORT-5-IF_DOWN_INTERFACE_REMOVED:InterfaceEthernet5/1isdown(Interfaceremoved)

DATESTAMP_RE=r'(\d+\s+\w+\s+\d+)'TIMESTAMP_RE=r'(\d+:\d+:\d+)'DEVICE_NAME_RE=r'(\S+)'ERROR_CODE_RE=r'%(\S+):'ERROR_MESSAGE_RE=r'(\.*)'COLUMN_DELIMITER_RE=r'(\s+)'

Page 132: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

132

Page 133: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

133

using the “re” module to match

Page 134: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

134

using the “re” module to match

Page 135: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

135

using the “re” module to match

Page 136: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

136

using the “re” module to match

continue — skip this

log line and go to the nexthttps://docs.python.org/3/reference/simple_stmts.html#continue

Page 137: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

137

using the “re” module to match

Page 138: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

138

DATESTAMP_RE=r'(\d+\s+\w+\s+\d+)'TIMESTAMP_RE=r'(\d+:\d+:\d+)'DEVICE_NAME_RE=r'(\S+)'ERROR_CODE_RE=r'%(\S+):'ERROR_MESSAGE_RE=r'(\.*)'COLUMN_DELIMITER_RE=r'(\s+)'

start of group 1 end of group 1

Page 139: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

139

try it out!

Page 140: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

140

try it out!

Page 141: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

141

try it out!

note -

SYSLOG_RE is already staged

Page 142: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

142

try it out!

Page 143: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

143

try it out!

Page 144: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

144

try it out!

Page 145: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

145

try it out!

Page 146: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

questions?

146

Page 147: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

147

Page 148: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

building our first

remediation

148

Page 149: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

first remediation —

a linecard failure

149

Page 150: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

150

2015Apr214:25:06switch1%ETHPORT-5-

IF_DOWN_INTERFACE_REMOVED:Interface

Ethernet5/1isdown(Interfaceremoved)

Page 151: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

151

2015Apr214:25:06switch1%ETHPORT-5-IF_DOWN_INTERFACE_REMOVED:InterfaceEthernet5/1isdown(Interfaceremoved)

Page 152: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

152

2015Apr214:25:06switch1%ETHPORT-5-IF_DOWN_INTERFACE_REMOVED:InterfaceEthernet5/1isdown(Interfaceremoved)

Page 153: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

153

2015Apr214:25:06switch1%ETHPORT-5-IF_DOWN_INTERFACE_REMOVED:InterfaceEthernet5/1isdown(Interfaceremoved)

Page 154: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

154

2015Apr214:25:06switch1%ETHPORT-5-IF_DOWN_INTERFACE_REMOVED:InterfaceEthernet5/1isdown(Interfaceremoved)

Page 155: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

155

2015Apr214:25:06switch1%ETHPORT-5-IF_DOWN_INTERFACE_REMOVED:InterfaceEthernet5/1isdown(Interfaceremoved)

Did the interface regex find a match?

Page 156: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

156

2015Apr214:25:06switch1%ETHPORT-5-IF_DOWN_INTERFACE_REMOVED:InterfaceEthernet5/1isdown(Interfaceremoved)

Group "1" is the data matched by the first set of ( )

Page 157: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

157

2015Apr214:25:06switch1%ETHPORT-5-IF_DOWN_INTERFACE_REMOVED:InterfaceEthernet5/1isdown(Interfaceremoved)

Page 158: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

158

2015Apr214:25:06switch1%ETHPORT-5-IF_DOWN_INTERFACE_REMOVED:InterfaceEthernet5/1isdown(Interfaceremoved)

Page 159: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

try it out!

159

Page 160: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

160

try it out!

• parse out only the module number from:

Ethernet5/1

• populate that value into the string:

showmodule{module}

Page 161: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

sending

commands

over SSH

161

Page 162: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

162

sending commands over SSH

Page 163: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

163

a wrapper around Paramiko

(Python SSH client)

included with our demo code

sending commands over SSH

http://docs.paramiko.org/

Page 164: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

164

no paramiko?

http://docs.paramiko.org/

Page 165: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

installing paramiko

165

Page 166: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

installing paramiko

166

Page 167: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

167

sending commands over sshsending commands over ssh

Page 168: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

168

sending commands over sshsending commands over ssh

Page 169: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

169

sending commands over sshsending commands over ssh

Connection opened!

Page 170: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

170

sending commands over ssh

Page 171: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

171

sending commands over ssh

Page 172: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

172

sending commands over ssh

Page 173: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

try it out!

173

Page 174: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

174

try it out!

• open a dummy ssh connection using:

importssh_helper

ssh=ssh_helper.SSHSession('switch1')

• inspect the output from:

showmodule5

Page 175: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

175

inspecting the output

output is a list —

[0:5] is a slice of the first 5 lineshttps://docs.python.org/3/tutorial/introduction.html

Page 176: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

176

inspecting the output

Page 177: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

177

inspecting the output

checking if one string

is present in another string

Page 178: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

178

inspecting the output

ignoring case using .lower()

Page 179: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

179

inspecting the output

Page 180: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

180

inspecting the output

Page 181: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

181

reacting to the output

using on-device

filtering with egrep

Page 182: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

182

reacting to the output

Page 183: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

183

reacting to the output

idea! parse the uptime and react based on the value

Page 184: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

184

reacting to the output

Page 185: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

questions?

185

Page 186: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

186

Page 187: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

next remediation —

link flaps &

light level checking

187

Page 188: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

188

2015May1812:43:15switch2%ETHPORT-5-

IF_DOWN_LINK_FAILURE:Interface

Ethernet1/4isdown(Linkfailure)

Page 189: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

189

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

Page 190: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

190

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

Page 191: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

191

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

Page 192: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

192

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

Page 193: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

193

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

Page 194: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

194

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

Page 195: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

195

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

Page 196: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

196

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

Page 197: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

197

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

Page 198: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

198

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

Page 199: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

199

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

Page 200: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

200

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

Page 201: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

201

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

Page 202: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

202

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

Page 203: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

203

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

Page 204: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

204

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

Page 205: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

205

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

Page 206: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

206

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

Page 207: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

207

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

Page 208: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

208

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

Page 209: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

209

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

Page 210: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

210

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

"show transceiver details"

Page 211: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

211

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

exampleoutput:'RxPower-8.84dBm....'

Page 212: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

212

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

exampleoutput:'RxPower-8.84dBm....'

Page 213: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

213

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

exampleoutput:'RxPower-8.84dBm....'

Page 214: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

214

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

exampleoutput:'RxPower-8.84dBm....'

Page 215: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

215

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

exampleoutput:'RxPower-8.84dBm....'

Page 216: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

216

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

exampleoutput:'RxPower-8.84dBm....'

Page 217: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

217

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

converting a string to a float for mathematical comparison

Page 218: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

218

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

Page 219: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

219

2015May1812:43:15switch2%ETHPORT-5-IF_DOWN_LINK_FAILURE:InterfaceEthernet1/4isdown(Linkfailure)

send_email()

Page 220: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

questions?

220

Page 221: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

keeping track of

what happened

221

Page 222: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

222

options

• in memory (lists, dictionaries, etc)

• on disk (as a file)

• in a database

Page 223: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

223

comparing a few database options

• sqlite

• mysql

Page 224: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

224

an event as a database table

E V E N T

ID

DA

TE

ST

AM

P

DE

VIC

E

ER

RO

R

CO

DE

ER

RO

R

ME

SS

AG

E

RE

SU

LT

Page 225: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

225

getting started with sqlite

Page 226: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

226

building the database

Page 227: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

227

building the database

Page 228: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

228

creating events

the safe way to handle string substitution

Page 229: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

229

creating events

auto-generated by the database

Page 230: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

230

updating events

Page 231: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

231

updating events

Page 232: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

questions?

232

Page 233: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

opportunities for

improvement

233

Page 234: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

234

scoping

• globally scoped code is a bad design

• use functions, classes, and modules

to keep things organized

Page 235: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

235

scoping

• globally scoped code and code inside large

functions are both hard to test

• concise functions are easier to test

• testability saves much pain and suffering

later

Page 236: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

236

organization

• remediation and system code together

- it can be quite a lot to digest.

• "sparse is better than dense" - use

smaller concise files and split logically.

Page 237: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

237

scaling

• it's not a real-time system, but would

need to be.

• for log parsing, continuous parsing with

some intelligence re: new vs old events.

Page 238: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

238

stability

• persistence is key!

• a restart to the parsing script

should not re-run previously

handled events / remediations.

Page 239: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

239

perf

• one by one processing is okay for a demo...

• in real life though, this should break up

the load within the script, or as separate

scripts/instances.

Page 240: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

240

device

syslog server

sendsmessage

parser

tails messages

1

23

remediationruns

remediates4

system design

Page 241: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

241

device

syslog server

sendsmessage

parser

tails messages

1

23

remediationruns

remediates4

system design

Page 242: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

appendix

242

Page 243: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

separating backend

code from

remediation logic

243

Page 244: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

244

separating the backend

from remediations

a dictionary where the

values are functions!

Page 245: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

245

separating the backend

from remediations

Page 246: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

246

separating the backend

from remediations

a function

Page 247: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

247

running a function

who’s name is not defined in this block of code

Page 248: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

248

Page 249: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

249

Page 250: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

250

Page 251: NetOps Coding 101 - NANOG Archive · NetOps Coding 101 …building your first robot! NANOG 66 02.09.2016 david swafford. what we’ll accomplish 2. automating 3. the remediation

NANOG 66

02.09.2016

david swafford

#netengcode