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

网络工程师用Python-路由器配置解析,python,cisco,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.2

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

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
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
我的脚本的输出:

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
   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
我想我知道为什么我的输出是这样的。第一个for循环(对于循环1)运行主机名的regex,然后继续到第二个for循环(对于循环2);这就是我的问题所在。当第一个循环继续报告PE1时,第二个循环继续解析我的regex参数下的配置文件。我希望第二个循环在看到另一个主机名条目时结束,这样我的脚本就可以分别从PE1解析PE2下接口详细信息的配置

结果应该如下所示:

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
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
配置文件:

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
   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

!!!解决了

'''
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']}

到目前为止有代码吗?@cricket_007,我还没有收到任何代码建议。我仍在尝试自己编写(未成功)。您正在编辑以显示预期的输出,这很好,但我们不会为您编写代码。你需要在这一部分做一些尝试,并解释什么不适合你。有关更多详细信息,请参阅。您的输出与您想要的完全相同。我遗漏了什么吗?@cricket_007,看看我的输出,这是我脚本的输出。循环返回所有接口的PE1。脚本应该解析主机名之间接口的配置。