作者:董帅岭
# 一、前言
# 二、模块说明
# 三、源码说明
```
#!/usr/bin/python3
# -*- coding:utf-8 -*-
import os
import time
from netmiko import ConnectHandler # netmiko模块,专门支持路由交换设备
now = time.strftime("%Y%m%d",time.localtime(time.time()))
log_time = time.strftime("%Y-%m-%d_%H-%M-%S",time.localtime()) #格式化时间
cu_path = os.getcwd() # 获取当前运行目录
# 当前路径下创建目录的函数
def mkdir(path):
import os
path = path.strip()
print(path)
path = path.rstrip("\\")
isExists = os.path.exists(path)
if not isExists:
os.makedirs(path)
return True
else:
return False
# 读取需要巡检的IP列表信息,需要特定的格式
with open(os.getcwd()+'\\config\\IP_list.ini') as file:
file = file.readlines() #读取文件的所有信息
format_file = [] #创建空列表
for line in file: # 循环每一行的信息,赋值给变量line
imgfile = line.strip() # 格式化行的内容,去除结尾的空格回车啥的
format_file.append(imgfile) #将格式化后的行信息添加至format_file的列表中
i = 0
for files in format_file:
sigle_file = str.split(files, ',') #获取ip_list.ini的行信息,然后格式化,取行信息按照“,”进行分割,并加入sigle_file列表中。
i = i + 1
# SW列表信息为netmiko模块需要的格式信息
SW = {
'device_type': sigle_file[4],
'username': sigle_file[2],
'ip': sigle_file[1],
'password': sigle_file[3]
}
#print(SW)
try: #防止报错程序停止运行
connect = ConnectHandler(**SW) #连接交换机,开始巡检啦!!!
# !注意此处执行一个配置命令,取消分屏操作。
print(log_time + ' Successfully connected to '+sigle_file[1])
# 下面都是巡检的命令,并将结果复制给变量
version = connect.send_command('display version')
device = connect.send_command('display device')
clock = connect.send_command('display clock')
cpu = connect.send_command('display cpu-usage')
memory = connect.send_command('display memory')
fan = connect.send_command('display fan')
power = connect.send_command('display device power')
environment = connect.send_command('display environment')
users = connect.send_command('display users')
acl = connect.send_command('display acl all')
routing_table = connect.send_command('display ip routing-table')
arp = connect.send_command('display arp')
mac = connect.send_command('display mac-address')
interface_brief = connect.send_command('display interface brief')
ip_interface_brief = connect.send_command('display ip interface brief')
stack_con = connect.send_command('display stack configuration')
stack_top = connect.send_command('display stack topology')
current_configuration = connect.send_command('display current-configuration')
trapbuffer = connect.send_command('display trapbuffer')
logbuffer = connect.send_command('display logbuffer')
# 往log文件里面写入记录信息
mkdir(cu_path + "\\log") # 当前目录下创建log,存放生成的log文件
# 定义生成log文件的名字和格式
fo = open(cu_path + '\\log\\Py_'+sigle_file[0]+'_'+sigle_file[1]+'_' + log_time + '.log', 'w',encoding="utf-8")
fo.write(sigle_file[0]) #文档第一行写入 设备用途
fo.write('\r\n')
fo.write(sigle_file[1]) # 文档第二行写入,设备的IP地址
fo.write('\r\n')
fo.write(log_time) #写入log生成的时间
fo.write('\r\n')
fo.write('\r\n')
fo.write('\r\n')
fo.write("display version####") #为了后边操作log生成word使用的分割符
fo.write('\r\n')
fo.write(version) # 将巡检出来的内容复制给变量,然后写入log文件中
fo.write('\r\n')
fo.write('\r\n')
fo.write("display device####")
fo.write('\r\n')
fo.write(device)
fo.write('\r\n')
fo.write('\r\n')
fo.write("display clock####")
fo.write('\r\n')
fo.write(clock)
fo.write('\r\n')
fo.write('\r\n')
fo.write("display cpu-usage####")
fo.write('\r\n')
fo.write(cpu)
fo.write('\r\n')
fo.write('\r\n')
fo.write("display memory####")
fo.write('\r\n')
fo.write(memory)
fo.write('\r\n')
fo.write('\r\n')
fo.write("display fan####")
fo.write('\r\n')
fo.write(fan)
fo.write('\r\n')
fo.write('\r\n')
fo.write("display device power####")
fo.write('\r\n')
fo.write(power)
fo.write('\r\n')
fo.write('\r\n')
fo.write("display environment####")
fo.write('\r\n')
fo.write(environment)
fo.write('\r\n')
fo.write('\r\n')
fo.write("display users####")
fo.write('\r\n')
fo.write(users)
fo.write('\r\n')
fo.write('\r\n')
fo.write("display acl all####")
fo.write('\r\n')
fo.write(users)
fo.write('\r\n')
fo.write('\r\n')
fo.write("display ip routing-table####")
fo.write('\r\n')
fo.write(routing_table)
fo.write('\r\n')
fo.write('\r\n')
fo.write("display arp####")
fo.write('\r\n')
fo.write(arp)
fo.write('\r\n')
fo.write('\r\n')
fo.write("display mac####")
fo.write('\r\n')
fo.write(mac)
fo.write('\r\n')
fo.write('\r\n')
fo.write("display interface brief####")
fo.write('\r\n')
fo.write(interface_brief)
fo.write('\r\n')
fo.write('\r\n')
fo.write("display ip interface brief####")
fo.write('\r\n')
fo.write(ip_interface_brief)
fo.write('\r\n')
fo.write('\r\n')
fo.write("display stack configuration####")
fo.write('\r\n')
fo.write(stack_con)
fo.write('\r\n')
fo.write('\r\n')
fo.write("display stack topology####")
fo.write('\r\n')
fo.write(stack_top)
fo.write('\r\n')
fo.write('\r\n')
fo.write("display current-configuration####")
fo.write('\r\n')
fo.write(current_configuration)
fo.write('\r\n')
fo.write('\r\n')
fo.write("display trapbuffer####")
fo.write('\r\n')
fo.write(trapbuffer)
fo.write('\r\n')
fo.write('\r\n')
fo.write("display logbuffer####")
fo.write('\r\n')
fo.write(logbuffer)
fo.write('\r\n')
fo.write('\r\n')
fo.write("巡检结束####") # 巡检结束标识位,为了操作log使用
fo.write('\r\n')
fo.close()
except Exception as e: # 配合try使用,处理报错信息的
print(e)
pass
continue #继续进行函数循环,直到结束
```