Network Programmability and Automation
Network Programmability and Automation
Contents
3 Cases
Page 1 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.
Page 2 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 network
Chef automation SaltStack
NMS
tool
Ansible Python Automated
scripts
Shell
Page 3 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 operations.
Example: Implementing automated device configuration
Python file
using Python
Configuration File • What can network automation do? The most intuitive
example of network automation is automated device
Sysname SW1 SSH/Telnet configuration. This process can be divided into two steps:
Vlan 10
description A writing a configuration file, and writing Python code to push
Vlan20 the configuration file to a device.
description B Network • Write the configuration script in command line interface (CLI)
VLAN 30 device mode, and then upload the script to the device using
description C
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.
Page 4 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Contents
3 Cases
Page 5 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
Page 6 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Computing Technology Stack and Program Execution Process
Computing Technology Stack Program Execution Process
Application
temp = v [k]; TEMP = V[K]
Algorithm High-level v[k] = v[k+1]; V[K] = V[K+1]
Increasing order of Complexity
programming
Increasing order of Abstraction
Software
High-Level Language language C/C++ Fortran
compiler compiler
Assembly Language
lw $t0, 0($2)
lw $t1, 4($2)
Machine Code Assembly sw $t1, 0($2)
language sw $t0, 4($2)
Instruction Set Architecture
Assembler
Micro Architecture
Hardware
0000 1001 1100 0110 1010 1111 0101 1000
1010 1111 0101 1000 0000 1001 1100 0110
Gates/Registers Machine 1100 0110 1010 1111 0101 1000 0000 1001
code 0101 1000 0000 1001 1100 0110 1010 1111
Transistors
Instruction
Physics Instruction 1 Data 1
set
Page 7 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.
Page 8 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
JVM PVM
Page 9 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.
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.
Page 10 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Python Code Execution Process
Process of compiling and
running a Python program Operation
Page 11 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
>>>
Page 12 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
Page 13 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:
Parentheses Space
• Parentheses can be used for the continuation of • Spaces are not recommended in parentheses.
long statements. Avoid unnecessary parentheses. • You can determine whether to add spaces on both
ends of an operator.
Page 14 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.
Page 15 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)
Page 16 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 '''''' ...'''''').
Single-line comment -- # Assign a string to a.
a = “Python”
print (a)
“””
Multi-line comment -- The output is Python.
“””
Page 17 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 -*-
⚫ A module is a saved Python file. Modules can contain definitions of functions, classes, and variables that can then
be utilized in other Python programs. The only difference between a module and a regular Python program is that
the module is used for importing by other programs. Therefore, a module usually does not have a main function.
demo.py test.py
def sit(): #Define a function. import demo #Import a module.
print ('A dog is now sitting’)
demo.sit() #Call a function.
sit() #Call a function.
Execution result:
Execution result:
A dog is now sitting.
A dog is now sitting. A dog is now sitting.
⚫ Different methods in the telnetlib.Telnet class are called to implement different functions.
Import the Telnet class of the telnetlib module. -- from telnetlib import Telnet
Create a Telnet connection to a specified server. -- tn = Telnet(host=None, port=0[, timeout])
Invoke the read_all() method. -- tn.read_all()
…
Method Function
Read data until a given byte string, expected, is encountered or until timeout seconds have
Telnet.read_until (expected, timeout=None)
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 connection
Telnet.read_very_eager() 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.
Page 21 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Contents
3 Cases
• Cases
Page 22 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.10 192.168.10.20
GE1/0/10
Telnet server Telnet client
Page 23 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Case: Logging In to a Device Using telnetlib
Configure Telnet. Verify the Telnet login Verify the
Write Python code.
procedure. result.
192.168.10.10 192.168.10.20
GE1/0/10
Telnet server Telnet client
Page 24 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Case: Logging In to a Device Using telnetlib
Configure Telnet. Verify the Telnet login procedure. Write Python code. Verify the result.
192.168.10.10 192.168.10.20
GE1/0/10
Telnet server Telnet client
Telnet login:
Page 25 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.10 192.168.10.20
GE1/0/10
Telnet server Telnet 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>
Page 27 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Quiz
1. Python is a compiled language. ( )
A. True
B. False
Page 28 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Summary
⚫ 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.
⚫ 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!
Page 29 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
More Information
⚫ For more information about Python, visit https://www.python.org/.
Page 30 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Thank You
www.huawei.com
Page 31 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.