Python助力华为设备接口状态查询

利用Python脚本检查交换机接口情况

一、背景

部署了对应厂商的管理设备,通过SNMP协议推送拉取设备状态也可以获取到交换机信息,但是当设备的数量较多、又没有对应厂商的管理器如何应对

二、方法

安装python
python下载

安装pycharm
pycharm下载

方法1

-导入项目(无git)

新建python空白文件


复制源码->修改参数

bash 复制代码
import paramiko   //注意组件安装
import time
import re


def count_interface_status(target_ip, target_username, target_password):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(target_ip, username=target_username, password=target_password)

    command = ssh.invoke_shell()
    command.send("screen-length 10 temporary\n")
    time.sleep(1)

    command.send("sys\n")
    time.sleep(1)
    output = command.recv(65535).decode()

    up_count = 0
    down_count = 0

    for i in range(1, 48):
        interface = f"G0/0/{i}"   //根据接口的类型选择G口或者10G、或者40G接口,当无法确定是否有效请在交换机上先display跟参数
        command.send(f"display interface {interface}\n")
        time.sleep(1)

        output = ""
        while True:
            page = command.recv(65535)
            page = page.decode("ASCII")
            output += page
            time.sleep(0.1)
            if page.endswith('>') or page.endswith(']'):
                break
            if "  ---- More ----" in page:
                command.send(" ")

        output = re.sub(r"  ---- More ----.*16D", "", output)

        if "Line protocol current state : UP" in output:
            up_count += 1
        elif "Line protocol current state : DOWN" in output:
            down_count += 1

    ssh.close()

    return up_count, down_count


# Replace with your actual IP address, username, and password
ip_addresses = [ "10.50.0.32", "10.50.0.33", "10.50.0.38", "10.50.0.40", "10.50.0.41"]  //地址需要参考具体需求更改
username = "xxxxxxx"        //账号需要根据具体情况修改
password = "Huawei@123"     //密码需要根据具体****

for ip in ip_addresses:
    up, down = count_interface_status(ip, username, password)
    print(f"IP: {ip}")
    print(f"处于'up'状态的接口数量:{up}")
    print(f"处于'down'状态的接口数量:{down}")
    print("-" * 20)

安装依赖组件(paramiko组件)

运行py脚本

结果展示:

方法2

克隆项目(有git)

Clone项目py文件至目录

终端操作:git clone https://github.com/shiyiwei7/Huawei-UP-Down-

图片为参考,网址参考👆

Readme文件包含操作解释,

运行参照方法1、效果如方法1

三、其他可视化方式

参考华为的厂商NCE-Fabric,可视化展示某台设备端口 数目

四、自动化思考

1、NCE-Fabric分析器的信息采集依托于什么完成采集过程?

2、python进行巡检的边界在哪里?

3、能否利用python完成数台交换机配置?

4、能能否完成交换机巡检/备份操作?
Econnect开源工具帮助巡检

相关推荐
WJ.Polar1 分钟前
Scapy基本应用
linux·运维·网络·python
maaath3 分钟前
【maaath】Flutter for OpenHarmony 乐器学习应用开发实战
flutter·华为·harmonyos
@insist12314 分钟前
信息安全工程师-入侵检测核心技术、APT 应对与工程实践
网络·安全·软考·信息安全工程师·软件水平考试
CSCN新手听安19 分钟前
【Qt】Qt窗口(六)QMessageBox消息对话框的使用
开发语言·c++·qt
H_unique21 分钟前
LangChain:调用工具Ⅲ
python·langchain
醉舞经阁半卷书11 小时前
深入掌握LangChain
python·langchain
CDN3601 小时前
[硬核] 你的DNS正在“裸奔”?用Python手撕DNS劫持与隧道检测逻辑
开发语言·网络·python
froginwe111 小时前
jQuery 添加元素
开发语言
zhangfeng11331 小时前
PHP 语法检查命令 php -l “$file“ > /dev/null 2>&1;
开发语言·php
csbysj20201 小时前
解释器模式
开发语言