导入必要的库

import paramiko from ncclient import manager import time import datetime

定义交换机连接信息

ip = '192.168.1.1' username = 'admin' password = 'password'

定义文件路径

command_file = 'commands.txt' config_file = 'backup.cfg'

建立 SSH 连接

ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(ip, username=username, password=password)

定义函数执行命令并返回结果

def run_command(command): stdin, stdout, stderr = ssh.exec_command(command) result = stdout.read().decode('utf-8') return result

读取命令文件

with open(command_file, 'r') as f: commands = f.readlines()

定义类来封装设备状态监控功能

class SwitchMonitor: def init(self): self.power = None self.fan = None self.lacp = None self.cpu = None self.memory = None self.ospf_neighbor = None

# 获取电源状态
def get_power_status(self):
    output = run_command('display power')
    if 'Faulty' in output:
        self.power = 'faulty'
    else:
        self.power = 'normal'

# 获取风扇状态
def get_fan_status(self):
    output = run_command('display fan')
    if 'Faulty' in output:
        self.fan = 'faulty'
    else:
        self.fan = 'normal'

# 获取 LACP 状态
def get_lacp_status(self):
    output = run_command('display lacp statistics')
    if 'Link Aggregation Control Protocol' in output:
        self.lacp = 'normal'
    else:
        self.lacp = 'faulty'

# 获取 CPU 利用率
def get_cpu_utilization(self):
    output = run_command('display cpu-usage')
    self.cpu = output.split()[-1]

# 获取内存利用率
def get_memory_utilization(self):
    output = run_command('display memory-usage')
    self.memory = output.split()[-1]

# 获取 OSPF 邻居状态
def get_ospf_neighbor_status(self):
    output = run_command('display ospf peer')
    if 'FULL' in output:
        self.ospf_neighbor = 'normal'
    else:
        self.ospf_neighbor = 'faulty'

# 监控设备状态
def monitor(self):
    self.get_power_status()
    self.get_fan_status()
    self.get_lacp_status()
    self.get_cpu_utilization()
    self.get_memory_utilization()
    self.get_ospf_neighbor_status()

# 输出监控结果
def output_result(self):
    print('Power status: ' + self.power)
    print('Fan status: ' + self.fan)
    print('LACP status: ' + self.lacp)
    print('CPU utilization: ' + self.cpu + '%')
    print('Memory utilization: ' + self.memory + '%')
    print('OSPF neighbor status: ' + self.ospf_neighbor)

实例化 SwitchMonitor 类

monitor = SwitchMonitor()

每 5 分钟监控设备状态并输出结果

while True: monitor.monitor() monitor.output_result() time.sleep(300)

# 检查风扇状态是否异常并给出提示
if monitor.fan == 'faulty':
    print('Fan status is faulty!')

# 每 24 小时保存一次配置文件并备份到本地
if datetime.datetime.now().hour == 0:
    # 使用 NETCONF 协议配置设备日志主机
    with manager.connect(host=ip, port=830, username=username, password=password, hostkey_verify=False) as m:
        config = '''
        <config>
            <system>
                <log>
                    <host>
                        <host-name>10.1.60.2</host-name>
                    </host>
                </log>
            </system>
        </config>
        '''
        m.edit_config(target='running', config=config)

    # 保存配置文件并备份到本地
    run_command('save')
    transport = ssh.get_transport()
    sftp = paramiko.SFTPClient.from_transport(transport)
    sftp.get('/flash/' + config_file, config_file)
    print('Config file backup completed!')

关闭 SSH 连接

ssh.close()

使用 Python 脚本监控华为交换机状态并备份配置

原文地址: https://www.cveoy.top/t/topic/n2en 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录