Say goodbye to manual inspections and automate network device management more efficiently
In a modern network architecture, the health of network devices is directly related to the stability and performance of the entire system. In order to ensure the continuous and efficient operation of network devices, traditional manual inspection methods often face problems such as heavy workload, low efficiency, and error-prone. Therefore, automatic inspection of network equipment has become an important means to improve network O&M efficiency.
The necessity of automated inspections
- Efficiency: Manual inspection is time-consuming and laborious, while automated inspection can quickly complete a large number of tasks, significantly improving efficiency.
- Reduce human error: manual inspection is prone to errors, and the automated system executes according to rules to reduce errors.
- Data traceability: The results of automated inspections will be recorded and saved to provide data support for troubleshooting and performance optimization.
- Detect potential problems in good time: Automated systems can check the status of equipment in real time or on a regular basis, detect anomalies before they occur, and reduce downtime.
The core technology of automated inspection
(1) Netmiko
Netmiko is a Python library designed for network devices to facilitate operations such as configuration tuning, command execution, and more. The library is compatible with a wide range of network devices and protocols from a variety of vendors, which greatly facilitates engineers to use Python scripts for automated inspection and configuration management.
(2) TextFSM
TextFSM is a Python library specifically designed to extract information from structured text, specifically the output of network devices. It utilizes templates to match and parse data and convert the results into an easy-to-process format such as a dictionary or list. This makes it easy to automatically extract critical information from CLI outputs such as routing tables and interface status, making it easy for network operators and developers to conduct further analysis.
Basic usage
(1) Install Netmiko
You'll need to install Netmiko first. It is possible to install with pip:
pip install netmiko
- 1.
(2) Basic connection
When using Netmiko to connect to a network device, you need to specify information such as the IP address, device type, username, and password of the device. Netmiko supports multiple device types (e.g., Cisco, Huawei, Juniper, etc.) that correspond to different commands and interactions.
from netmiko import ConnectHandler
# 定义设备连接信息
device = {
'device_type': 'huawei', # 设备类型,例如 'cisco_ios'、'huawei' 等
'host': '192.168.56.10', # 设备的 IP 地址
'username': 'user01', # 登录用户名
'password': 'user01', # 登录密码
'secret': 'secret', # (可选) 如果需要启用特权模式,可以提供 secret
}
# 建立连接
net_connect = ConnectHandler(**device)
# 进入特权模式(如果需要)
# net_connect.enable()
# 执行命令并获取输出
output = net_connect.send_command('display version')
# 打印命令输出
print(output)
# 断开连接
net_connect.disconnect()
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
(3) Command output formatting
Netmiko supports formatting command output in different ways. For example, you can use send_command with the use_textfsm=True parameter to parse the output into structured data (a TextFSM template needs to be pre-configured).
# 执行命令并获取输出
output = net_connect.send_command('display version',use_textfsm=True,\
textfsm_template='./templates/huawei/huawei_version.textfsm')
- 1.
- 2.
- 3.
After the preceding test script is executed, the following result is returned:
$ python test.py
[{'version': '5.110', 'device_model': 'S5700-28C-HI', 'uptime': '0 week, 0 day, 2 hours, 46 minutes'}]
- 1.
- 2.
Encapsulation scripting tool
This script uses Netmiko and TextFSM to help us perform device inspections. Here's a diagram of the script directory structure:
- templates directory to store the TextFSM parsing template.
- config.toml is the configuration file for the project
- devices.xlsx documents to store inspection equipment and inspection orders. The format is shown in the following figure:
Inspect the equipment ledger
Inspection commands
Using this script is very simple. First, adjust the contents of the devices.xlsx file to your specific situation, and then you can run the main.py.
This script now implements the basic functionality and provides a good starting point for everyone. We highly encourage students with ideas to further develop and expand more practical functions according to their own business needs!