21 Network Programmability and Automation
21 Network Programmability and Automation
Automation
age 2 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Forewor
d
New protocols, technologies, and delivery and O&M modes are emerging in
the network engineering field. Conventional networks face challenges from
new connection requirements, such as requirements for cloud computing
and artificial intelligence (AI). Enterprises are also pursuing service agility,
flexibility, and elasticity. Against this backdrop, network automation becomes
increasingly important.
Network programmability and automation is to simplify network
configuration, management, monitoring, and operations for engineers and
improve deployment and O&M efficiency. This course is to help network
engineers understand Python programming and implement network
automation.
age 3 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Objective
s
On completion of this course, you will be able to:
▫ Describe the difficulties of conventional network O&M.
age 4 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Content
s
1. Introduction to Network Programmability and Automation
2. Overview of Programming Language and Python
3. Cases
age 5 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Background: Difficulties in Conventional
Network O&M
Conventional network O&M requires network engineers to manually log in to network devices, query and
execute configuration commands, and filter command output. This highly human-dependent working
mode is time-consuming, inefficient, and difficult to audit.
Typical O&M Scenarios
Network device
age 6 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Network Automation
Network automation: Tools are used to implement automated network deployment, operations, and O&M,
gradually reducing dependency on human. This solves the conventional network O&M problems.
Many open-source tools, such as Ansible, SaltStack, Puppet, and Chef, are available for network
automation in the industry. From the perspective of network engineering capability construction, it is
recommended that engineers acquire the code programming capability.
Keywords of
Chef network SaltSta
automation ck
NMS
tool
Ansibl
Python Automate
e d scripts
Shell
age 7 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Programming-based Network
Automation
In recent years, with the emergence of network automation technologies, Python-based
programming capabilities have become a new skill requirement for network engineers.
Automation script written in Python can execute repeated, time-consuming, and rule-based
Example: Implementing automated device
operations. configuration
Python file
using Python
Configuration • What can network automation do? The most intuitive
File example of network automation is automated device
Sysname SW1 SSH/Telnet
configuration. This process can be divided into two
Vlan 10
description A steps: writing a configuration file, and writing Python
Vlan20 code to push the configuration file to a device.
description B Network • Write the configuration script in command line interface
VLAN 30 device (CLI) mode, and then upload the script to the device
description C
using Telnet/SSH. This method is easy to understand for
network engineers who are beginning to learn network
programmability and automation. This presentation
describes how to implement network automation.
age 8 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Content
s
1. Introduction to Network Programmability and Automation
2. Overview of Programming Language and Python
3. Cases
age 9 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Programming Languages
A programming language is used to write a computer program and control behavior of a computer.
According to whether compilation is required before execution of a language, the programming language
may be classified into the compiled language, and interpreted language that does not need to be
compiled.
Compiler
Interpreter:
Interprets source
code line by line.
Executable file
age 10 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Computing Technology Stack and
Program Execution Process
Computing Technology Program Execution Process
Stack
Application temp = v [k]; TEMP = V[K]
v[k] = V[K] =
Algorithm High-level v[k+1]; V[K+1]
Software
programmin v[k+1] = V[K+1] =
High-Level Language g language temp;
C/C++
TEMP
Fortran
compiler compile
r
Assembly Language
Increasing order of
lw $t0, 0($2)
Increasing order of
lw $t1, 4($2)
Machine Code Assembly sw $t1, 0($2)
language sw $t0, 4($2)
Instruction Set Architecture
Complexity
Abstraction
Assembler
0000 1001 1100 0110 1010 1111 0101
Micro Architecture 1000
Hardwar
1010 1111 0101 1000 0000 1001 1100
0110
Gates/Registers Machine 1100 0110 1010 1111 0101 1000 0000
code 1001
Transistors 0101 1000 0000 1001 1100 0110 1010
e
1111
Instructio Instruction
Physics Data 1
n set 1
age 11 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
High-level Programming Language -
Compiled Language
Compiled language: Before a program in a compiled language is executed, a compilation process is
performed to compile the program into a machine language file. The compilation result can be directly
used without re-translation during running. Typical compiled languages include C/C++ and Go.
From source code to program: The source code needs to be translated into machine instructions by the
compiler and assembler, and then the linker uses the link library function to generate the machine
language program. The machine language must match the instruction set of the CPU, which is loaded to
the memory by the loader during running and executed by the CPU.
Executable
Object module:
Assembly code:
C/C++ Compile machine
language Assembler Linker machine Loader Memory
source code r language
program language
module
program
age 12 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
High-level Programming Language -
Interpreted Language
Interpreted language: Interpreted language programs do not need to be compiled before running. They
are translated line by line when running. Typically, Java and Python are interpreted languages.
Process from source code to programs: Source code of an interpreted language is generated by the
compiler and then interpreted and executed by a virtual machine (VM) (for example, JVM/PVM). The VM
shields the differences between CPU instruction sets. Therefore, portability of the interpreted language is
relatively good.
Java language
Python program
program
Compiler Compiler
Python library
Class file Java library function functions .pyc file
(byte code) (machine language) (machine (byte code)
language)
JVM PVM
age 13 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
What Is Python?
Python is a fully-open-source high-level programming language. Its author is Guido Van
Rossum.
Advantages of Python: Disadvantages of Python:
• Is a dynamically typed interpreted language with elegant • Runs slow. Is an interpreted
syntax. It allows learners to focus on program logic rather language that runs without being
than syntax detail learning. compiled. Code is translated line by
• Supports both process- and object-oriented programming. line at run time into machine code
• Provides abundant third-party libraries. that the CPU can understand, which
• Is nicknamed the glue language because it can call code is time-consuming.
written in other languages.
With support for abundant third-party libraries and advantages of the Python language, Python
can be used in many fields, such as AI, data science, apps, and scripts for automated O&M.
age 14 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Python Code Execution Process
Process of compiling and
running a Python Operation
program
Python source 1. Install Python and the running
code
environment in an operating system.
age 15 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Getting Started with Python Code -
Interactive Running
Python runs in either interactive or script mode.
Interactive programming does not require script files to be created, and code is written in the
interactive mode of the Python interpreter.
C:\Users\Richard>python
Python 3.7.4 (default, Aug 9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)]
:: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
1. Input -- >>> print ("hello world")
2. Output -- hello world
3. Input -- >>> a = 1
4. Input -- >>> b = 2
5. Input -- >>> print ( a + b )
6. Output -- 3
>>>
age 16 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Getting Started with Python Code -
Script-based Running
Code in script mode can run on various Python compilers or in integrated development
environments. For example, IDLE, Atom, Visual Studio, Pycharm, and Anaconda provided by
Python can be used.
demo.py
age 17 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Code Style Guide for Python
Code style rules refer to naming rules, code indentation, and code and statement segmentation modes
that must be complied with when Python is used to write code. Good style rules help improve code
readability and facilitate code maintenance and modification.
For example, the following rules for using semicolons, parentheses, blank lines, and spaces are
recommended:
Semicolon Blank line
• A semicolon can be added at the end of a • Different functions or statement blocks can be
line in Python, but is not recommended to separated by spaces. A blank line helps
separate statements. differentiate two segments of code, improving
• It is recommended that each sentence be in code readability.
a separate line.
Parentheses Space
• Parentheses can be used for the continuation • Spaces are not recommended in parentheses.
of long statements. Avoid unnecessary • You can determine whether to add spaces on
parentheses.
both ends of an operator.
age 18 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Code Style Guide for Python - Identifier
Naming
• A Python identifier represents the name of a constant, variable, function, or another object.
• An identifier is usually composed of letters, digits, and underscores, but cannot start with a digit.
Identifiers are case sensitive and must be unique. If an identifier does not comply with the rules, the
compiler will output a SyntaxError message when running the code.
age 19 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Code Style Guide for Python - Code
Indentation
In Python programs, code indentation represents the scope of a code block. If a code block
contains two or more statements, the statements must have the same indentation. For
Python, code indentation is a syntax rule that uses code indentation and colons to distinguish
between layers of code.
When writing code, you are advised to use four spaces for indentation. If incorrect indentation
is used in the program code, an IndentationError error message is displayed during code
running.
if True:
Correct indentation -- print ("Hello")
else:
Correct indentation -- print (0)
a = “Python”
Incorrect indentation -- print (a)
age 20 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Code Style Guide for Python - Using
Comments
Comments are explanations added to programs to improve program readability. In the Python
program, comments are classified into single-line comments and multi-line comments.
A single-line comment starts with a pound sign (#).
A multi-line comment can contain multiple lines, which are enclosed in a pair of three
quotation marks ('''...''' or '''''' ...'''''').
“””
Multi-line comment -- The output is Python.
“””
age 21 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Code Style Guide for Python - Source
Code File Structure
A complete Python source code file generally consists of interpreter and encoding format
declaration, document string, module import, and running code.
If you need to call a class of a standard library or a third-party library in a program, use
"import" or "from... import" statement to import related modules. The import statement is
always after the module comment or document string (docstring) at the top of the file.
Interpreter declaration -- #!/usr/bin/env python
Encoding format declaration -- #-*- coding:utf-8 -*-
age 23 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Python Classes and Methods
A class is a collection of properties and methods that are the same. The class keyword is used
to define a class.
The function of an instantiated class is called a method. When you define a method, a class
must carry the self keyword, which indicates the instance of the class.
demo.py
Import the Telnet class of the telnetlib module. from telnetlib import Telnet
-- tn = Telnet(host=None, port=0[, timeout])
Create a Telnet connection to a specified server. tn.read_all()
-- …
Invoke the read_all() method. --
Method Function
Telnet.read_until (expected, Read data until a given byte string, expected, is encountered or until timeout seconds
timeout=None) have passed.
Telnet.read_all () Read all data until EOF as bytes; block until connection closed.
Read everything that can be without blocking in I/O (eager). Raise EOFError if
Telnet.read_very_eager() connection closed and no cooked data available. Return b'' if no cooked data available
otherwise. Do not block unless in the midst of an IAC sequence.
Telnet.write(buffer) Write a byte string to the socket, doubling any IAC characters.
age 25 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Content
s
1. Introduction to Network Programmability and Automation
2. Overview of Programming Language and Python
3. Cases
age 26 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Case: Logging In to a Device Using
telnetlib
Case description :
A network device functions as a Telnet server, and the Python telnetlib needs to be used as a Telnet client
to log in to the device.
Verify the Telnet login Verify the
Configure Telnet. Write Python code.
procedure. result.
192.168.10. 192.168.10.
10 20
GE1/0/1
Telnet 0 Telnet
server client
The implementation process is as follows :
Configure the Telnet service.
Manually verify and view the Telnet login procedure as a reference for code implementation.
Compile and run Python code.
Verify the result.
age 27 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Case: Logging In to a Device Using
telnetlib
Configure Verify the Telnet login Verify the
Telnet. Write Python code.
procedure. result.
192.168.10. 192.168.10.
10 20
GE1/0/1
Telnet 0 Telnet
server client
age 28 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Case: Logging In to a Device Using
telnetlib
Verify the Telnet login
Configure Telnet. Write Python code. Verify the result.
procedure.
192.168.10. 192.168.10.
10 20
GE1/0/1
Telnet 0 Telnet
server client
Telnet
login:
1 Run a login C:\Users\Richard>telnet 192.168.10.10
command.
Command output Login authentication
Info: The max number of VTY users is 5, and the number of current VTY users on line is
1. The current login time is 2020-01-15 21:12:57.
192.168.10. 192.168.10.
10 20
GE1/0/1
Telnet 0 Telnet
server client
C:\Users\Richard>telnet 192.168.10.10
Login authentication
Manual Telnet login
result: Password:
Info: The max number of VTY users is 5, and the number of current VTY users on
line is 1. The current login time is 2020-01-15 21:12:57.
<Huawei>
#Run Python code in the compiler.
Python code Info: The max number of VTY users is 5, and the number
execution result: of current VTY users on line is 1.
The current login time is 2020-01-15 22:12:57.
<Huawei>
age 31 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Quiz
1. Python is a compiled language. ( )
A. True
B. False
age 32 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Summar
y
Network automation uses tools to implement automated network
deployment, operation, and O&M, gradually reducing dependency on people.
You can use a programming language or tool to implement the network
automation.
Python is a fully-open-source high-level programming language that is
simple syntax and is easy to learn. It has rich standard libraries and third-
party libraries, which are applicable to the network engineering field.
The telnetlib module of Python provides the telnetlib.Telnet class for
implementing the Telnet function. It helps you enter the network
programmability and automation world!
age 33 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
More Information
For more information about Python, visit https://www.python.org/.
age 34 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Thank You
www.huawei.com
age 35 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.