简介
在现在这个网络越来越大的情况下,我们管理调式设备手工方式已经显得效率低下,Python自动化运维能够很好的帮助我们把一些单调的命令批量进行配置,还可以在巡检的时候提取我们需要的信息。
介绍
下面是一个Python检查设备相关信息导出在excel上的小实验,运用netmiko库和re库实现,而在网络自动化配置中,有netmiko和paramiko两种,相较于paramiko,netmiko将很多细节优化和简化,比如不需要导入time模块做休眠,输入每条命令不需要在后面加换行符\n,不需要执行config term,exit,end等命令,提取、打印回显内容更方便,比如说paramiko就是手动挡的汽车,那netmiko就是自动挡汽车。
拓补:
交换机命令:
#
vlan batch 10
#
interface Vlanif10
ip address 192.168.56.10 255.255.255.0
#
aaa
local-user hyl password cipher huawei@123
local-user hyl privilege level 15
local-user hyl service-type ssh
#
interface GigabitEthernet0/0/1
port link-type access
port default vlan 10
#
stelnet server enable
ssh user hyl
ssh user hyl authentication-type password
ssh user hyl service-type stelnet
#
user-interface con 0
user-interface vty 0 4
authentication-mode aaa
protocol inbound ssh
配置完后,打开控制台,ping交换机,确保交换机和云是能通的。
在匹配所需的信息时,要用到正则表达式,推荐菜鸟的正则表达式测试工具,挺好用的,正则表达式看不懂的可以去看看,里面有表达式符号的解释,这里就不做多的阐述了。
代码示例:
import netmiko
import re
import xlwt
def main():
ip_list = open('ip_list.txt', 'r') # 打开所存的ip文本
ip_addr = ip_list.readlines() # 对ip文本进行每一行读取,并存到列表
ip_list.close()
# cmd_list = ['display version', 'display device', 'display interface']
# 两种存放命令方式
cmd_list = open('cmd_list.txt', 'r')
command_list = cmd_list.readlines()
cmd_list.close()
workbook = xlwt.Workbook(encoding='utf-8') # 新建excel
worksheet = workbook.add_sheet('巡检') # 创建工作表,在excel表底部可以看到
worksheet.write(0, 0, label="交换机IP")
worksheet.write(0, 1, label="设备版本")
worksheet.write(0, 2, label="设备状态")
worksheet.write(0, 3, label="MAC地址")
row = 0 # 初始化表行0
column = 0 # 初始化表列0
for ip in ip_addr:
SW = {
'device_type': 'huawei',
'ip': ip,
'username': 'hyl',
'password': 'huawei@123',
}
connect = netmiko.ConnectHandler(**SW) # 连接交换机
print("login...... 地址为" + ip + "的设备连接成功")
row = row + 1
column = 0
worksheet.write(row, column, label=ip)
for cmd in command_list:
# 如果要用这个cmd_list = ['dis version', 'dis device', 'dis mac-address'],把command_list改成cmd_list
output = connect.send_command(cmd) # 输送命令
match_string = []
if 'VRP (R) software' in output:
match_string = '\(\w*\d\d.*\)'
version = re.search(match_string, output)
column = column + 1 # 表列加一,在设备版本的标题下进行写入
worksheet.write(row, column, label=version.group()) # 写入excel表,(行,列,值)
print(version.group())
output = ''
if 'Device ' in output:
if 'Abnormal' in output:
column = column + 1
worksheet.write(row, column, label='Abnormal')
print("Abnormal")
elif 'WrongType' in output:
column = column + 1
worksheet.write(row, column, label='WrongType')
print("WrongType")
elif 'Unregistered' in output:
column = column + 1
worksheet.write(row, column, label='Unregistered')
print("Unregistered")
elif 'Off' in output:
column = column + 1
worksheet.write(row, column, label='Off')
print("Off")
elif 'Offline' in output:
column = column + 1
worksheet.write(row, column, label='Offline')
print("Offline")
else:
column = column + 1
worksheet.write(row, column, label='Normal')
print("Normal")
output = ''
if 'Hardware address' in output: # 匹配本机mac地址
match_string = '([a-f,0-9,A-F]{4}-){2}[a-f,0-9,A-F]{4}'
mac_address = re.search(match_string, output)
column = column + 1
worksheet.write(row, column, label=mac_address.group())
print(mac_address.group())
output = ''
workbook.save('设备巡检表.xls') # 保存excel表,命名为“设备巡检表”
if __name__ == '__main__':
main()
cmd_list.txt是存放命令的文本文件,内容如下:
ip_list.txt是存放交换机ip地址的文本文件,内容如下:
cmd_list.xt、ip_list.txt必须和.py文件处于同一目录下。
运行代码结果:
导出的excel表: