H3C_V5_V7设备巡检信息提取

This is an article that was created 1274 days ago, and the information may have evolved or changed.

上一篇博客写到如何运行脚本导出设备信息,这一篇介绍一下怎样在海量的设备信息中提取指定信息,本文中假设要提取的信息有设备名称当前软件版本设备运行时间设备 CPU 使用率设备内存使用率设备序列号。后续可以根据这些信息使用 Python 脚本写入到巡检报告或者表格中。

如何操作

  • 没有提取前已导出的设备信息(保护隐私,使用 HCL 模拟器产生的设备信息)

Python 脚本

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Author:KIRASTER
# Time:2021/3/21 23:26
# Description:Device_info
import os
import re


filename = 'F:/111.txt'
resultfilename = 'F:/result.txt'
if os.path.exists(resultfilename):
os.remove(resultfilename)
result_txt = open('F:/result.txt', 'a')

with open(filename, 'r', encoding='utf8') as file_to_read:
while True:
lines = file_to_read.readline()
# 首行显示50个“=”号,提取设备版本信息,开头注释
if "display version" in lines:
result_line01 = '=' * 50 + '\n'
result_line02 = 'Device Name:\t' + ''.join(re.findall(r'[<](.*?)[>]', lines)) + '\n'
result_txt.write(result_line01)
print(result_line01)
print(result_line02)
result_txt.write(result_line02)
# 提取设备版本信息
if "Comware Software" in lines:
result_line03 = 'Comware Software:\t' +''.join(re.findall('(?<=Software, ).*$', lines)) + '\n'
print(result_line03)
result_txt.write(result_line03)
# 提取设备运行时间
if "uptime is" in lines:
result_line04 = 'Uptime:\t' + ''.join(re.findall('(?<=uptime is ).*$', lines)) + '\n'
print(result_line04)
result_txt.write(result_line04)
# 提取设备序列号
if "DEVICE_SERIAL_NUMBER" in lines:
result_line05 = 'SN:\t' + ''.join(re.findall('(?<=:).*$', lines)) + '\n'
result_line05 = result_line05.replace(' ', '')
print(result_line05)
result_txt.write(result_line05)
# 提取设备cpu使用率方式1
if "Slot 1 CPU 0 CPU usage:" in lines:
result_line06 = next(file_to_read)
result_line06 = result_line06[5:9] + '\n'
result_line06 = 'cpu-usage:\t' + result_line06.replace(' ', '')
print(result_line06)
result_txt.write(result_line06)
# 提取设备cpu使用率方式2
if "Slot 1 CPU usage:" in lines:
result_line066 = next(file_to_read)
result_line066 = result_line066[5:9] + '\n'
result_line066 = 'cpu-usage:\t' + result_line066.replace(' ', '')
print(result_line066)
result_txt.write(result_line066)
# 提取设备cpu使用率方式3
if "Unit CPU usage:" in lines:
result_line0666 = next(file_to_read)
result_line0666 = result_line0666[5:9] + '\n'
result_line0666 = 'cpu-usage:\t' + result_line0666.replace(' ', '')
print(result_line0666)
result_txt.write(result_line0666)
# 提取设备memory使用率方式1
if "Used Rate" in lines:
result_line07 = lines[-4:]
result_line07 = 'memory-usage:\t' + result_line07.replace(' ', '')
print(result_line07)
result_txt.write(result_line07)
# 提取设备memory使用率方式2
if "Mem:" in lines:
result_line08 = lines[-6:]
result_line08 = result_line08.replace(' ', '')
result_line08 = result_line08.replace('%', '')
result_line08 = 100 - float(result_line08)
result_line08 = '%.1f%%' % result_line08
result_line08 = 'memory-usage:\t' + result_line08 + '\n'
print(result_line08)
result_txt.write(result_line08)
result_txt.flush()
if "结束记录时间" in lines:
break


生成信息

其他

  • 本文中的脚本在实际工作中使用过,设备包含华三 V5 和 V7 版本的交换机、防火墙和无线控制器等
  • 根据实际需要可以修改脚本中的内容提取更多信息
  • 生成的文件为 word 或 excel 等都是可以实现的
HCL_VPN不同实例互通实验 H3C_SecureCRT巡检脚本
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×