Script traitement fichier texte

Bonjour,

Voilàa je me pose une petite colle pour le traitement de fichier texte.

En realité il s’agit d’un fichier de configuration Cisco que je dois “parser”.
Mon fichier va ressembler à ceci quand je le recupère :

[code]Ethernet107/1/20 is down (Link not connected)
Hardware: 100/1000 Ethernet, address: a4138.7589.1955 (bia a4138.7589.1955)
Description: – Libre –
MTU 1500 bytes, BW 1000000 Kbit, DLY 10 usec,
reliability 255/255, txload 1/255, rxload 1/255
Encapsulation ARPA
Port mode is access
auto-duplex, auto-speed
Beacon is turned off
Input flow-control is off, output flow-control is on
Switchport monitor is off
EtherType is 0x8100
Last link flapped never
Last clearing of “show interface” counters never
30 seconds input rate 0 bits/sec, 0 bytes/sec, 0 packets/sec
30 seconds output rate 0 bits/sec, 0 bytes/sec, 0 packets/sec
Load-Interval #2: 5 minute (300 seconds)
input rate 0 bps, 0 pps; output rate 0 bps, 0 pps
RX
0 unicast packets 0 multicast packets 0 broadcast packets
0 input packets 0 bytes
0 jumbo packets 0 storm suppression bytes
0 giants 0 input error 0 short frame 0 overrun 0 underrun
0 watchdog 0 if down drop
0 input with dribble 0 input discard
0 Rx pause
TX
0 unicast packets 0 multicast packets 0 broadcast packets
0 output packets 0 bytes
0 jumbo packets
0 output errors 0 collision 0 deferred 0 late collision
0 lost carrier 0 no carrier 0 babble
0 Tx pause
0 interface resets

Ethernet107/1/21 is down (Link not connected)
Hardware: 100/1000 Ethernet, address: a418.7589.1956 (bia a418.7589.1956)
Description: – Libre –
MTU 1500 bytes, BW 1000000 Kbit, DLY 10 usec,
reliability 255/255, txload 1/255, rxload 1/255
Encapsulation ARPA
Port mode is access
auto-duplex, auto-speed
Beacon is turned off
Input flow-control is off, output flow-control is on
Switchport monitor is off
EtherType is 0x8100
Last link flapped never
Last clearing of “show interface” counters never
30 seconds input rate 0 bits/sec, 0 bytes/sec, 0 packets/sec
30 seconds output rate 0 bits/sec, 0 bytes/sec, 0 packets/sec
Load-Interval #2: 5 minute (300 seconds)
input rate 0 bps, 0 pps; output rate 0 bps, 0 pps
RX
0 unicast packets 0 multicast packets 0 broadcast packets
0 input packets 0 bytes
0 jumbo packets 0 storm suppression bytes
0 giants 0 input error 0 short frame 0 overrun 0 underrun
0 watchdog 0 if down drop
0 input with dribble 0 input discard
0 Rx pause
TX
0 unicast packets 0 multicast packets 0 broadcast packets
0 output packets 0 bytes
0 jumbo packets
0 output errors 0 collision 0 deferred 0 late collision
0 lost carrier 0 no carrier 0 babble
0 Tx pause
0 interface resets

Ethernet107/1/22 is down (Link not connected)
Hardware: 100/1000 Ethernet, address: a4138.7589.1957 (bia a4138.7589.1957)
Description: – Libre –
MTU 1500 bytes, BW 1000000 Kbit, DLY 10 usec,
reliability 255/255, txload 1/255, rxload 1/255
Encapsulation ARPA
Port mode is access
auto-duplex, auto-speed
Beacon is turned off
Input flow-control is off, output flow-control is on
Switchport monitor is off
EtherType is 0x8100
Last link flapped never
Last clearing of “show interface” counters never
30 seconds input rate 0 bits/sec, 0 bytes/sec, 0 packets/sec
30 seconds output rate 0 bits/sec, 0 bytes/sec, 0 packets/sec
Load-Interval #2: 5 minute (300 seconds)
input rate 0 bps, 0 pps; output rate 0 bps, 0 pps
RX
0 unicast packets 0 multicast packets 0 broadcast packets
0 input packets 0 bytes
0 jumbo packets 0 storm suppression bytes
0 giants 0 input error 0 short frame 0 overrun 0 underrun
0 watchdog 0 if down drop
0 input with dribble 0 input discard
0 Rx pause
TX
0 unicast packets 0 multicast packets 0 broadcast packets
0 output packets 0 bytes
0 jumbo packets
0 output errors 0 collision 0 deferred 0 late collision
0 lost carrier 0 no carrier 0 babble
0 Tx pause
0 interface resets
[/code]

Pour l’exemple je n’ai pris que 3 interfaces :mrgreen:

Il faudrait que le scipt récupère en fonction de la demande les informations suivantes :
Si l’utilisateur veut voir l’interface Ethernet107/1/22 le script ne doit retourner que ceci :

Ethernet107/1/22 is down (Link not connected) Hardware: 100/1000 Ethernet, address: a4138.7589.1957 (bia a4138.7589.1957) Description: -- Libre -- MTU 1500 bytes, BW 1000000 Kbit, DLY 10 usec, reliability 255/255, txload 1/255, rxload 1/255 Encapsulation ARPA Port mode is access auto-duplex, auto-speed Beacon is turned off Input flow-control is off, output flow-control is on Switchport monitor is off EtherType is 0x8100 Last link flapped never Last clearing of "show interface" counters never 30 seconds input rate 0 bits/sec, 0 bytes/sec, 0 packets/sec 30 seconds output rate 0 bits/sec, 0 bytes/sec, 0 packets/sec Load-Interval #2: 5 minute (300 seconds) input rate 0 bps, 0 pps; output rate 0 bps, 0 pps RX 0 unicast packets 0 multicast packets 0 broadcast packets 0 input packets 0 bytes 0 jumbo packets 0 storm suppression bytes 0 giants 0 input error 0 short frame 0 overrun 0 underrun 0 watchdog 0 if down drop 0 input with dribble 0 input discard 0 Rx pause TX 0 unicast packets 0 multicast packets 0 broadcast packets 0 output packets 0 bytes 0 jumbo packets 0 output errors 0 collision 0 deferred 0 late collision 0 lost carrier 0 no carrier 0 babble 0 Tx pause 0 interface resets

et non pas mes 5000 pages de conf :laughing:

Donc pour le début il me suffit de faire un grep car l’utilisateur va saisir le nom exacte de l’interface.
Ensuite toutes les lignes, propre à cette interface sont indentés d’une tabulation, donc il suffit que je lui dise que le premier caractère est une tabulation.

Mais la ou je ne sais pas faire, c’est comment dire : ça commence à ça et ça se termine par une ligne vide.

Pour le moment j’ai donc mon début :

#Debut en trouvant le nom de l’interface
grep Ethernet107/1/20

#Milieu en disant “tant que tabulation” ou autre

#FIN en matchant la ligne de séparation
grep ^$

Malheureusement avec un grep il n’est pas possible de délimiter un paragraphe, avec awk en colonne je ne sais pas comment m’en sortir (sauf option utiles que je connais pas )

Connaissez-vous des outils pour le faire ?

Merci

Lut,
Je ne suis pas sur de voir ce que tu veux faire, mais sinon je dirais que pour faire ça j’aurais tendance à dire perl.
Fait pour ce genre de traitement , du coup regex et traitement des fichiers très pratique…

Ce que tu décris ressemble quand même fort à un motif… du coup regex…

sed te permet de récupérer une partie d’un texte en matchant deux regex, une pour le début et une pour la fin.

Dans ton exemple :

$ sed -n '/Ethernet107\/1\/20/,/^$/p' fichier
Ethernet107/1/20 is down (Link not connected)
  Hardware: 100/1000 Ethernet, address: a4138.7589.1955 (bia a4138.7589.1955)
  Description: -- Libre --
  MTU 1500 bytes, BW 1000000 Kbit, DLY 10 usec,
     reliability 255/255, txload 1/255, rxload 1/255
  Encapsulation ARPA
  Port mode is access
  auto-duplex, auto-speed
  Beacon is turned off
  Input flow-control is off, output flow-control is on
  Switchport monitor is off
  EtherType is 0x8100
  Last link flapped never
  Last clearing of "show interface" counters never
  30 seconds input rate 0 bits/sec, 0 bytes/sec, 0 packets/sec
  30 seconds output rate 0 bits/sec, 0 bytes/sec, 0 packets/sec
  Load-Interval #2: 5 minute (300 seconds)
    input rate 0 bps, 0 pps; output rate 0 bps, 0 pps
  RX
    0 unicast packets  0 multicast packets  0 broadcast packets
    0 input packets  0 bytes
    0 jumbo packets  0 storm suppression bytes
    0 giants  0 input error  0 short frame  0 overrun   0 underrun
    0 watchdog  0 if down drop
    0 input with dribble  0 input discard
    0 Rx pause
  TX
    0 unicast packets  0 multicast packets  0 broadcast packets
    0 output packets  0 bytes
    0 jumbo packets
    0 output errors  0 collision  0 deferred  0 late collision
    0 lost carrier  0 no carrier  0 babble
    0 Tx pause
  0 interface resets


$ sed -n '/Ethernet107\/1\/21/,/^$/p' fichier
Ethernet107/1/21 is down (Link not connected)
  Hardware: 100/1000 Ethernet, address: a418.7589.1956 (bia a418.7589.1956)
  Description: -- Libre --
  MTU 1500 bytes, BW 1000000 Kbit, DLY 10 usec,
     reliability 255/255, txload 1/255, rxload 1/255
  Encapsulation ARPA
  Port mode is access
  auto-duplex, auto-speed
  Beacon is turned off
  Input flow-control is off, output flow-control is on
  Switchport monitor is off
  EtherType is 0x8100
  Last link flapped never
  Last clearing of "show interface" counters never
  30 seconds input rate 0 bits/sec, 0 bytes/sec, 0 packets/sec
  30 seconds output rate 0 bits/sec, 0 bytes/sec, 0 packets/sec
  Load-Interval #2: 5 minute (300 seconds)
    input rate 0 bps, 0 pps; output rate 0 bps, 0 pps
  RX
    0 unicast packets  0 multicast packets  0 broadcast packets
    0 input packets  0 bytes
    0 jumbo packets  0 storm suppression bytes
    0 giants  0 input error  0 short frame  0 overrun   0 underrun
    0 watchdog  0 if down drop
    0 input with dribble  0 input discard
    0 Rx pause
  TX
    0 unicast packets  0 multicast packets  0 broadcast packets
    0 output packets  0 bytes
    0 jumbo packets
    0 output errors  0 collision  0 deferred  0 late collision
    0 lost carrier  0 no carrier  0 babble
    0 Tx pause
  0 interface resets

Merci beaucoup pour ta réponse.
Je l’ai testé hier soir :slightly_smiling:
C’est exactement ce qu’il me fallait ! :mrgreen:

Problem Solved ! :slightly_smiling: