部署了对应厂商的管理设备,通过SNMP协议推送拉取设备状态也可以获取到交换机信息,但是当设备的数量较多、又没有对应厂商的管理器如何应对
安装python
python下载
安装pycharm
pycharm下载
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)
终端操作:git clone https://github.com/shiyiwei7/Huawei-UP-Down-
图片为参考,网址参考👆
运行参照方法1、效果如方法1
参考华为的厂商NCE-Fabric,可视化展示某台设备端口 数目
1、NCE-Fabric分析器的信息采集依托于什么完成采集过程?
2、python进行巡检的边界在哪里?
3、能否利用python完成数台交换机配置?
4、能能否完成交换机巡检/备份操作?
Econnect开源工具帮助巡检