我正试图为我正在使用Python和Netmiko库登录思科交换机并清除端口sec的程序提供一个例外。我想提示用户输入IP地址,这样我就可以让他们知道IP地址是坏的(用户不熟悉登录cisco交换机和使用命令)。我看到了两个例外,并尝试为两者创建一个例外规则,也尝试为每个例外创建一个例外规则。当交换机的IP坏了(不存在),就会出现例外。*比;回溯(最近一次调用):
输出下面是我的代码:文件"C:Usersjesse.garciaAnaconda3libsite-packagesnetmikobase_connection.py",第1046行,在establish_connection .py"self.remote_conn_pre.connect (* * ssh_connect_params)文件"C:Usersjesse.garciaAnaconda3libsite-packagesparamikoclient.py",第340行,在连接To_try = list(self。_families_and_addresses(主机名、端口)文件"C:Usersjess .garciaAnaconda3libsite-packagesparamikoclient.py",第203行,在_families_and_addressesaddrinfo = socket.getaddrinfo(文件"C:Usersjesse.garciaAnaconda3libsocket.py",第954行,在getaddrinfo对于_socket中的res。Getaddrinfo(主机,端口,家族,类型,原型,标志):套接字。gaierror: [Errno 11001] getaddrinfo failed
在处理上述异常时,出现了另一个异常:
Traceback(最近一次调用):用户文件"C: 杰西。garciaDocumentsGitHubpythonPractice OOP Practice .py",第18行,在
net_connect = ConnectHandler(**cisco_881)文件"C:Usersjesse.garciaAnaconda3libsite-packagesnetmikossh_dispatcher.py",第365行,在ConnectHandler返回ConnectionClass(*args, **kwargs)文件"C:Usersjesse.garciaAnaconda3libsite-packagesnetmikobase_connection.py",第439行,在initself._open ()文件"C:Usersjesse.garciaAnaconda3libsite-packagesnetmikobase_connection.py",第444行,在_open中self.establish_connection ()文件"C:Usersjesse.garciaAnaconda3libsite-packagesnetmikobase_connection.py",第1068行,在establish_connection提高NetmikoTimeoutException(味精)netmiko.exceptions.NetmikoTimeoutException: TCP connection to device failed. 这个问题的常见原因有:主机名或IP地址不正确。TCP端口错误。中间防火墙阻止访问。
设备设置:cisco_ios +95:22*
from netmiko import ConnectHandler
import netmiko
import paramiko
import socket
ipadd = input("Please enter the IP address of the switch ")
port = input("Please enter the port ")
cisco_881 = {
'device_type': 'cisco_ios',
'host': ipadd,
'username': '',
'password': '',
'port' : 22, # optional, defaults to 22
'secret': '', # optional, defaults to ''
}
if __name__ == "__main__":
net_connect = ConnectHandler(**cisco_881)
try:
net_connect.send_command(f'clear port-security sticky int g1/0/{port}')
except (socket.gaierror, netmiko.exceptions.NetmikoTimeoutException):
print("Ip address is bad. ")
我想用我们的消息来处理异常,这样当我部署时,它将在while循环中显示异常消息,然后再次提示IP。
我也试着用这段代码看看解释器到底看到了什么。
except Exception as err:
exception_type = type(err).__name__
print(exception_type)
以及:
try:
# Some stuff
except ConnectionRefusedError as err:
print(f"Connection Refused: {err}")
except TimeoutError as err:
print(f"Connection Refused: {err}")
except Exception as err:
print(f"Oops! {err}")
到目前为止似乎没有任何效果。我只是继续得到如上所示的输出。
from pprint import pprint
from netmiko import ConnectHandler
if __name__ == "__main__":
ip_add = input("Please enter the IP address of the switch:-")
port = input("Please enter the port:-")
cisco_881 = {
'device_type': 'cisco_ios',
'host': ip_add,
'username': '',
'password': '',
'port' : 22, # optional, defaults to 22
'secret': '', # optional, defaults to ''
}
result = {}
command = f'clear port-security sticky int g1/0/{port}'
try:
with ConnectHandler(**cisco_881) as ssh:
print('enabling wait...')
ssh.enable()
output = ssh.send_command(command)
result[command] = output
except Exception:
print("Ip address is bad.")
pprint(result, width=120)
更多信息请参考文档:-https://pyneng.readthedocs.io/en/latest/book/18_ssh_telnet/netmiko.html