Saguu | Hello ici,
Je suis une bille en Python et j'aimerai comprendre comment résoudre mon problème.
Je souhaite récupérer dans une variable le contenu d'une commande (Cisco en l'occurrence) pour pouvoir la traiter par la suite.
Je me suis basé sur le lien suivant https://stackoverflow.com/questions [...] bor-output pour tester ce script :
Code :
- from netmiko import ConnectHandler
- import time
- import re
- import pprint
- # Start of execution time
- start_time = time.time()
- # Read from a list of hostnames to connect to
- hosts = open('hosts.txt','r')
- hosts = hosts.read()
- hosts = hosts.strip().splitlines()
- # Loop to process hosts in hosts.txt file
- for host in hosts:
- # Define device type and connection attributes
- switches = {
- 'device_type': 'cisco_ios',
- 'ip': host,
- 'username': myusername',
- 'password': 'mypassword',
- 'secret': 'mypassword',
- 'fast_cli': True,
- }
- connectswitch = ConnectHandler(**switches)
- connectswitch.enable()
- command = connectswitch.send_command('show cdp neighbors detail')
- output = command.split("\n" )
- print(output)
- device = { }
- device_list = [ ]
- for line in output:
- if '-------------------------' in line:
- device = { }
- device_list += [ device ]
- dev = re.match(r'Device ID: (.*)', line, re.M | re.I)
- if dev:
- device["id"] = dev.group(1)
- ip_adr = re.match(r' IP address: (.*)', line, re.M | re.I)
- if ip_adr:
- device["ip"] = ip_adr.group(1)
- plat = re.match(r'(.*): (.*),', line, re.M | re.I)
- if plat:
- device["platform"] = plat.group(2)
- pprint.pprint(device_list)
|
Malheureusement je ne parviens pas à "ordonner" correctement cet output pour pouvoir le traiter ligne par ligne.
Ex :
Code :
- '-------------------------',
- 'Device ID: Device1',
- 'Entry address(es): ',
- ' IP address: 1.1.1.1',
- 'Platform: cisco WS-C3560CG-8PC-S, Capabilities: Switch IGMP ',
- 'Interface: GigabitEthernet0/10, Port ID (outgoing port): GigabitEthernet0/9',
- 'Holdtime : 172 sec',
- '',
- 'Version :',
- 'Cisco IOS Software, C3560C Software (C3560c405ex-UNIVERSALK9-M), Version '
- '15.2(2)E7, RELEASE SOFTWARE (fc3)',
- 'Technical Support: http://www.cisco.com/techsupport',
- 'Copyright (c) 1986-2017 by Cisco Systems, Inc.',
- 'Compiled Wed 12-Jul-17 16:08 by prod_rel_team',
- '',
- 'advertisement version: 2',
- 'Protocol Hello: OUI=0x00000C, Protocol ID=0x0112; payload len=27, '
- 'value=00000000FFFFFFFF010221FF000000000000BC16F5073B00FF0000',
- "VTP Management Domain: 'VTP1'",
- 'Native VLAN: 1',
- 'Duplex: full',
- 'Management address(es): ',
- ' IP address: 1.1.1.1',
- '',
- '-------------------------',
- 'Device ID: Device2',
- 'Entry address(es): ',
- ' IP address: 2.2.2.2',
- 'Platform: cisco WS-C3560CG-8PC-S, Capabilities: Switch IGMP ',
- 'Interface: GigabitEthernet0/9, Port ID (outgoing port): GigabitEthernet0/10',
- 'Holdtime : 136 sec',
- '',
- 'Version :',
- 'Cisco IOS Software, C3560C Software (C3560c405ex-UNIVERSALK9-M), Version '
- '15.2(2)E7, RELEASE SOFTWARE (fc3)',
- 'Technical Support: http://www.cisco.com/techsupport',
- 'Copyright (c) 1986-2017 by Cisco Systems, Inc.',
- 'Compiled Wed 12-Jul-17 16:08 by prod_rel_team',
- '',
- 'advertisement version: 2',
- 'Protocol Hello: OUI=0x00000C, Protocol ID=0x0112; payload len=27, '
- 'value=00000000FFFFFFFF010221FF000000000000A41875F29B00FF0000',
- "VTP Management Domain: 'VTP2'",
- 'Native VLAN: 1',
- 'Duplex: full',
- 'Management address(es): ',
- ' IP address: 2.2.2.2',
- '',
- '',
- 'Total cdp entries displayed : 2'
|
Comment m'y prendre ?
Merci ! Message édité par Saguu le 26-05-2021 à 21:20:14
|