Bonjour,
ci-dessous j'ai un bout de configuration d'un switch Cisco
Je souhaite matcher les lignes concernant le Port-channel et la description associé (non pas le port TenGigabitEthernet1/0/1 par exemple )interface Port-channel112
description *** vers XXXX ***
!
interface Port-channel113
description *** vers YYYYY ***
!
interface Port-channel127
description *** ZZZ ***
!
interface TenGigabitEthernet1/0/1
description *** AAA ***
J'ai le code suivant qui fonctionne ou chatgpt m'a indiqué l'astuce de créer une variable False pour servir de seconde condition dans un IF pour récupérer la ligne de description de l'interface souhaitée.
Ma question est la suivante. Est-ce la bonne méthode pour mon besoin ? y a t'il une méthode plus générique, plus python ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import re file_path = Path("H:/CISCO_AUDIT_VLAN/10.255.255.70.config") # Define regular expressions port_channel_pattern = re.compile(r'interface Port-channel\d+$') description_pattern = re.compile(r'^\s+description .*$') with file_path.open('r') as file: in_port_channel_block = False while (line := file.readline()): if port_channel_pattern.match(line): print(line.strip()) in_port_channel_block = True elif in_port_channel_block and description_pattern.match(line): print(line.strip()) in_port_channel_block = FalseEn vous remerciant.OUTPUT
interface Port-channel112
description *** vers XXXX ***
interface Port-channel113
description *** vers YYYYY ***
Partager