码迷,mamicode.com
首页 > 编程语言 > 详细

初学python-获取网络设备配置文件接口IP信息

时间:2016-07-09 22:25:30      阅读:1303      评论:0      收藏:0      [点我收藏+]

标签:interface   配置文件   address   access   python   

#!/usr/bin/python

#coding=utf-8

import re,sys,csv

from string import replace


def Interfaceinfo(file_path,firms):

with open(file_path,‘r‘) as f:


filecontext = f.readlines()


if firms == ‘a10‘:

splitstring_keyword = ‘(^!)‘

firms_interface = ‘^interface (.+)‘

ip_keyword = ‘^ip address (.+)‘

desc_keyword = ‘^name (.+)‘

access_vlan_keyword = ‘^switchport access vlan (.+)‘

trunk_vlan_keyword = ‘^switchport trunk vlan (.+)‘

elif firms == ‘zxr10‘:

splitstring_keyword = ‘(^\$)‘

firms_interface = ‘^interface (.+)‘

ip_keyword = ‘^ip address (.+)‘

desc_keyword = ‘^description (.+)‘

access_vlan_keyword = ‘^switchport access vlan (.+)‘

trunk_vlan_keyword = ‘^switchport trunk vlan (.+)‘

elif firms == ‘zte‘ or firms == ‘ruijie‘:

splitstring_keyword = ‘(^!)‘

firms_interface = ‘^interface (.+)‘

ip_keyword = ‘^ip address (.+)‘

desc_keyword = ‘^description (.+)‘

access_vlan_keyword = ‘^switchport access vlan (.+)‘

trunk_vlan_keyword = ‘^switchport trunk vlan (.+)‘

elif firms == ‘hillstone‘:

splitstring_keyword = ‘(^exit)‘

firms_interface = ‘^interface (.+)‘

ip_keyword = ‘^ip address (.+)‘

desc_keyword = ‘^description (.+)‘

access_vlan_keyword = ‘^port access vlan (.+)‘

trunk_vlan_keyword = ‘^port trunk permit vlan (.+)‘

else:

splitstring_keyword = ‘(^#)‘

firms_interface = ‘^interface (.+)‘

ip_keyword = ‘^ip address (.+)‘

desc_keyword = ‘^description (.+)‘

access_vlan_keyword = ‘^port access vlan (.+)‘

trunk_vlan_keyword = ‘^port trunk permit vlan (.+)‘


‘‘‘

定义列表:分隔符行值

‘‘‘

splitstring_rowvalues = []


‘‘‘

循环文件内容,获得分隔符所在的行号

‘‘‘

for itemkey in xrange(0,len(filecontext)):

splitstring_rowvalue = re.findall(splitstring_keyword,filecontext[itemkey].strip())

if len(splitstring_rowvalue) != 0:

splitstring_rowvalues.append(itemkey)


‘‘‘

定义列表:接口名行值

‘‘‘

portname_rowvalues = []


‘‘‘

循环文件内容,获得接口名所在的行号

‘‘‘

for itemkey in xrange(0,len(filecontext)):

portname_value = re.findall(firms_interface,filecontext[itemkey].strip())

if len(portname_value) != 0:

portname_rowvalues.append(itemkey)


‘‘‘

定义列表:接口名所在行最近的分隔符的行值

例子:

interface Ten-GigabitEthernet2/8/0/30

port link-mode bridge

shutdown

#     <-- 此分割符行号

‘‘‘

portnameEND_rowvalues = []


‘‘‘

列表开始位置值初始化为0

‘‘‘

startkey = 0


‘‘‘

循环提取接口名所在行最近的分隔符的行号

‘‘‘

for p_itemkey in xrange(0,len(portname_rowvalues)):

for s_itemkey in xrange(startkey,len(splitstring_rowvalues)):

if portname_rowvalues[p_itemkey] < splitstring_rowvalues[s_itemkey]:

portnameEND_rowvalues.append(splitstring_rowvalues[s_itemkey])

startkey = s_itemkey

break


‘‘‘

定义列表:接口信息

‘‘‘

portinfo = []


‘‘‘

循环提取接口名称,IP地址,接口描述,trunk或access相关的vlan信息  ----开始处

‘‘‘

for itemkey01,itemkey02 in zip(portname_rowvalues,portnameEND_rowvalues):

‘‘‘

提取接口名称

‘‘‘

port_name = re.findall(firms_interface,filecontext[itemkey01].strip())

port_name = port_name[0].replace(‘ ‘,‘-‘)


‘‘‘

提取接口描述

‘‘‘

for itemkey in xrange(itemkey01,itemkey02):

port_desc = re.findall(desc_keyword,filecontext[itemkey].strip())

if len(port_desc) != 0:

port_desc = port_desc[0].replace(‘ ‘,‘-‘)

break

else:

port_desc = ‘none‘

‘‘‘

提取接口IP

‘‘‘

for itemkey in xrange(itemkey01,itemkey02):

ip_list = []

ipvalue = re.findall(ip_keyword,filecontext[itemkey].strip())

if len(ipvalue) != 0:

‘‘‘

子网掩码位数转换

‘‘‘

ip_netmask = lambda netmask:sum(bin(int(i)).count(‘1‘) \

for i in netmask.split(‘.‘))


ip_list = ipvalue[0].replace(‘ ‘,‘/‘).split(‘/‘)

ip_list[1] = str(ip_netmask(ip_list[1]))

port_ip = ‘/‘.join(ip_list)

break

else:

port_ip = ‘none‘

‘‘‘

提取接口Trunk_VLAN

‘‘‘

trunk_vlan = []

for itemkey in xrange(itemkey01,itemkey02):

port_vlan = re.findall(trunk_vlan_keyword,filecontext[itemkey].strip())

if len(port_vlan) != 0:

port_vlan = port_vlan[0].replace(‘ ‘,‘-‘)

trunk_vlan.append(port_vlan)

‘‘‘

提取接口Access_VLAN

‘‘‘

for itemkey in xrange(itemkey01,itemkey02):

port_vlan = re.findall(access_vlan_keyword,filecontext[itemkey].strip())

if len(port_vlan) != 0:

port_vlan = ‘access‘ + ‘-‘ + port_vlan[0].replace(‘ ‘,‘-‘)

break

else:

port_vlan = ‘none‘

‘‘‘

将接口信息拼凑

‘‘‘

if len(trunk_vlan) != 0:

trunk_vlan = ‘trunk‘ + ‘-‘ + reduce(lambda x,y:x + ‘-‘ + y,trunk_vlan)

portinfo.append(port_name + ‘ ‘ + port_ip + ‘ ‘ + trunk_vlan + ‘ ‘+ port_desc)

else:

portinfo.append(port_name + ‘ ‘ + port_ip + ‘ ‘ + port_vlan + ‘ ‘ + port_desc)

‘‘‘

输出结果到csv文件

‘‘‘

headColContext = re.split(‘[/.]‘,file_path)[-2]

csv_file_path = ‘/root/log/Interfaceinfo.csv‘

with open(csv_file_path,‘ab‘) as cf:

cfwriter = csv.writer(cf,dialect=‘excel‘)

for p in portinfo:

cfwriter.writerow((headColContext + ‘ ‘ + p).split(‘ ‘))


if __name__ == ‘__main__‘:

Interfaceinfo(sys.argv[1],sys.argv[2])


初学python-获取网络设备配置文件接口IP信息

标签:interface   配置文件   address   access   python   

原文地址:http://ryanhb.blog.51cto.com/2839728/1813845

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!