基于Python的企业网络设备自动化运维研究
作者: 侯阔 杨晓涛 赵鑫 杨佳琦 王昊 李亚明
摘要:文章针对现在企业网络的运维存在的问题,基于Python设计了三种方法实现了企业网络设备的自动化运维。同时,在运维过程中提取大量有价值的设备日志数据,为以后的智能运维云平台设计提供了数据基础。
关键词:Python;企业网;网络自动化运维
中图分类号:TP393 文献标识码:A
文章编号:1009-3044(2022)34-0071-03
1 问题描述
目前企业网络的现状是,企业信息系统中的网络设备厂商繁多,大多数设备已经过了维保期。建设时间太长,原始资料丢失等原因为目前企业的信息系统维护工作增加了难度[1]。传统的信息系统运维方式需要运维人员进行大量重复的运维工作,以手工形式管理硬件、软件资源[2]。流程较长,消耗成本较高,且容易出现各种人为失误。同时,人工巡检难以及时、准确地发现网络中潜在安全隐患[3]。运营商和大型IDC机房配备完整的监控系统,但是需要消耗大量的人力物力,中小型企业几乎没有配备独立的监控大厅和充足的维护人员,又因缺乏一个有效的监控手段[4],信息系统的隐患不能提前被发现,业务中断后也不能及时修复。所以企业迫切地需要一种轻型便捷的运维方式。
当前,国内的网络自动化运维发展尚处于初级阶段。运用Python或其他编程语言,设计一种基于命令的网络设备自动化运维工具,还没有做出一款包含前后端的运维云平台[5]。国外有一些基于Web界面的网络监控系统,还没有推出App的版本,对设备做巡检的数据也没有做进一步挖掘分析,没有将深度学习模型,智能算法与网络自动化运维相结合,实现网络运维的智能化[6]。想要开发出一款网络自动化运维软件,第一步就要能够实现在设备中自动提取中有价值的数据,后期可以基于数据挖掘开发出智能监控云平台。针对此问题,本文对网络自动化运维进行设计,可以实现网络设备自动化配置、网络设备的状态实时监控、自动化提取网络日志数据。
2 Python脚本编程设计
2.1 自动化配置设备代码设计
网络维护人员通常都是在凌晨0点至6点对网络进行割接升级操作,会极大地影响维护人员的工作状态和身体健康。可以提前写好设备的配置命令,并通过测试和双人审核,然后通过编写Python代码,设置任务计划,能够实现定时地自动远程登录设备,并进行设备的配置。代码如下:
import paramiko #加载模块
import time #加载模块
username='gfq' #用户
password='gfq123' #密码
ip = '192.168.20.3' #地址
def ensp(command, ssh=None):# 创建SSH对象
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 连接服务器
ssh.connect(hostname=ip,port=22,username=username,password=password)# 执行命令
print("进入网络设备",ip)
command=ssh.invoke_shell()# 激活terminal
command.send("sys\n")
command.send("dhcp enable\n")
command.send("dhcpv6 pool pooll\n")
command.send("address prefix 3000::/64\n")
command.send("excluded-address 3000::1\n")
command.send("dns-server 4000::1\n")
command.send("dns-domain-name huawie.com\n")
command.send("int g0/0/1\n")
command.send("ip add 192.168.30.5 24\n")
command.send("ipv6 enable\n")
command.send("ipv6 address 3000::1/64 \n")
command.send("dhcpv6 server pooll\n")
time.sleep(20) # 如果程序执行得太快,没有等到返回足够的信息,chan.recv(65535)不能得到想要的结果
outpit = command.recv(65535)
print(outpit.decode().strip())
ssh.close()
if __name__ =='__main__':
ensp("dis cu")
2.2 设备状态监测设计
目前的企业网故障处理流程是一种倒序的流程,当企业员工发现网络出了问题反馈到网络管理员,网络管理员才去排除网络故障,而且网络管理员不能精准地定位发生故障的时间,为处理故障带来很大的麻烦。通过编写Python代码,对所有网络设备进行循环ping测试,可以实时全年无休地对设备进行检测,监测设备是否因故障离网。代码如下:
#导入pythoning的模块
from pythonping import ping
#导入datetime模块来保存检查的日期
from datetime import datetime
import os,sys
while 1:
#获取当前的日期和具体时间
now = datetime.now()
date = "%s-%s-%s" % (now.year, now.month, now.day)
time = "%s-%s-%s" % (now.hour, now.minute, now.second)
#打开存放交换机IP地址的文件
ip_list = open("ip_list.txt", "r")
#使用追加读写的方式创建一个以日期命名的文件,方便每日检查
f = open("ping_result.txt", "a+")
#在文件中首先写入检查的具体时间
f.write("Time: " + "\n"+date +"\n"+ time + "\n")
#通过for循环遍历每台设备的IP地址
for line in ip_list:
ip = ip = line.strip()
ping_result = ping(ip)
#pythonping如果能够ping通,返回值内有Reply这个字符串,失败时则没有。可以通过这个不同来打印出最后的结果
if "Reply" in str(ping_result):
print(ip + " is reachable.")
f.write(ip + " is reachable." + "\n")
else:
print(ip + " is not reachable.")
# 将能够通信的IP地址写入文档中
f.write(ip + " is not reachable."+ "\n")
ip_list.close()
f.close()
2.3 自动化提取设备日志数据设计
想开发一个智能化的网络自动化运维平台,数据是必不可少的。平台通过对数据的阈值设置而发生报警,平台也可以基于网络设备的历史数据对系统的健康度做出一个预测,有了设备日志数据,还可以对设备做巡检的数据做进一步挖掘分析,将深度学习模型,智能算法与网络自动化运维相结合,实现网络运维的智能化。自动化提取设备日志代码如下:
import telnetlib
import time
import os
from datetime import datetime
host = "10.10.0.39"
username = "hktest"
password = "hk@123456"
command1 = "display temperature all"
command2="display cpu-usage"
command3="display memory-usage"
command4="display interface brief"
command5="display transceiver verbose"
now = datetime.now() # 获得当前时间
timestr = now.strftime("%Y_%m_%d_%H_%M_%S")
print('年_月_日_时_分_秒:', timestr)
dir = 'E:/shuju/data' + '/' + timestr # os.getcwd()获得当前执行目录
if os.path.exists(dir): # 看文件夹是否存在
print('文件夹已存在')
else: # 如果不存在
os.makedirs(dir) # 则创建文件夹
tn = telnetlib.Telnet(host)
tn.read_until(b'username:',timeout=8)
tn.write(username.encode('ascii')+b"\n")
tn.read_until(b"password:",timeout=8)
tn.write(password.encode('ascii')+b"\n")
time.sleep(2)
tn.write(command1.encode('ascii')+b"\n")
time.sleep(5)
v_result = tn.read_very_eager().decode('ascii')
file=open(dir+'\\temperature.txt',mode='w')
file.write(v_result)
print(v_result)
tn.write(command2.encode('ascii')+b"\n")