|
接上篇:
这次我写了一个稍微复杂的textfsm模板(模板里有filldown、required、list),利用net_inspect提取华为USG防火墙地址对象信息,只是扩展net_inspect的实际应用场景,希望可以激发大家对net_inspect和textfsm的兴趣,再次感谢 @Elin 大神的付出!
相关参考:
[1]
net_inspect项目源码--by知乎“Elin”大神
[2]
《网络工程师的Python之路》奕心老师教程
[3]
【网工手艺】专栏-朱嘉盛python网络运维自动化零基础入门及进阶
<hr/>用到的文件
华为USG防火墙回显内容(截取部分,且脱敏处理)
[USG]display version
Huawei Versatile Routing Platform Software
[USG]disp ip address-s type object
Object address-set maximum number(s): 30000
Object address-set total number(s): 1319
Object address-set item total number(s): 2106
Object address-set reference total number(s): 1330
Address-set: 开挂的史莱克7怪
Type: object
Item number(s): 7
Reference number(s): 1
Item(s):
address 0 1.1.1.1 mask 32
address 1 2.2.2.2 mask 32
address 2 3.3.3.3 mask 32
address 3 4.4.4.4 mask 32
address 4 5.5.5.5 mask 32
address 5 6.6.6.6 mask 32
address 6 7.7.7.7 mask 32
Address-set: 武魂殿7大供奉
Type: object
Item number(s): 1
Reference number(s): 1
Item(s):
address 0 range 10.10.10.1 10.10.10.7
Address-set: 编不下去了
Type: object
Item number(s): 1
Reference number(s): 1
Item(s):
address 0 10.10.10.192 mask 255.255.255.224“huawei_vrp_display_ip_address-set_type_object.textfsm”模板内容
Value Filldown Obj_Name (\s*\S+\s*\S*)
Value Required Obj_Nums (\d+)
Value List Obj_Items (\S+\s*\S+\s*\S*)
Start
^Address-set: -> Continue.Record
^Address-set: ${Obj_Name}
^Item number\(s\): ${Obj_Nums}
^\s+address \d+\s*(range)* ${Obj_Items}index文件内容
请看上篇代码
from net_inspect import NetInspect
import pandas as pd
net = NetInspect()
net.set_plugins(input_plugin=&#39;console&#39;)
net.set_external_templates(r&#39;D:\Py_exec\local_templates&#39;) # 调用本地模板库
net.run(path=r&#39;D:\Py_exec\logs&#39;)
print(&#39;total devices:&#39;, len(net.cluster.devices))
for device in net.cluster.devices:
info = device.info
print(info.hostname)
with device.search_cmd(&#39;display ip address-set type object&#39;) as cmd:
df = cmd.parse_result
aaa = pd.DataFrame(df)
aaa.to_csv(&#34;objects.csv&#34;, index=False)
print(&#34;请检查生成的csv文件!&#34;)
aaa.to_excel(&#39;objects.xlsx&#39;)
print(&#34;请用WPS或Office打开生成的xlsx文件!&#34;)请注意:net-inspect作者还在陆续更新版本,如果新版本碰到类似下面的报错,按照提示修改代码参数即可:
net.run(input_path=r&#39;D:\Py_exec\logs&#39;)
NetInspect.run: `path`参数已废弃, 请使用`input_path`参数代替.执行代码后的输出
csv文件
obj_name,obj_nums,obj_items
开挂的史莱克7怪,7,&#34;[&#39;1.1.1.1 mask 32&#39;, &#39;2.2.2.2 mask 32&#39;, &#39;3.3.3.3 mask 32&#39;, &#39;4.4.4.4 mask 32&#39;, &#39;5.5.5.5 mask 32&#39;, &#39;6.6.6.6 mask 32&#39;, &#39;7.7.7.7 mask 32&#39;]&#34;
武魂殿7大供奉,1,[&#39;10.10.10.1 10.10.10.7&#39;]
编不下去了,1,[&#39;10.10.10.192 mask 255.255.255.224&#39;]excel文件
obj_name | obj_nums | obj_items | 0 | 开挂的史莱克7怪 | 7 | [&#39;1.1.1.1 mask 32&#39;, &#39;2.2.2.2 mask 32&#39;, &#39;3.3.3.3 mask 32&#39;, &#39;4.4.4.4 mask 32&#39;, &#39;5.5.5.5 mask 32&#39;, &#39;6.6.6.6 mask 32&#39;, &#39;7.7.7.7 mask 32&#39;] | 1 | 武魂殿7大供奉 | 1 | [&#39;10.10.10.1 10.10.10.7&#39;] | 2 | 编不下去了 | 1 | [&#39;10.10.10.192 mask 255.255.255.224&#39;] |
|
|