导入所需库

import paramiko from ncclient import manager import time import datetime

定义类

class HuaweiDevice: def init(self, ip, username, password): self.ip = ip self.username = username self.password = password self.client = None self.netconf_session = None

# 连接设备
def connect(self):
    self.client = paramiko.SSHClient()
    self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    self.client.connect(self.ip, username=self.username, password=self.password)

# 断开设备连接
def disconnect(self):
    if self.client:
        self.client.close()

# 执行命令
def execute_command(self, command):
    stdin, stdout, stderr = self.client.exec_command(command)
    return stdout.readlines()

# 获取设备状态
def get_device_status(self):
    # 获取power状态
    power_status = self.execute_command('display power')
    # 获取fan状态
    fan_status = self.execute_command('display fan')
    # 获取eth状态
    eth_status = self.execute_command('display eth')
    # 获取CPU状态
    cpu_status = self.execute_command('display CPU')
    # 获取memory状态
    memory_status = self.execute_command('display memory')
    # 获取OSPF peer状态
    ospf_peer_status = self.execute_command('display OSPF peer b')
    # 返回状态结果
    return {
        'power': power_status,
        'fan': fan_status,
        'eth': eth_status,
        'CPU': cpu_status,
        'memory': memory_status,
        'OSPF_peer': ospf_peer_status
    }

# 与设备建立NETCONF连接
def connect_netconf(self):
    self.netconf_session = manager.connect(host=self.ip, port=830, username=self.username, password=self.password, hostkey_verify=False)

# 断开NETCONF连接
def disconnect_netconf(self):
    if self.netconf_session:
        self.netconf_session.close_session()

# 配置设备日志主机
def configure_logging(self):
    # 定义配置命令
    config_xml = '''
    <config>
        <logging xmlns='http://www.huawei.com/netconf/vrp' content-version='1.0.0'>
            <hosts>
                <host>
                    <name>10.1.60.2</name>
                    <severity>debugging</severity>
                </host>
            </hosts>
        </logging>
    </config>
    '''
    # 发送配置命令
    self.netconf_session.edit_config(config_xml, target='running')

定义函数:从文件中读取命令并执行

def execute_commands_from_file(file_path): with open(file_path, 'r') as f: commands = f.readlines() for command in commands: device.execute_command(command)

定义函数:分析设备状态监控结果

def analyze_device_status(status): if 'faulty' in status['fan']: print('Fan状态异常!') if 'faulty' in status['power']: print('Power状态异常!')

定义函数:备份配置文件

def backup_config_file(): # 获取当前时间 now = datetime.datetime.now() # 定义文件名 file_name = f'{device.ip}_{now.strftime('%Y%m%d')}.cfg' # 获取配置文件 config = device.execute_command('display current-configuration') # 写入文件 with open(file_name, 'w') as f: f.writelines(config) # 使用sftp传输协议将配置文件备份在电脑本地 transport = paramiko.Transport((device.ip, 22)) transport.connect(username=device.username, password=device.password) sftp = paramiko.SFTPClient.from_transport(transport) sftp.put(file_name, file_name) sftp.close()

初始化设备

device = HuaweiDevice('192.168.1.1', 'admin', 'password')

连接设备

device.connect()

从文件中读取命令并执行

execute_commands_from_file('commands.txt')

获取设备状态

device_status = device.get_device_status()

分析设备状态监控结果

analyze_device_status(device_status)

每24h备份一次配置文件

while True: backup_config_file() time.sleep(86400)

打开设备NETCONF功能并配置设备日志主机

device.connect_netconf() device.configure_logging() device.disconnect_netconf()

断开设备连接

device.disconnect()

华为设备监控脚本:使用 Python 实现状态监测和配置备份

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

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