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

2024-09-29 21:26:42 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我的脚本如下:

^{pr2}$

我的脚本输出:

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循环(forloop1)为hostname运行一个regex,然后继续执行第二个for循环(forloop2);我认为这就是我的问题所在。第一个循环继续报告PE1,第二个循环继续解析regex参数下的配置文件。我希望第二个循环在看到另一个主机名条目时结束,这样我的脚本就可以解析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

Tags: ip脚本address配置文件protocolenglabelinterface
1条回答
网友
1楼 · 发布于 2024-09-29 21:26:42

!!!解决了的!!!

'''
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),

输出

^{pr2}$

相关问题 更多 >

    热门问题