python的异常处理批量执行网络设备的巡检命令

前言

在网络设备数量超过千台甚至上万台的大型企业网中,难免会遇到某些设备的管理IP地址不通,SSH连接失败的情况,设备数量越多,这种情况发生的概率越高。
这个时候如果你想用python批量配置所有的设备,就一定要注意这种情况,很可能你的脚本运行了还不到一半就因为中间某一个连接不通的设备而停止了。

比如你有5000台交换机需要统一更改本地用户名和密码,前500台交换机因为某个网络问题导致管理IP地址不可达,SSH连不上,此时python会返回错误,然后脚本就此停住!脚本不会再对剩下的4500台交换机配置,也就意味着”挂机“失败!

解决这些问题我们可以使用python的异常处理来解决

云配置

请添加图片描述

云的配置是为了让主机能与交换机互相访问

环境

请添加图片描述

我们把LSW2用户名admin的密码从Huawei@123改为123,模拟一个用户名密码错误的环境。再把LSW3的接口给shutdown,模拟一个IP不可达的环境。我们使用python的异常处理来SSH远程设备进行配置,遇到错误时不中断python程序

交换机SSH配置

aaa
 local-user admin password cipher Huawei@123   //创建python用户,密码为123
 local-user admin privilege level 15
 local-user admin service-type ssh
#
user-interface vty 0 4
 authentication-mode aaa
 protocol inbound ssh
#
stelnet server enable
ssh user admin authentication-type all
ssh user admin service-type all
ssh client first-time enable

这个时候我们就能与交换机互访,并SSH登陆了

目的

批量连接设备并查看dis clock,就算遇到错误也继续程序,直止结束。把错误的设备地址记录下来

代码

import paramiko
import time
import socket

username = input('Userame: ')  # 获取用户名输入
password = input('Password: ')  # 获取密码输入

switch_with_authentication_issue = []  # 记录身份验证失败的交换机IP列表
switch_not_reachable = []  # 记录无法连接的交换机IP列表

iplist = open("ip_list.txt", "r")  # 打开存储IP地址清单的文件
for line in iplist.readlines():  # 逐行读取文件中的IP地址
    try:
        ip = line.strip()  # 获取IP地址
        ssh_client = paramiko.SSHClient()  # 创建SSH客户端实例
        ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())  # 设置自动添加主机密钥
        ssh_client.connect(hostname=ip, username=username, password=password, look_for_keys=False)  # 连接SSH客户端到交换机
        print("You have successfully connect to ", ip)  # 打印连接成功的消息
        command = ssh_client.invoke_shell()  # 开启交互式Shell
        command.send(b"screen-length 0 temporary\n")  # 发送命令,设置交换机临时显示长度
        command.send(b"sys\n")  # 发送进入系统视图的命令
        command.send(b"dis clock\n")  # 发送显示交换机时钟的命令
        time.sleep(2)  # 等待2秒,确保交互命令执行完成

        output = command.recv(65535)  # 接收命令的输出结果
        print(output.decode('ascii'))  # 打印输出结果

    except paramiko.ssh_exception.AuthenticationException:
        print("User authentication failed for " + ip + ".")  # 打印身份验证失败的消息
        switch_with_authentication_issue.append(ip)  # 将身份验证失败的IP地址添加到列表中

    except socket.error:
        print(ip + " is not reachable.")  # 打印无法连接的消息
        switch_not_reachable.append(ip)  # 将无法连接的IP地址添加到列表中

iplist.close()  # 关闭文件

print('\nUser authentication failed for below switches: ')
for i in switch_with_authentication_issue:
    print(i)  # 打印身份验证失败的交换机IP列表

print('\nBelow switches are not reachable: ')
for i in switch_not_reachable:
    print(i)  # 打印无法连接的交换机IP列表

结果

请添加图片描述

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

datacom_chen

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

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

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

打赏作者

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

抵扣说明:

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

余额充值