华为设备监控脚本 - 使用 Python 和 paramiko/ncclient 实现
导入所需库
import paramiko from ncclient import manager import time import datetime
定义交换机类
class Switch: def init(self, ip, username, password): self.ip = ip self.username = username self.password = password self.ssh = paramiko.SSHClient() self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.ssh.connect(self.ip, username=self.username, password=self.password)
def run_command(self, command):
stdin, stdout, stderr = self.ssh.exec_command(command)
return stdout.readlines()
def display_power(self):
power_status = self.run_command('display power')
return power_status
def display_fan(self):
fan_status = self.run_command('display fan')
return fan_status
def display_eth(self):
eth_status = self.run_command('display eth')
return eth_status
def display_CPU(self):
CPU_status = self.run_command('display CPU')
return CPU_status
def display_memory(self):
memory_status = self.run_command('display memory')
return memory_status
def display_OSPF_peer(self):
OSPF_peer_status = self.run_command('display OSPF peer b')
return OSPF_peer_status
def close_ssh(self):
self.ssh.close()
定义监控类
class Monitor: def init(self, switches): self.switches = switches
def check_status(self):
for switch in self.switches:
power_status = switch.display_power()
fan_status = switch.display_fan()
eth_status = switch.display_eth()
CPU_status = switch.display_CPU()
memory_status = switch.display_memory()
OSPF_peer_status = switch.display_OSPF_peer()
# 分析监控结果
if 'faulty' in fan_status:
print('Fan on switch {} is faulty!'.format(switch.ip))
else:
print('Fan on switch {} is OK.'.format(switch.ip))
# 输出监控结果
print('Power status on switch {}: {}'.format(switch.ip, power_status))
print('Fan status on switch {}: {}'.format(switch.ip, fan_status))
print('Ethernet status on switch {}: {}'.format(switch.ip, eth_status))
print('CPU status on switch {}: {}'.format(switch.ip, CPU_status))
print('Memory status on switch {}: {}'.format(switch.ip, memory_status))
print('OSPF peer status on switch {}: {}'.format(switch.ip, OSPF_peer_status))
def backup_config(self):
# 每24h备份一次配置文件
while True:
current_time = datetime.datetime.now()
if current_time.hour == 0 and current_time.minute == 0:
for switch in self.switches:
# 建立SFTP连接
transport = paramiko.Transport((switch.ip, 22))
transport.connect(username=switch.username, password=switch.password)
sftp = paramiko.SFTPClient.from_transport(transport)
# 备份配置文件到本地
local_path = 'config_backup_{}.cfg'.format(switch.ip)
remote_path = 'flash:/config.cfg'
sftp.get(remote_path, local_path)
# 关闭连接
sftp.close()
transport.close()
time.sleep(60)
def configure_netconf(self):
for switch in self.switches:
# 建立NETCONF连接
with manager.connect(host=switch.ip, port=830, username=switch.username, password=switch.password, hostkey_verify=False) as m:
# 配置设备日志主机为10.1.60.2
netconf_config = '''
<config>
<system>
<log-host>
<host>10.1.60.2</host>
</log-host>
</system>
</config>
'''
m.edit_config(target='running', config=netconf_config)
主程序
if name == 'main': # 定义交换机对象 switch1 = Switch('192.168.1.1', 'admin', 'password') switch2 = Switch('192.168.1.2', 'admin', 'password') switches = [switch1, switch2]
# 定义监控对象
monitor = Monitor(switches)
# 监控关键运行状态并输出结果
monitor.check_status()
# 备份配置文件
monitor.backup_config()
# 配置NETCONF
monitor.configure_netconf()
# 关闭SSH连接
for switch in switches:
switch.close_ssh()
原文地址: https://www.cveoy.top/t/topic/n35U 著作权归作者所有。请勿转载和采集!