用于网络工程师的Python - 路由器配置解析

时间:2016-08-10 12:59:26

标签: python cisco

我可以在解析我创建列表的配置文件时使用一些帮助,其中每个列表具有以下格式;从配置文件(如下所示)。我试图让我的脚本读取配置文件并列出接口详细信息,为主机名后面的每个接口。

PE1-Loopback0-1.1.1.1 255.255.255.255
PE1-GigabitEthernet1.10-205.1.1.1 255.255.255.0
PE1-GigabitEthernet1.999-10.10.1.1 255.255.255.0
PE2-Loopback0-2.2.2.2 255.255.255.255
PE2-GigabitEthernet1.10-205.1.1.2 255.255.255.0
PE2-GigabitEthernet1.999-10.10.1.2 255.255.255.0

我的脚本如下所示:

import random, re, pprint
from collections import defaultdict

routerconfig = open('C:/Users/adrian/workspace/Learning Python/configfile.txt', 'r')

for line1 in iter(routerconfig): #for loop 1.  Pulls out host name
    HostNameRGX = re.search(r'hostname .*', line1)

    if HostNameRGX:
        HostNameGRP = HostNameRGX.group()
        HostName = (HostNameGRP[9:])

        for line2 in iter(routerconfig): #for loop 2.  finds interface details
            IPAddressRGX = re.search(r'[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*.*255\.255.*',line2)
            InterfaceRGX = re.search(r'Loop.*|Giga.*',line2)

            if InterfaceRGX:
                    Interface=InterfaceRGX.group()

            if IPAddressRGX:
                IPAddress = IPAddressRGX.group()
                InterfacePair = (Interface + '-' + IPAddress)
                print(HostName + '-' + InterfacePair) 

routerconfig.close()

从我的脚本输出:

PE1-Loopback0-1.1.1.1 255.255.255.255
PE1-GigabitEthernet1.10-205.1.1.1 255.255.255.0
PE1-GigabitEthernet1.999-10.10.1.1 255.255.255.0
PE1-Loopback0-2.2.2.2 255.255.255.255
PE1-GigabitEthernet1.10-205.1.1.2 255.255.255.0
PE1-GigabitEthernet1.999-10.10.1.2 255.255.255.0

我想我知道为什么我的输出看起来像这样。第一个for循环(for循环1)运行主机名的正则表达式,然后继续第二个for循环(for循环2);这是我认为我的问题所在。当第一个循环继续报告PE1时,第二个循环继续解析我的正则表达式参数下的配置文件。我希望第二个循环在看到另一个主机名条目时结束,所以我的脚本可以分析PE2下的接口详细信息的配置,与PE1分开。

结果应如下所示:

PE1-Loopback0-1.1.1.1 255.255.255.255
PE1-GigabitEthernet1.10-205.1.1.1 255.255.255.0
PE1-GigabitEthernet1.999-10.10.1.1 255.255.255.0
PE2-Loopback0-2.2.2.2 255.255.255.255
PE2-GigabitEthernet1.10-205.1.1.2 255.255.255.0
PE2-GigabitEthernet1.999-10.10.1.2 255.255.255.0

配置文件:

   hostname PE1
!
!
interface Loopback0
 ip address 1.1.1.1 255.255.255.255
!
interface Tunnel999
 ip unnumbered Loopback0
 mpls ip
 mpls label protocol ldp
 tunnel mode mpls traffic-eng
 tunnel destination 2.2.2.2
 tunnel mpls traffic-eng autoroute announce
 tunnel mpls traffic-eng path-option 10 dynamic
 tunnel mpls traffic-eng path-selection metric te
 tunnel mpls traffic-eng name PE1-TO-PE2
!
interface GigabitEthernet1
 no ip address
 negotiation auto
 ip rsvp bandwidth
!
interface GigabitEthernet1.10
 encapsulation dot1Q 10
 ip address 205.1.1.1 255.255.255.0
 mpls ip
 mpls label protocol ldp
 mpls traffic-eng tunnels
 mpls traffic-eng administrative-weight 100
 ip rsvp bandwidth 99
!
interface GigabitEthernet1.999
 encapsulation dot1Q 999
 ip address 10.10.1.1 255.255.255.0
!


hostname PE2
!
!
interface Loopback0
 ip address 2.2.2.2 255.255.255.255
!
interface Tunnel999
 ip unnumbered Loopback0
 mpls ip
 mpls label protocol ldp
 tunnel mode mpls traffic-eng
 tunnel destination 2.2.2.2
 tunnel mpls traffic-eng autoroute announce
 tunnel mpls traffic-eng path-option 10 dynamic
 tunnel mpls traffic-eng path-selection metric te
 tunnel mpls traffic-eng name PE1-TO-PE2
!
interface GigabitEthernet1
 no ip address
 negotiation auto
 ip rsvp bandwidth
!
interface GigabitEthernet1.10
 encapsulation dot1Q 10
 ip address 205.1.1.2 255.255.255.0
 mpls ip
 mpls label protocol ldp
 mpls traffic-eng tunnels
 mpls traffic-eng administrative-weight 100
 ip rsvp bandwidth 99
!
interface GigabitEthernet1.999
 encapsulation dot1Q 999
 ip address 10.10.1.2 255.255.255.0

1 个答案:

答案 0 :(得分:1)

<强> !!!解决!!!

'''
Created on Aug 9, 2016

@author: adrian
'''
import random, re, pprint
from collections import defaultdict
DOT1Q=''
Routers={}
routerconfig = open('configfile.txt', 'r')

for line1 in iter(routerconfig):
    line1 = line1.rstrip('\n')

    if re.match(r'hostname.*', line1):
        HostName = line1[9:]

    else:
        if re.search(r'interface (Loop.*|Giga.*\.[0-9].*)',line1):
            Interface = line1[9:]


        if re.search(r'encap.*dot1[qQ].*',line1):
            DOT1Q = '-' + line1[21:]


        if re.search(r'ip address.*[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*.*255\.255.*',line1):
            IPAddress = line1[12:]

            Final=Interface+'-'+IPAddress+DOT1Q
            Routers.setdefault(HostName, []).append(Final)
            print HostName+'-'+Interface+'-'+IPAddress+DOT1Q
            DOT1Q=''
print '\n\n'

routerconfig.close()
pprint.pprint(Routers),

<强>输出

PE1- Loopback0-1.1.1.1 255.255.255.255
PE1- GigabitEthernet1.10-205.1.1.1 255.255.255.0-10
PE1- GigabitEthernet1.999-10.10.1.1 255.255.255.0-999
PE2- Loopback0-2.2.2.2 255.255.255.255
PE2- GigabitEthernet1.10-205.1.1.2 255.255.255.0-10
PE2- GigabitEthernet1.999-10.10.1.2 255.255.255.0-999
PE3- Loopback0-3.3.3.3 255.255.255.255
PE3- GigabitEthernet1.10-205.1.1.3 255.255.255.0-10
PE3- GigabitEthernet1.999-10.10.1.3 255.255.255.0-999



{'PE1': [' Loopback0-1.1.1.1 255.255.255.255',
         ' GigabitEthernet1.10-205.1.1.1 255.255.255.0-10',
         ' GigabitEthernet1.999-10.10.1.1 255.255.255.0-999'],
 'PE2': [' Loopback0-2.2.2.2 255.255.255.255',
         ' GigabitEthernet1.10-205.1.1.2 255.255.255.0-10',
         ' GigabitEthernet1.999-10.10.1.2 255.255.255.0-999'],
 'PE3': [' Loopback0-3.3.3.3 255.255.255.255',
         ' GigabitEthernet1.10-205.1.1.3 255.255.255.0-10',
         ' GigabitEthernet1.999-10.10.1.3 255.255.255.0-999']}