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

拓扑:

Nornir 获取cisco设备接口信息-LAB_第1张图片

 

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

创建: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 获取cisco设备接口信息-LAB_第2张图片

 

引用:

        Nornir官方文档 

你可能感兴趣的:(CSDN-网络Devops,p2p,网络协议,网络,python)