Automating IP networks with
Python
Tomás Lynch
Vultr, LLC.
September 2019 - LACNOG2019
What would you choose?
Life with or without automation?
LACNOC2019 - Automation - Lynch 2
Actually...
Life without automation Life with automation
LACNOC2019 - Automation - Lynch 3
Standardization before automation
Automation 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.co
vs.
diego10-router, pele.br, co7ar0-edge
LACNOC2019 - Automation - Lynch 4
Automation and
Python
LACNOC2019 - Automation - Lynch 5
Python network element packages
ncclient
● 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
LACNOC2019 - Automation - Lynch 6
show lldp neighbors performance
PyEZ - predefined table netmiko - CLI
dev = Device( device = {
host=router, 'device_type': 'brocade',
user=uname, 'ip': router,
connect
password=pw 'username': uname,
) 'password': pw,
'port': port,
'secret': enablepass
}
dev.open() ssh_connect=Netmiko(**device)
ssh_connect.enable()
command
ssh_connect.send_command('skip-page-display')
router_lldp = LLDPNeighborTable(dev) lldp_neighbors =
lldp_neighbors = router_lldp.get() ssh_connect.send_command('show lldp neighbors
detail')
dis.
dev.close() ssh_connect.disconnect()
LACNOC2019 - Automation - Lynch 7
Package performance
PyEZ netmiko (ssh)
Predefined operational table Command-line interface
10 routers 10 routers
15 seconds 1 minute 38 seconds
Output: lldp_neighbors Output: lldp_neighbors
Dictionary Plain text
Ready to use! More processing
LACNOC2019 - Automation - Lynch 8
Automation and
Python in use
LACNOC2019 - Automation - Lynch 9
The network
Internet
16 locations
1600 network elements
Edge Edge
router router
1 2
Automation using puppet, python, etc.
Distribution 1 Distribution n
TOR 1 TOR 2 TOR 3 TOR m
VMs VMs VMs VMs
LACNOC2019 - Automation - Lynch 10
Example 1: update_bgp_peer
13 Public Peering Exchange Points
17 Private Peering Facilities
1100 peers aprox.
LACNOC2019 - Automation - Lynch 11
Example 2: interface_description
LACNOC2019 - Automation - Lynch 12
Conclusions,
recommendations,
and references
LACNOC2019 - Automation - Lynch 13
Conclusions
Standardization 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”
LACNOC2019 - Automation - Lynch 14
Recommendations
Do 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!
LACNOC2019 - Automation - Lynch 15
References
Python 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
LACNOC2019 - Automation - Lynch 16
Thank you!
Tomas Lynch
tlynch [at] vultr (dot) com
Backup slides
Tomas Lynch
[email protected]
Automation
LACNOC2019 - Automation - Lynch 19
What is network automation?
Process of automating:
configuration,
management,
testing,
deployment, and
operations
Also called network programmability
LACNOC2019 - Automation - Lynch 20
Automation block diagram
Variables Script API Infrastructure
Device
name REST Router
ASN XML Switch
IP address JSON Server
Description NETCONF Etc.
Etc.
LACNOC2019 - Automation - Lynch 21
Also monitoring?
Variables
Script API Infrastructure
LACNOC2019 - Automation - Lynch 22
If it helps to make automated decisions
Variables
Script
API Infrastructure
Script
LACNOC2019 - Automation - Lynch 23
Standardization
LACNOC2019 - Automation - Lynch 24
Configuration standardization
Automation 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á
LACNOC2019 - Automation - Lynch 25
Software version standardization
junos.version_info(major=(15, 1) junos.version_info(major=(18, 4)
{ {
'community': [{ 'community': [{
'name': { 'name': 'EXAMPLE_COMM',
'data': 'EXAMPLE_COMM' 'members': ['65536:1']
}, }]
'members': [{ }
'data': '65536:1'
}]
}]
}
LACNOC2019 - Automation - Lynch 26
PyEZ warning
LACNOC2019 - Automation - Lynch 27
Script
dev = 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))
LACNOC2019 - Automation - Lynch 28
Output
Unable to load configuration changes:
ConfigLoadError(severity: error, bad_element: interface,
message: error: syntax error)
LACNOC2019 - Automation - Lynch 29
The problem?
set interface != set interfaces
LACNOC2019 - Automation - Lynch 30
Corrected script
dev = 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))
LACNOC2019 - Automation - Lynch 31