实验九 netmiko 异常

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',
                # 'conn_timeout':10
            }
            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)
# 公众号释然IT杂谈
print('\n ====结果输出====')
print('·下列交换机⽤户验证失败:')
for i in switch_with_authentication_issue:
    print(f" {i}")
# 公众号:释然IT杂谈
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

期待未来的男孩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值