import netmiko
from netmiko import ConnectHandler
switch_with_authentication_issue = []
switch_not_reachable = []
with open('ip_list.txt') as f:
for ips in f.readlines():
try:
ip = ips.strip()
connection_info = {
'device_type': 'huawei',
'ip': ip,
'username': 'python',
'password': '123asd',
}
with ConnectHandler(**connection_info) as conn:
print(f'已经成功登陆交换机{ip}')
output = conn.send_command('display cur | inc sysname')
print(output)
except netmiko.NetmikoAuthenticationException:
print(ip + "⽤户验证失败!")
switch_with_authentication_issue.append(ip)
except netmiko.exceptions.NetmikoTimeoutException:
print(ip + "⽬标不可达!")
switch_not_reachable.append(ip)
print('\n ====结果输出====')
print('·下列交换机⽤户验证失败:')
for i in switch_with_authentication_issue:
print(f" {i}")
print('·下列交换机不可达:')
for i in switch_not_reachable:
print(f" {i}")
C:\Users\82127\.conda\envs\pythonProject\python.exe E:/log/python/pythonProject/Demo09.py
192.168.1.9⽤户验证失败!
192.168.1.12⽬标不可达!
192.168.1.11⽤户验证失败!
====结果输出====
·下列交换机⽤户验证失败:
192.168.1.9
192.168.1.11
·下列交换机不可达:
192.168.1.12
进程已结束,退出代码0