处理设备连接
from nornir_utils.plugins.functions import print_result
from nornir_napalm.plugins.tasks import napalm_get
[2]:
nr = InitNornir(config_file="handling_connections/config.yaml")
r1 = nr.filter(name="rt01")
r = r1.run(
task=napalm_get,
getters=["facts"]
)
[3]:
napalm_get**********************************************************************
* rt01 ** changed : False ******************************************************
vvvv napalm_get ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
{ 'facts': { 'fqdn': 'Unknown',
'hostname': 'R1',
'interface_list': [],
'model': 'Unknown',
'os_version': 'Unknown',
'serial_number': [],
'uptime': -1,
'vendor': 'Huawei'}}
^^^^ END napalm_get ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[4]:
def task_manages_connection_manually(task):
print(f"开始连接:{task.host.name}")
task=napalm_get,
getters=["facts"]
)
print(f"连接成功: {not r[0].failed}")
task.host.close_connection("napalm")
nr = InitNornir(config_file="handling_connections/config.yaml")
rtr = nr.filter(name="rt01")
r = rtr.run(
task=task_manages_connection_manually,
)
开始连接:rt01
连接成功: True
print_result(r)
task_manages_connection_manually************************************************
* rt01 ** changed : False ******************************************************
vvvv task_manages_connection_manually ** changed : False vvvvvvvvvvvvvvvvvvvvvvv INFO
{ 'facts': { 'fqdn': 'Unknown',
'hostname': 'R1',
'interface_list': [],
'model': 'Unknown',
'os_version': 'Unknown',
'serial_number': [],
'uptime': -1,
'vendor': 'Huawei'}}
^^^^ END task_manages_connection_manually ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
port: 22
hostname: 192.168.56.20
username: username
password: password
platform: ios
connection_options:
netmiko:
port: 22
hostname:
username: user
password: pass
platform: ios
extras:
optional_args:
secret: secret
session_log: path/to/save_log
napalm:
platform: ios
extras:
optional_args:
path: path/to/save_log
dummy:
hostname: dummy_from_host
port:
username:
password:
platform: