说明:利用eNSP华为模拟器,拉出一台AR路由器,以及一朵云,该云可以实现与本机设备网络上互通。
eNSP下载地址:链接: https://pan.baidu.com/s/15LhQjYQVThrLudeP8aJifA?pwd=yh9c 提取码: yh9c
<HUAWEI>system-view # 进入系统试图
[HUAWEI]aaa # 进入aaa系统
[HUAWEI-aaa]local-user admin password cipher Huhawei@123 # 配置admin账号的密码为Huawei@123
[HUAWEI-aaa]local-user admin privilege level 15 # 配置admin账号的用户权限为15,最高位15
[HUAWEI-aaa]local-user admin service-type telnet terminal ssh http # 配置admin账号可以用来telnet terminal ssh http登录
[HUAWEI-aaa]quit
[HUAWEI]user-interface vty 0 4
[HUAWEI-ui-vty0-4]authentication-mode aaa
[HUAWEI-ui-vty0-4]protocol inbound all
[HUAWEI-ui-vty0-4]quit
[HUAWEI]rsa local-key-pair create
The key name will be: Host
% RSA keys defined for Host already exist.
Confirm to replace them? (y/n)[n]:
[HUAWEI]rsa local-key-pair create
The key name will be: Host
% RSA keys defined for Host already exist.
Confirm to replace them? (y/n)[n]:y
The range of public key size is (512 ~ 2048).
NOTES: If the key modulus is greater than 512,
It will take a few minutes.
Input the bits in the modulus[default = 512]:
Generating keys...
................++++++++++++
...........++++++++++++
..............++++++++
.++++++++
[HUAWEI]stelnet server enable # 确保ssh、stelnet服务已开启
[HUAWEI]interface GigabitEthernet 0/0/0 # 进去GE0/0/0端口
# 配置I192.168.16.100的ip地址,该地址要跟云中绑定的虚拟网卡同一网段
[HUAWEI-GigabitEthernet0/0/0] ip address 192.168.16.100 255.255.255.0
说明:按照上图的配置对云进行配置即可。
#!/usr/bin/env python
# coding=utf-8
import paramiko,time,datetime,getpass,re
# 需要巡检的数据
def data():
cpu = re.findall(r"CPU Usage :.\d\d.", str)
for i in cpu:
print('CPU:' , re.findall(r"\d\d.", i),end=' ')
memory = re.findall(r" Memory Using Percentage Is: \d\d.", str)
for i in memory:
print('内存:' , re.findall(r"\d\d.", i),end=' ')
temperature = re.findall(r"display temperature all([\s\S]*)[Core]", str)
temperature = re.findall(r'Normal \d{2}', i)
temperature = re.findall(r'\d{2}', i)
print('温度:', temperature , "℃",end=' ')
device = re.findall(r"Present PowerOn Registered..........", str)
for i in device:
print( re.findall(r"Normal", i),end=' ')
fan = re.findall(r"display fan([\s\S]*)display temperature all", str)
for i in fan:
print(re.findall(r"Normal", i),end=' ')
# 读取命令
file = open('comm.txt')
with file as comm:
comm = comm.readlines()
# # 配置登录信息
username = 'admin'
password = 'Huawei@12345'
# 读取ip地址
for ip,names in zip(open('ip.txt', 'r'),open('name.txt')): #打开文件
ip = ip.rstrip('\n') # 移除行尾换行符
print('【' + names.rstrip('\n') + '】')
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip, username=username, password=password) # 进行输入账号密码
command = ssh_client.invoke_shell()
for com in comm:
command.send(com)
##暂停2秒,并将命令执行过程赋值给output对象,通过print output语句回显内容
time.sleep(2)
output = command.recv(655355).decode()
str = output
data()
print('\n')
##退出SSH
ssh_client.close()
说明:在配置Python脚本时,需要创建comm.txt、ip.txt、name.txt。
Python脚本文件以及所需要创建的文本如下图:
ip.txt:写入需要巡检的ip地址。
comm.txt:写入巡检的命令。
name.txt:为了能够区别巡检的设备,ip与设备名称一一对应。
Python文件脚本下载地址:「网络设备巡检python脚本」https://www.aliyundrive.com/s/7P6PRUNMHS4
ps:巡检的名字可以用数据清洗中自动获取,过程对于本人来说过于麻烦,因此直接使用文本一行一行获取与之对应。
说明:上图为各个文本内容,下文为模拟器巡检结果。
【AR】
<HUAWEI>display device
AR2220's Device status:
Slot Sub Type Online Power Register Alarm Primary
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
0 - AR2220 Present PowerOn Registered Normal Master
7 - PWR Present PowerOn Registered Normal NA
<HUAWEI>
进程已结束,退出代码0
各文本配置内容
说明:上图为各个文本内容,下文为真实机器的巡检。
【A-GG1-NT-HX-HW-7712-001】
Hello!
Welcome to system!
Info: The max number of VTY users is 10, and the number
of current VTY users on line is 1.
The current login time is 2022-04-14 09:17:54+08:00.
<Core>display cpu-usage
CPU Usage Stat. Cycle: 60 (Second)
CPU Usage : 18% Max: 88%
CPU Usage Stat. Time : 2022-04-14 09:17:55
CPU utilization for five seconds: 18%: one minute: 18%: five minutes: 18%
Max CPU Usage Stat. Time : 2022-04-09 14:46:33.
TaskName CPU Runtime(CPU Tick High/Tick Low) Task Explanation
VIDL 82% 5/ba4635fe DOPRA IDLE
OS 11% 0/c4b200fe Operation System
bcmCNTR.0 5% 0/5ae5a740
SFPT 1% 0/135f1b40 SFPT Timer
TICK 1% 0/1954fc40
1AGA 0% 0/ 0 1AGAGT
AAA 0% 0/ 0 AAA Authen Account Authorize
ACL 0% 0/ 4c4b40 ACL Access Control List
ADPT 0% 0/ 0 ADPT Adapter
AGENT_CAPWAP 0% 0/ 0 AGENT_CAPWAP
AGENT_WLAN 0% 0/ 0 AGENT_WLAN
AGNT 0% 0/ 0 AGNTSNMP agent task
AGT6 0% 0/ 0 AGT6SNMP AGT6 task
ALM 0% 0/ 0 ALM Alarm Management
ALS 0% 0/ 0 ALS Loss of Signal
AM 0% 0/ e4e1c0 AM Address Management
APP 0% 0/ 0 APP
---- More ----
<Core>
<Core>display memory-usage
Memory utilization statistics at 2022-04-14 09:17:55+08:00
System Total Memory Is: 1598029824 bytes
Total Memory Used Is: 160886188 bytes
Memory Using Percentage Is: 10%
<Core>display device
S5720-32P-EI-AC's Device status:
Slot Sub Type Online Power Register Status Role
-------------------------------------------------------------------------------
0 - S5720-32P-EI Present PowerOn Registered Normal Master
<Core>display fan
-------------------------------------------------------------------------
Slot FanID Online Status Speed Mode Airflow
-------------------------------------------------------------------------
0 1 Present Normal 35% Auto Side-to-Side
<Core>display temperature all
-------------------------------------------------------------------------------
Slot Card Sensor Status Current(C) Lower(C) Lower Upper(C) Upper
Resume(C) Resume(C)
-------------------------------------------------------------------------------
0 NA NA Normal 44 0 4 58 54
<Core>
进程已结束,退出代码0
配合数据清洗后的运行结果,得到自己想要的数据:
【A-GG1-NT-HX-HW-7712-001】
CPU: ['18%'] 内存: ['10%'] 温度: ['10'] ℃ ['Normal'] ['Normal']
「eNSP软件」 https://pan.baidu.com/s/15LhQjYQVThrLudeP8aJifA?pwd=yh9c 提取码: yh9c
「网络设备巡检python脚本」 https://www.aliyundrive.com/s/7P6PRUNMHS4
「python自动化运维实验」 https://www.aliyundrive.com/s/ENSjWiZknrr