华为交换机 nornir + textfsm使用入门

安装相关模块

pip3 install netmiko

pip3 install napalm

pip3 install nornir

pip3 install nornir_utils

pip3 install nornir_napalm

pip3 install nornir_netmiko

pip3 install textfsm

创建相关配置文件(config.yml,defaults.yaml,groups.yaml,hosts.yaml)

config.yml

---
inventory:
    plugin: SimpleInventory
    options:
        host_file: "hosts.yaml"
        group_file: "groups.yaml"
        defaults_file: "defaults.yaml"
runner:
    plugin: threaded
    options:
        num_workers: 100

defaults.yaml

---

username: xxx
password: 'xxx'

groups.yaml

---

huawei_test:

        platform:huawei

hosts.yaml

---
test1:
    hostname: 192.168.x.x
    username: xxx
    password: 'xxx'
    groups:
        - huawei_test

python输出device信息:

from nornir import InitNornir
from nornir_netmiko import netmiko_send_command
from nornir_utils.plugins.functions import print_result
from nornir.core.filter import F

nr = InitNornir(config_file="config.yaml")

def dis_device(group,cmd):

        group = nr.filter(F(groups__contains=group))

        results = group.run(netmiko_send_command, command_string=cmd,use_textfsm=True)

        for key in results.keys:

                res = results[key].result

                ip = group.inventory.hosts[key].hostname

dis_device('huawei_test','display device')

#use_textfsm=True ,如果模板存在就按照模板显示

#路径  ...../site-packages/ntc-templates/templates

#自定义模板,templates文件夹下

创建huawei_display_device.textfsm:

Value Filldown device_name (.+) 

Value Filldown Slot (\w+)

Value Type (\S+)

Value Online  ([a-zA-Z]+)

Value Register ([a-zA-Z]+)

Value Status ([a-zA-Z]+)

Value Role  ([a-zA-Z]+)

Value LsId (\d+)

Value Primary([a-zA-Z]+)

Start

        ^${device_name}\s+Device\s+Status

                ^${Slot}\s+${Type}\s+${Online}\s+${Register}\s+${Status}\s+${Role}\s+${LsId}\s+${Primary}\s+ -> Record

EOF

同时在templates文件夹的index文件添加一行

huawei_display_device.textfsm, .* ,huawei ,disp[[lay]]  dev[[ice]]

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值