SANOG35 Tutorial Programming and Python For Network Engineers PDF
SANOG35 Tutorial Programming and Python For Network Engineers PDF
SANOG35 Tutorial Programming and Python For Network Engineers PDF
Network Engineer
2
Tedious Networking Operations
3
Network Operational Problems
• Infrastructure scaling
4
Evolutional Drift to DevOps
5
Programming to the Rescue
6
Why learn Programming
• Programming languages are not just for programmers. If you are a Logic
Building
network engineer, knowing a programming language (or two or
three) can come in handy.
Problem
Productivity
Solving
Programming
7
Programming in Networks
• Network Automation.
8
Orchestration vs Automation
• Automation
• Well specified task run on its own
• Orchestration
• Automating a lot of things at once
• Multiple tasks to execute a workflow
• Is automation, coordination and management
9
Orchestration vs Automation
Scenario – Network Engineer needs to configure Customer link
Each individual tasks can be automated, and the whole process is orchestrated
10
“Automation” A Smart Way Out !
• Why Automation? - Programmed management tools are designed to reduce the complexity of manually
configuring and managing distributed infrastructure resources by enabling speed, ensuring reliability and
compliance
• Using different programming languages provide different paths to achieve a common goal of managing
infrastructure efficiently
11
Languages For Automation
Ansible •Python
• Puppet, Chef and Ansible are different paths to achieve a common
goal of managing infrastructure efficiently.
CHEF •Ruby
12
Which Programming Language should we learn
User Technical
Easy Size of What People Market Which
Friendly Online
Learning Community are available demands ! Language ?
Resources
13
Type of Languages
FRONTEND BACKEND
• The digital interaction is accomplished by using HTML, CSS and
JavaScript.
HTML Bash
PHP
• Backend of an application is responsible for things like calculations, Laravel
logic, database interactions, and performance. Perl
Java Script
Ruby
• If we talk about networks, it is used to interact with Nodes and Django Python
perform actions such as run commands, gather stats, perform
configurational changes and view output.
14
What is Python
• “Batteries Included”
• a standard distribution includes many modules
• Dynamic typed
Object
• Dominating language at this point of time in Network oriented
Automation space.
Platform
Powerful
independent
• A high level language, don’t have to write a lot of blue codes
to get things done.
PYTHON
17
Why Python
• Interpreted
• You can just copy over your code to another system and it will auto-magically work! *with python
platform
• Object-Oriented
• Extensive libraries
• (i.e. reg. expressions, doc generation, CGI, ftp, web browsers, ZIP, WAV, cryptography, etc...)
(wxPython, Twisted, Python Imaging library)
Why Python
• Simple
• Its Pseudo-code nature allows one to concentrate on the problem rather than the language
• Easy to Learn
• Microsoft Windows
• Apple Mac OS
• Linux Distribution OS
Python development environment is not included in Windows default setup. For Windows 10, a small patch
does the trick where as for earlier versions proper setup is required. Coding could be done on CMD window.
In Mac OS X, Python 2 and its built-in Interactive Development Environment (IDE) comes pre-installed.
Everything could be done in Terminal that includes creating the code and its execution.
Famous Linux Distributions also have Python enabled on default setup. Just as Mac OS, a user can start coding
and execute it in Linux terminal window using CLI.
22
Installing Python
• Linux
$sudo apt-get install python3-minimal
• Windows
Download 64 bit python
• Python on default
download is 32 bit hence
we have to download a
specific release and
version from python
download release
section.
• Hence chose your
respective python version
as per your requirements.
24
What is PIP
• You use PIP to install and update packages and modules as per required by your python code.
• If you do not have PIP installed, you can download and install it from,
• https://pypi.org/project/pip/
Download Package
• Open the command line interface and tell PIP to download the package you want.
• Make sure to navigate your command line to the location of Python's script directory, and type the
following:
• Python Shell – running 'python' from the Command Line opens this interactive shell
• Spyder- It is a scientific integrated development environment written in Python, available through Anaconda
• PyCharm - a cross-platform IDE, that can be used on Windows, macOS, and Linux.
• Sublime Text 3 - Sublime Text 3 is a code editor which supports many languages including Python
• Visual Studio Code - The editor provides smart code completion based on function definition, imported modules, as well as variable types
• PythonWin – a Windows only interface to Python, It provides a simple graphical interface for editing and running Python programs
• Jupyter- It supports for Numerical simulation, data cleaning machine learning data visualization, and statistical modelling.
• For the exercises, we'll use Spyder, but you can try them all and pick a favorite
IDLE – Integrated Development Environment
• debugging
• auto-indent
• interactive shell
Python Programming Modes
1. Interpreter Mode
Interactive mode is a command line shell which gives immediate feedback for each statement
The “>>>” indicates that the shell is ready to accept interactive commands
Python Programming Modes
Normal mode is the mode where the scripted (.py) files are run in
the Python interpreter.
Python programs are nothing more than text files, and they may be edited
with a standard text editor program. For example Vim, Nano,
Notepad++,Sublime Text
Instead of having to run one line or block of code at a time, you can type up
all your code in one text file, or script, and run all the code at once.
To run the script, either select “Run” -> “Run Module” or press F5.
One more way of execute the python (.py) file from Linux distribution is
31
Python Shell
32
Python Jupyter
33
Python Spyder
34
Installing an IDE
35
Online Python IDE
• https://repl.it/
36
Deeper into Python
Deeper into Python
Variable :
Variables are reserved memory locations to store values.
Numbers | Strings | List | Tuple | Dictionary
Operators :
Operators are the constructs which can manipulate the value of operands.
• Arithmetic Operators
• Comparison (Relational) Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
Deeper into Python
Control Flow Tools :
These are the tools which control the logical flow of the program based on the following parameters.
Types:
• if statement
• while statement
• for statement
• break, continue and pass
Deeper into Python
Iteration:
Computers are often used to automate repetitive tasks. Repeated execution of a set of statements is called
iteration.
Conditionals:
In python conditional statements are features which perform different computations or actions depending
on whether a programmer specified boolean condition evaluates to true or false
Regular Expressions:
A regular expression is a special sequence of characters that helps you match or find other strings or sets of
strings, using a specialized syntax held in a pattern
40
Deeper into Python
Function:
A function is a block of organized, reusable code that is used to perform a single or multiple related action
Classes:
Python uses classes to keep related things together.
Modules:
A Python module is a Python source file which can expose classes, functions and global variables. A Python
package is a directory of Python module
41
Python semantics
• Each statement has its own semantics, the def statement doesn’t get executed immediately like other
statements
print x
x=1
print x
x=x+1 x=x+1
print x
print x
Conditional Steps
x=5
Yes
X < 10 ?
When a program is running, it flows from
print 'Smaller' one step to the next step based on the
condition designed.
Yes
X > 20 ?
print 'Bigger'
print 'Finis'
Repeated Steps
n=5
Loops (repeated steps) have iteration variables that change
each time through a loop. Often these iteration variables go
No Yes through a sequence of numbers.
n>0?
Program:
print n
n=5
while n > 0 :
n = n -1 print n
n=n–1
print 'Blastoff!'
print 'Blastoff'
Variables Types
• Variables
• Constants
Variables
A variable is a named place in the memory where a programmer can store data and later retrieve the data
using the variable “name”
x = 12.2
y = 14
Constants
Fixed values such as numbers, letters, and strings are called “constants” - because their value does
not change
In C/C++:
In Python:
int a;
a=5
float b;
a = “Hello”
a=5
a = [ 5, 2, 1]
b = 0.43
Some Data Types in Python
• Tuple (Example: ( 4, 2, 7, 3) )
String Data Type
b a n a n a
0 1 2 3 4 5
• We can get at any single character in a string using an index
specified in square brackets
M o n t y P y t h o n
0 1 2 3 4 5 6 7 8 9 10 11
M o n t y P y t h o n
0 1 2 3 4 5 6 7 8 9 10 11
>>> a = 'Hello‘
>>> b = a + 'There‘
• When the + operator is applied to >>> print b
strings, it means "concatenation" HelloThere
• We prefer to read data in using strings and then >>> name = raw_input('Enter:')
parse and convert the data as we need Enter:Chuck
>>> print name
Chuck
• This gives us more control over error situations >>> apple = raw_input('Enter:')
and/or bad user input Enter:100
>>> x = apple – 10
Traceback (most recent call last): File "<stdin>", line 1, in
• Raw input numbers must be converted from <module>TypeError: unsupported operand type(s) for -: 'str' and 'int‘
strings >>> x = int(apple) – 10
>>> print x
90
Casting
• Int()
• Float()
• Str()
Python Collections (Arrays)
• There are four collection data types in the Python programming language:
• List is a collection which is ordered and changeable. Allows duplicate members.
• Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
• Set is a collection which is unordered and unindexed. No duplicate members.
• Dictionary is a collection which is unordered, changeable and indexed. No duplicate members.
• When choosing a collection type, it is useful to understand the properties of that type. Choosing the
right type for a particular data set could mean retention of meaning, and, it could mean an increase in
efficiency or security.
59
Lists
• A list is a collection which is ordered and changeable. In Python lists are written with square brackets.
• You access the list items by referring to the index number.
• You can specify a range of indexes by specifying where to start and where to end the range.
• When specifying a range, the return value will be a new list with the specified items.
60
Tuples
• A tuple is a collection which is ordered and unchangeable. In Python tuples are written with round
brackets.
• You can access tuple items by referring to the index number, inside square brackets.
• Negative indexing means beginning from the end.
• You can specify a range of indexes by specifying where to start and where to end the range.
• When specifying a range, the return value will be a new tuple with the specified items
61
Sets
• A set is a collection which is unordered and unindexed. In Python sets are written with curly brackets.
• You cannot access items in a set by referring to an index, since sets are unordered the items has no
index.
• But you can loop through the set items
• Once a set is created, you cannot change its items, but you can add new items.
62
Dictionary
• A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are
written with curly brackets, and they have keys and values.
• You can access the items of a dictionary by referring to its key name, inside square brackets.
• You can change the value of a specific item by referring to its key name.
• You can loop through a dictionary.
• When looping through a dictionary, the return value are the keys of the dictionary, but there are
methods to return the values as well
63
Operators
• Arithmetic Operators
• Assignment Operators
• Logical Operators
• Membership Operators
Arithmetic Operators
+ Addition Adds the values on either side of the operator and calculate a result.
- Subtraction Subtracts values of right side operand from left side operand.
+= Add AND Adds right side operand value to the left side operand
value and assigns the results to the left operand.
-= Subtract AND Subtracts right side operand value to the left side operand
value and assigns the results to the left operand.
*= Multiply AND Similarly does their respective operations and assigns the
operator value to the left operand.
Logical Operators
and Logical AND If both the operands are true, then the condition is true.
not Logical NOT It is used to reverse the logical state of its operand.
Membership Operators
• In
• The result of this operation becomes True if it finds a value in a specified sequence
and False otherwise.
• Not in
• The result of this operation becomes True if it doesn't find a value in a specified sequence
and False otherwise.
Decision making
• if Statement
• if else Statements
• elif Statements
Decision making
Decision making
Loops
• For
• While
• Nested for
Looping with For
• We could use a for loop to perform geoprocessing tasks on each layer in a list
• We could get a list of features in a feature class and loop over each, checking attributes
• For allows you to loop over a block of code a set number of times
• Built-in functions that are provided as part of Python - raw_input(), type(), float(), max(), min(),
int(), str(), …
• We treat the built-in function names like reserved words (i.e. we avoid them as variable names)
Definition of Function
• In Python a function is some reusable code that takes arguments(s) as input does some
computation and then returns a result or results
• We call/invoke the function by using the function name, parenthesis and arguments in an expression
Functions
• Code reuse
• Routines can be called multiple times within a program
• Routines can be used from multiple programs
• Namespace partitioning
• Group data together with functions used for that data
• instance -- an object created from the template (an instance of the class)
• method -- a function that is part of the object and acts on instances directly
class Thing:
"""Initialize a Thing."""
constructor
self.value = value
def showme(self):
method
w Opens a file for writing. (If a file doesn't exist already, then it creates a new file. Otherwise, it's truncate a file.)
x Opens a file for exclusive creation. (Operation fails if a file does not exist in the location.)
a Opens a file for appending at the end of the file without truncating it. (Creates a new file if it does not exist in the
location.)
• Python has a module named “time” to handle time-related tasks. To use functions defined in the module,
we need to import the module first.
• Time function returns the number of seconds passed since epoch
• Ctime function takes seconds passed since epoch as an argument and returns a string representing local
time
• Sleep function suspends (delays) execution of the current thread for the given number of seconds.
95
Types of errors
96
Exception handling
• try/except
• catch the error and recover from exceptions hoist by programmers or Python itself.
• try/finally
• Whether exception occurs or not, it automatically performs the clean-up action.
• Assert
• triggers an exception conditionally in the code.
Comments
• Single-Line Comment
• Multi-Line Comment
Indentation Rules
• Maintain indent to indicate the scope of the block (which lines are affected by the if/for)
• Reduce indent to back to the level of the if statement or for statement to indicate the end of the
block
• Lines within the code block are indented at the same level
• You'll want blocks of code that run only when certain conditions are met
Indentation
Whitespace
• Colons start of a new block in many constructs, e.g. function definitions, then clauses
Naming Rules
Accessing a name before it’s been properly created (by placing it on the left side of an assignment),
raises an error
>>> y
and assert in
del else raise
from if continue
not pass finally
while yield is
as break return
elif except def
global import for
or print lambda
with class try
exec
String Methods
Method Description
lstrip() Returns a left trim version of the string
replace() Returns a string where a specified value is replaced with a specified value
rfind() Searches the string for a specified value and returns the last position of where it was found
rindex() Searches the string for a specified value and returns the last position of where it was found
rsplit() Splits the string at the specified separator, and returns a list
rstrip() Returns a right trim version of the string
split() Splits the string at the specified separator, and returns a list
splitlines() Splits the string at line breaks and returns a list
startswith() Returns true if the string starts with the specified value
strip() Returns a trimmed version of the string
swapcase() Swaps cases, lower case becomes upper case and vice versa
title() Converts the first character of each word to upper case
upper() Converts a string into upper case
zfill() Fills the string with a specified number of 0 values at the beginning
108
Method Description
capitalize() Converts the first character to upper case
count() Returns the number of times a specified value occurs in a string
encode() Returns an encoded version of the string
endswith() Returns true if the string ends with the specified value
find() Searches the string for a specified value and returns the position of where it was found
format() Formats specified values in a string
index() Searches the string for a specified value and returns the position of where it was found
isalnum() Returns True if all characters in the string are alphanumeric
isalpha() Returns True if all characters in the string are in the alphabet
isdecimal() Returns True if all characters in the string are decimals
isdigit() Returns True if all characters in the string are digits
islower() Returns True if all characters in the string are lower case
isnumeric() Returns True if all characters in the string are numeric
isspace() Returns True if all characters in the string are whitespaces
istitle() Returns True if the string follows the rules of a title
isupper() Returns True if all characters in the string are upper case
join() Joins the elements of an iterable to the end of the string
lower() Converts a string into lower case 109
List Methods
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
110
Sets Methods
Method Description
add() Adds an element to the set
clear() Removes all the elements from the set
copy() Returns a copy of the set
difference() Returns a set containing the difference between two or more sets
difference_update() Removes the items in this set that are also included in another, specified set
discard() Remove the specified item
pop() Removes an element from the set
remove() Removes the specified element
symmetric_difference() Returns a set with the symmetric differences of two sets
symmetric_difference_update() inserts the symmetric differences from this set and another
union() Return a set containing the union of sets
update() Update the set with the union of this set and others
111
Network Integration with Python
• Telnet
• Netmiko
• NAPALM
112
GNS3 DEMO
113
GNS3 DEMO
• Download gns3
• https://www.gns3.com/
• Download the GNS3 VM
• Edit/Preference/GNS3VM/download here
114
GNS3 DEMO
• Use VMware workstation to import the GNS3VM
• Download the GNS3 appliance
• https://www.gns3.com/marketplace/appliance/network-automation
115
GNS3 DEMO
• Import the appliance
• File/import appliance
116
GNS3 DEMO
117
Telnet
118
Telnet
In Python telnet is implemented by the module telnetlib which has the Telnet class which has the required
methods to establish the connection
import telnetlib
import time
password = ("cisco")
tn = telnetlib.Telnet("192.168.1.10")
tn.read_until("Password: ")
tn.write(password + "\n")
root@NetworkAutomation:~# python telnet.py
tn.write("enable \n")
SW1#conf t
tn.read_until("Password: ")
Enter configuration commands, one per line. End with CNTL/Z.
tn.write(password + "\n")
SW1(config)#interface loopback100
SW1(config-if)#ip address 100.1.1.1 255.255.255.0
tn.write("conf t \n")
SW1(config-if)#end
time.sleep(1)
SW1#
tn.write("interface loopback10 \n")
time.sleep(1)
tn.write("ip address 10.1.1.1 255.255.255.0 \n")
time.sleep(1)
tn.write("end \n")
time.sleep(1)
tn.write("exit \n")
print tn.read_very_eager()
print("\nThank You")
119
Telnet
import telnetlib
import time root@NetworkAutomation:~# python telnet_loop.py
120
Netmiko
121
Netmiko
• Simplify the execution of show commands and the retrieval of output data
122
Netmiko
root@NetworkAutomation:~# python netmiko1.py
Interface IP-Address OK? Method Status Protocol
from netmiko import ConnectHandler Ethernet0/0 unassigned YES unset up up
Ethernet0/1 unassigned YES unset up up
ios = { Ethernet0/2 unassigned YES unset up up
Ethernet0/3 unassigned YES unset up up
'device_type': 'cisco_ios', Ethernet1/0 unassigned YES unset up up
'ip': '192.168.1.10', Ethernet1/1 unassigned YES unset up up
'username': 'cisco', Ethernet1/2 unassigned YES unset up up
Ethernet1/3 unassigned YES unset up up
'password': 'cisco' Ethernet2/0 unassigned YES unset up up
} Ethernet2/1 unassigned YES unset up up
Ethernet2/2 unassigned YES unset up up
Ethernet2/3 unassigned YES unset up up
net_connect = ConnectHandler(**ios) Ethernet3/0 unassigned YES unset up up
output = net_connect.send_command('show ip int brief') Ethernet3/1 unassigned YES unset up up
print (output) Ethernet3/2 unassigned YES unset up up
Ethernet3/3 unassigned YES unset up up
Loopback0 1.1.1.1 YES NVRAM up up
config_commands = ['int loop 0', 'ip address 1.1.1.1 255.255.255.0'] Vlan1 unassigned YES unset administratively down down
output = net_connect.send_config_set(config_commands) Vlan10 192.168.1.10 YES NVRAM up up
config term
print (output) Enter configuration commands, one per line. End with CNTL/Z.
IOU1(config)#int loop 0
IOU1(config-if)#ip address 1.1.1.1 255.255.255.0
IOU1(config-if)#end
123
NAPALM
124
NAPALM
NAPALM (Network Automation and Programmability Abstraction Layer with Multivendor support) is a
Python library that implements a set of functions to interact with different network device Operating
Systems using a unified API.
NAPALM supports several methods to connect to the devices, to manipulate configurations or to retrieve
data.
•Arista EOS
•Cisco IOS
•Cisco IOS-XR
•Cisco NX-OS
•Juniper JunOS
https://napalm.readthedocs.io/en/latest/index.html
125
NAPALM
126
NAPALM
127
NAPALM
python napalm1.py
from napalm import get_network_driver
{u'os_version': u'Solaris Software (I86BI_LINUXL2-ADVENTERPRISEK9-M),
driver = get_network_driver('ios') Experimental Version 15.1(20130726:213425) [dstivers-j
uly26-2013-team_track 104]',
ios = driver('192.168.1.10', 'cisco', 'cisco') u'uptime': 2640,
u'interface_list': [u'Ethernet0/0', u'Ethernet0/1', u'Ethernet0/2', u'Ethernet
0/3', u'Ethernet1/0', u'Ethernet1/1', u'Ethernet1/2', u'Ethernet1/3',
ios.open() u'Ethernet2/0', u'Ethernet2/1', u'Ethernet2/2', u'Ethe
rnet2/3', u'Ethernet3/0', u'Ethernet3/1', u'Ethernet3/2', u'Ethernet3/3',
ios_output = ios.get_facts() u'Loopback0',
u'Vlan1', u'Vlan10'],
print (ios_output) u'vendor': u'Cisco',
u'serial_number': u'2048001', u'model': u'Unknown', u'hostname': u'IOU1',
u'fqdn': u'IOU1.cisco.com'}
128
Use Case
Case I :
Configure Vlan on multiple switches and interfaces simultaneously.
129
Thinking Process !
• Problem identification
• Configure Vlan on multiple switches and its interfaces simultaneously.
• Error handling.
• Time mismanagement.
• Implementing a Plan
• Automate vlan creation.
• Use programming language
• Debugging issues
• Determine format of output
• Integration of frontend and backend code.
• Solution
• No more manual configuration
• Error less provisioning
• Efficient utilization of time
• Future Enhancements
• Multi vendor support
130
Frontend HTML Page
131
Frontend HTML Page
132
Backend Python Code
133
Debug session on Network Node
134
What happened when vlan already exists !
135
Enter incorrect vlan !
136
Python for Network Engineers
• The language which is widely opt by Network community and there is a very big gap between
Python and other prevalent languages such as Perl, Ruby, Go as far as the size of the
community.
• For new comers make simple scripts then move to complicated ones.
• Use various Python tools/modules to handle network devices from several networking vendors:
Cisco (IOS, IOS XE, IOS XR), Juniper, Arista, HP, Avaya.
137
Closing Remarks
• If you are really interested in moving forward, try making your mind and picking up a choice.
• Start small, celebrate the little things, and build, build, build!
• At the end of the day the true reason for happiness is creating something out of nothing.
A good mentor of mine told me if you want to create a flying car, then start with making some
wheels into a skateboard, enjoy the skateboard and turn that into a bike and so on. – Yad Faeq
Questions ! 13
8
Thank You
You will never be 100% ready to change. Don’t wait for the perfect time. It will never come.
Start today ! 139