31
Automating IP networks with Python Tomás Lynch Vultr, LLC. September 2019 - LACNOG2019

September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

  • Upload
    others

  • View
    37

  • Download
    0

Embed Size (px)

Citation preview

Page 1: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

Automating IP networks with PythonTomás Lynch

Vultr, LLC.

September 2019 - LACNOG2019

Page 2: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

What would you choose?

Life with or without automation?2

Page 3: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

Actually...

Life without automation Life with automation3

Page 4: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

Standardization before automationAutomation is useless without standardized configuration

Naming convention, same OS version, etc. are automation fundamental pieces

Automation relies on regular expressions

Example: add a prefix list to all edge routers:

router.edge1.ar, router.edge1.br, router.edge1.covs.

diego10-router, pele.br, co7ar0-edge

4

Page 5: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

Automation and Python

5

Page 6: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

Python network element packagesncclient● Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP● netconf only

PyEZ● Juniper

netmiko● Arista, Cisco IOS, Juniper, MikroTik, among others

And 6,594 projects more

6

Page 7: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

show lldp neighbors performancedi

s.co

mm

and

conn

ect

device = { 'device_type': 'brocade', 'ip': router, 'username': uname, 'password': pw, 'port': port, 'secret': enablepass}

ssh_connect=Netmiko(**device)ssh_connect.enable()ssh_connect.send_command('skip-page-display')

lldp_neighbors = ssh_connect.send_command('show lldp neighbors detail')

ssh_connect.disconnect()

dev = Device(host=router, user=uname, password=pw

)

dev.open()

router_lldp = LLDPNeighborTable(dev)lldp_neighbors = router_lldp.get()

dev.close()

PyEZ - predefined table netmiko - CLI

7

Page 8: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

Package performancePyEZ

Predefined operational table

10 routers

15 seconds

Output: lldp_neighbors

Dictionary

Ready to use!

netmiko (ssh)

Command-line interface

10 routers

1 minute 38 seconds

Output: lldp_neighbors

Plain text

More processing

8

Page 9: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

Automation and Python in use

9

Page 10: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

The network16 locations

1600 network elements

Automation using puppet, python, etc.

Edge router

1

Edge router

2

Distribution 1 Distribution n

TOR 1 TOR 2 TOR 3 TOR m

VMs VMs VMs VMs

Internet

10

Page 11: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

Example 1: update_bgp_peer

13 Public Peering Exchange Points17 Private Peering Facilities1100 peers aprox.

11

Page 12: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

Example 2: interface_description

12

Page 13: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

Conclusions, recommendations, and references

13

Page 14: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

ConclusionsStandardization is the most important step before automation

Automate repetitive and boring tasks

Peering information, standards verification, massive changes, etc.

Use complete commands: “show running-config” instead of “sh ru”

14

Page 15: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

RecommendationsDo not spend time in once in a lifetime scripts

Use your old friends: grep, awk, etc.

If no experience: start with non-disrupting commands

Use vendor specific packages if possible

Do not store passwords in scripts!

15

Page 16: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

ReferencesPython Package Index – pypi.org

Network automation – juni.pr/2YVgjVj

netmiko platforms – bit.ly/2Tf6Oeo

Clos architecture – RFC7938

PyEZ – juni.pr/2YSmf1g

BGP summary using PyEZ – www.inetzero.com/pyez

16

Page 17: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

Thank you!Tomas Lynch

tlynch [at] vultr (dot) com

Page 18: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

Backup slidesTomas Lynch

[email protected]

Page 19: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

Automation

19

Page 20: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

What is network automation?Process of automating:

configuration,

management,

testing,

deployment, and

operations

Also called network programmability20

Page 21: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

Automation block diagram

Script API InfrastructureVariables

Device nameASN

IP addressDescription

Etc.

RESTXML

JSONNETCONF

RouterSwitchServer

Etc.

21

Page 22: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

Also monitoring?

Script API Infrastructure

Variables

22

Page 23: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

If it helps to make automated decisions

Script

API Infrastructure

Variables

Script

23

Page 24: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

Standardization

24

Page 25: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

Configuration standardizationAutomation is useless without a configuration standard or naming convention

Automation relies on regular expressions:

^TRANSIT.* = all transit interfaces

.*PRIV_PEER = all private peers

.*(PUB|PRIV)_PEER = all peers

router.cisco.*\.pa = Cisco routers in Panamá

25

Page 26: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

Software version standardizationjunos.version_info(major=(15, 1)

{'community': [{

'name': {'data': 'EXAMPLE_COMM'

}, 'members': [{

'data': '65536:1'}]

}]}

junos.version_info(major=(18, 4)

{ 'community': [{

'name': 'EXAMPLE_COMM', 'members': ['65536:1']}]

}

26

Page 27: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

PyEZ warning

27

Page 28: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

Scriptdev = Device(host=router, user=username, password=password)dev.open()

cli = Config(dev, mode='private')

command = 'set interface et-0/0/0 description "A nice description"'

try: cli.load(command, format='set')except (ConfigLoadError, Exception) as err: print ("Unable to load configuration changes: {0}".format(err))

28

Page 29: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

OutputUnable to load configuration changes: ConfigLoadError(severity: error, bad_element: interface, message: error: syntax error)

29

Page 30: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

The problem?

set interface != set interfaces

30

Page 31: September 2019 - LACNOG2019 Python · Juniper, Cisco CSR, Cisco Nexus, Huawei, Alcatel Lucent, H3C, HP netconf only PyEZ ... PyEZ - predefined table netmiko - CLI 7. LACNOC2019 -

LACNOC2019 - Automation - Lynch

Corrected scriptdev = Device(host=router, user=username, password=password)dev.open()

cli = Config(dev, mode='private')

command = 'set interfaces et-0/0/0 description "A nice description"'

try: cli.load(command, format='set')except (ConfigLoadError, Exception) as err: print ("Unable to load configuration changes: {0}".format(err))

31