ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-01-31 23:34:06  阅读:355  来源: 互联网

标签:huawei Value yaml nornir 交换机 install device 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]]

标签:huawei,Value,yaml,nornir,交换机,install,device,textfsm
来源: https://blog.csdn.net/binlinuxbin/article/details/122761968

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有