2016-08-10 2 views
-1

Je pourrais utiliser de l'aide pour analyser un fichier de configuration où je crée des listes, où chaque liste a le format suivant; à partir du fichier de configuration (illustré ci-dessous). J'essaie d'obtenir mon script pour lire le fichier de configuration et les détails des interfaces de la liste, pour chaque interface qui vient après le nom d'hôte.Python pour les ingénieurs de réseau - Routing Config Parsing

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 

Mon script ressemble à ceci:

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

SORTIE DE MON SCRIPT:

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 

Je pense que je sais pourquoi ma sortie ressemble à ceci. La première boucle for (pour la boucle 1) exécute une expression rationnelle pour le nom d'hôte, puis continue jusqu'à la deuxième boucle for (pour la boucle 2); qui est où je pense que mes problèmes sont. Alors que la première boucle continue à signaler PE1, la deuxième boucle continue à analyser les fichiers de configuration sous mes paramètres regex. Je veux que la deuxième boucle se termine quand elle voit une autre entrée de nom d'hôte, ainsi mon script peut analyser la config pour les détails d'interface sous PE2, séparément de PE1.

Le résultat devrait ressembler à ceci:

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 

du fichier de configuration:

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

Demandez un code à ce jour? –

+0

@ cricket_007, Je n'ai reçu aucune recommandation de code. J'essaie toujours de travailler dessus (sans succès). – adrian

+0

C'est génial que vous éditiez pour montrer les résultats attendus, mais nous n'allons pas écrire le code pour vous. Vous devrez essayer quelque chose dans cette partie et expliquer ce qui ne fonctionne pas pour vous. Pour plus de détails, voir [Comment poser une bonne question] (http://stackoverflow.com/help/how-to-ask). –

Répondre

0

!!! !!! SOLVED

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

SORTIE

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