Nornir 获取cisco设备接口信息-LAB

本文介绍了如何利用Python的Nornir库来自动化管理网络设备,包括创建虚拟环境、安装依赖库、配置文件的设置以及运行示例代码,通过Nornir连接到Cisco Nexus和 IOS设备,获取设备事实和接口信息。
摘要由CSDN通过智能技术生成

拓扑:

 

创建、进入、退出虚拟环境:

创建:python3 -m venv test

进入:  source /root/test/bin/activate

退出:  deactivate

依赖库:

nornir==3.1.1

nornir-napalm==0.1.2

nornir-netmiko==0.1.1

nornir-utils==0.1.2

napalm==3.3.1

netmiko==3.4.0

nornir-jinja2==0.2.0

scrapli==2021.7.30

ipdb==0.13.9

ncclient==0.6.9

文件配置:

nornir.py

from nornir import InitNornir

from nornir_napalm.plugins.tasks import napalm_get

from nornir_utils.plugins.functions import print_result

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

results = nr.run(task=napalm_get, getters=["facts", "interfaces"])

print_result(results)

config.yaml

---

inventory:

    plugin: SimpleInventory

    options:

        host_file: "inventory/hosts.yaml"

        group_file: "inventory/groups.yaml"

        defaults_file: "inventory/defaults.yaml"

runner:

    plugin: threaded

    options:

        num_workers: 100

Directory:
        inventory
       

hosts.yaml

---

netdevops01:

    hostname: 192.168.2.202

    username: admin

    password: Admin_1234!

    port:8181

    platform: cisco_nxos

    connection_options:

        napalm:

            extras:

                optional_args:

                     secret: admin1234!

        netmiko:

            extras:

                conn_timeout: 60

                timeout: 180

                session_log:sbx_nxos-mgmt.cisco.com.log

    groups:

        - bj_leaf

        - netdevops

        - ios

    data:

        role: leaf

        tags:

           - test

           - netdevops

           - ios

        cmds:

           - show version

   

sw1:

    hostname: 192.168.2.11

    username: python

    password: '123'

    platform: ios

sw2:

    hostname: 192.168.2.12

    username: python

    password: '123'

    platform: ios

sw3:

    hostname: 192.168.2.13

    username: python

    password: '123'

    platform: ios

sw4:

    hostname: 192.168.2.14

    username: python

    password: '123'

    platform: ios

sw5:

    hostname: 192.168.2.15

    username: python

    password: '123'

    platform: ios

groups.yaml (可以为空)

defaults.yaml (可以为空)

 

运行结果:

 

引用:

        Nornir官方文档 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值