0

我是网络自动化的新手,并尝试使用 pyntc 连接到设备。我不确定如何正确编写代码来处理 ssh 超时或身份验证错误等。

我的代码如下:

from pyntc import ntc_device as NTC
from pyntc_devices_list import Get_Devices_List

all_devices = Get_Devices_List()

for device in all_devices:
        print('Backing up ' + device['name'])
        DEVICE = NTC(host=device['ip'], username=device['username'], password=device['password'], device_type='cisco_ios$
    try:
            DEVICE.open()
    except Exception:
            print('Error')
            continue

    back_config = DEVICE.backup_running_config(device['name'] + '.cfg')
    DEVICE.close()

我手动关闭了这个设备,然后我发现python代码被退出了:

root@Network-Automation:~/Pyntc# python3 pyntc_error_handling.py
Backing up ESW1
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/netmiko/base_connection.py", line 884, in establish_connection
    self.remote_conn_pre.connect(**ssh_connect_params)
  File "/usr/local/lib/python3.5/dist-packages/paramiko/client.py", line 368, in connect
    raise NoValidConnectionsError(errors)
paramiko.ssh_exception.NoValidConnectionsError: [Errno None] Unable to connect to port 22 on 192.168.122.72

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "pyntc_error_handling.py", line 8, in <module>
    DEVICE = NTC(host=device['ip'], username=device['username'], password=device['password'], device_type='cisco_ios_ssh')
  File "/usr/local/lib/python3.5/dist-packages/pyntc/__init__.py", line 38, in ntc_device
    return device_class(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/pyntc/devices/ios_device.py", line 42, in __init__
    self.open()
  File "/usr/local/lib/python3.5/dist-packages/pyntc/devices/ios_device.py", line 300, in open
    verbose=False,
  File "/usr/local/lib/python3.5/dist-packages/netmiko/ssh_dispatcher.py", line 246, in ConnectHandler
    return ConnectionClass(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/netmiko/base_connection.py", line 317, in __init__
    self._open()
  File "/usr/local/lib/python3.5/dist-packages/netmiko/base_connection.py", line 322, in _open
    self.establish_connection()
  File "/usr/local/lib/python3.5/dist-packages/netmiko/base_connection.py", line 890, in establish_connection
    raise NetMikoTimeoutException(msg)
netmiko.ssh_exception.NetMikoTimeoutException: Connection to device timed-out: cisco_ios 192.168.122.72:22

看起来有超过1个错误......

4

1 回答 1

0

你在这里犯了一个小错误,你期望一个错误而不是尝试相同的输出。如果出现错误,脚本将通过错误。试试这个方法。

from pyntc import ntc_device as NTC
from pyntc_devices_list import Get_Devices_List

all_devices = Get_Devices_List()
try:
    for device in all_devices:
        print('Backing up ' + device['name'])
        DEVICE = NTC(host=device['ip'], username=device['username'], password=device['password'], device_type='cisco_ios')
        DEVICE.open()
        back_config = DEVICE.backup_running_config(device['name'] + '.cfg')
        DEVICE.close()
except Exception as Err:
    print(Err)
于 2021-12-14T09:20:33.773 回答