处理设备连接

    1. from nornir_utils.plugins.functions import print_result
    2. from nornir_napalm.plugins.tasks import napalm_get
    1. [2]:
    1. nr = InitNornir(config_file="handling_connections/config.yaml")
    2. r1 = nr.filter(name="rt01")
    3. r = r1.run(
    4. task=napalm_get,
    5. getters=["facts"]
    6. )
    1. [3]:
    1. napalm_get**********************************************************************
    2. * rt01 ** changed : False ******************************************************
    3. vvvv napalm_get ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
    4. { 'facts': { 'fqdn': 'Unknown',
    5. 'hostname': 'R1',
    6. 'interface_list': [],
    7. 'model': 'Unknown',
    8. 'os_version': 'Unknown',
    9. 'serial_number': [],
    10. 'uptime': -1,
    11. 'vendor': 'Huawei'}}
    12. ^^^^ END napalm_get ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    1. [4]:
    1. def task_manages_connection_manually(task):
    2. print(f"开始连接:{task.host.name}")
    3. task=napalm_get,
    4. getters=["facts"]
    5. )
    6. print(f"连接成功: {not r[0].failed}")
    7. task.host.close_connection("napalm")
    8. nr = InitNornir(config_file="handling_connections/config.yaml")
    9. rtr = nr.filter(name="rt01")
    10. r = rtr.run(
    11. task=task_manages_connection_manually,
    12. )
    1. 开始连接:rt01
    2. 连接成功: True
    1. print_result(r)
    1. task_manages_connection_manually************************************************
    2. * rt01 ** changed : False ******************************************************
    3. vvvv task_manages_connection_manually ** changed : False vvvvvvvvvvvvvvvvvvvvvvv INFO
    4. ---- napalm_get ** changed : False --------------------------------------------- INFO
    5. { 'facts': { 'fqdn': 'Unknown',
    6. 'hostname': 'R1',
    7. 'interface_list': [],
    8. 'model': 'Unknown',
    9. 'os_version': 'Unknown',
    10. 'serial_number': [],
    11. 'uptime': -1,
    12. 'vendor': 'Huawei'}}
    13. ^^^^ END task_manages_connection_manually ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    1. # %load handling_connections/inventory/test-hosts.yaml
    2. port: 22
    3. hostname: 192.168.56.20
    4. username: username
    5. password: password
    6. platform: ios
    7. connection_options:
    8. netmiko:
    9. port: 22
    10. hostname:
    11. username: user
    12. password: pass
    13. platform: ios
    14. extras:
    15. optional_args:
    16. secret: secret
    17. session_log: path/to/save_log
    18. napalm:
    19. platform: ios
    20. extras:
    21. optional_args:
    22. path: path/to/save_log
    23. dummy:
    24. hostname: dummy_from_host
    25. port:
    26. username:
    27. password:
    28. platform: