Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1131 connectés 

  FORUM HardWare.fr
  Programmation
  Python

  [Python] - Utilisation dictionnaire

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[Python] - Utilisation dictionnaire

n°2386342
Saguu
Posté le 26-05-2021 à 21:19:08  profilanswer
 

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 :
  1. from netmiko import ConnectHandler
  2. import time
  3. import re
  4. import pprint
  5. # Start of execution time
  6. start_time = time.time()
  7. # Read from a list of hostnames to connect to
  8. hosts = open('hosts.txt','r')
  9. hosts = hosts.read()
  10. hosts = hosts.strip().splitlines()
  11. # Loop to process hosts in hosts.txt file
  12. for host in hosts:
  13. # Define device type and connection attributes
  14. switches = {
  15.  'device_type': 'cisco_ios',
  16.  'ip': host,
  17.  'username': myusername',
  18.  'password': 'mypassword',
  19.  'secret': 'mypassword',
  20.  'fast_cli': True,
  21. }
  22. connectswitch = ConnectHandler(**switches)
  23. connectswitch.enable()
  24. command = connectswitch.send_command('show cdp neighbors detail')
  25. output = command.split("\n" )
  26. print(output)
  27. device      = { }
  28. device_list = [ ]
  29. for line in output:
  30.  if '-------------------------' in line:
  31.   device       = { }
  32.   device_list += [ device ]
  33. dev = re.match(r'Device ID: (.*)', line, re.M | re.I)
  34. if dev:
  35.  device["id"] = dev.group(1)
  36. ip_adr = re.match(r'  IP address: (.*)', line, re.M | re.I)
  37. if ip_adr:
  38.  device["ip"] = ip_adr.group(1)
  39. plat = re.match(r'(.*): (.*),', line, re.M | re.I)
  40. if plat:
  41.  device["platform"] = plat.group(2)
  42. pprint.pprint(device_list)


 
Malheureusement je ne parviens pas à "ordonner" correctement cet output pour pouvoir le traiter ligne par ligne.
Ex :  

Code :
  1. '-------------------------',
  2. 'Device ID: Device1',
  3. 'Entry address(es): ',
  4. '  IP address: 1.1.1.1',
  5. 'Platform: cisco WS-C3560CG-8PC-S,  Capabilities: Switch IGMP ',
  6. 'Interface: GigabitEthernet0/10,  Port ID (outgoing port): GigabitEthernet0/9',
  7. 'Holdtime : 172 sec',
  8. '',
  9. 'Version :',
  10. 'Cisco IOS Software, C3560C Software (C3560c405ex-UNIVERSALK9-M), Version '
  11. '15.2(2)E7, RELEASE SOFTWARE (fc3)',
  12. 'Technical Support: http://www.cisco.com/techsupport',
  13. 'Copyright (c) 1986-2017 by Cisco Systems, Inc.',
  14. 'Compiled Wed 12-Jul-17 16:08 by prod_rel_team',
  15. '',
  16. 'advertisement version: 2',
  17. 'Protocol Hello:  OUI=0x00000C, Protocol ID=0x0112; payload len=27, '
  18. 'value=00000000FFFFFFFF010221FF000000000000BC16F5073B00FF0000',
  19. "VTP Management Domain: 'VTP1'",
  20. 'Native VLAN: 1',
  21. 'Duplex: full',
  22. 'Management address(es): ',
  23. '  IP address: 1.1.1.1',
  24. '',
  25. '-------------------------',
  26. 'Device ID: Device2',
  27. 'Entry address(es): ',
  28. '  IP address: 2.2.2.2',
  29. 'Platform: cisco WS-C3560CG-8PC-S,  Capabilities: Switch IGMP ',
  30. 'Interface: GigabitEthernet0/9,  Port ID (outgoing port): GigabitEthernet0/10',
  31. 'Holdtime : 136 sec',
  32. '',
  33. 'Version :',
  34. 'Cisco IOS Software, C3560C Software (C3560c405ex-UNIVERSALK9-M), Version '
  35. '15.2(2)E7, RELEASE SOFTWARE (fc3)',
  36. 'Technical Support: http://www.cisco.com/techsupport',
  37. 'Copyright (c) 1986-2017 by Cisco Systems, Inc.',
  38. 'Compiled Wed 12-Jul-17 16:08 by prod_rel_team',
  39. '',
  40. 'advertisement version: 2',
  41. 'Protocol Hello:  OUI=0x00000C, Protocol ID=0x0112; payload len=27, '
  42. 'value=00000000FFFFFFFF010221FF000000000000A41875F29B00FF0000',
  43. "VTP Management Domain: 'VTP2'",
  44. 'Native VLAN: 1',
  45. 'Duplex: full',
  46. 'Management address(es): ',
  47. '  IP address: 2.2.2.2',
  48. '',
  49. '',
  50. 'Total cdp entries displayed : 2'


 
Comment m'y prendre ?
 
Merci !


Message édité par Saguu le 26-05-2021 à 21:20:14
mood
Publicité
Posté le 26-05-2021 à 21:19:08  profilanswer
 


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Python

  [Python] - Utilisation dictionnaire

 

Sujets relatifs
[Résolu]Licences : droits et vérifications d'utilisation des licencesPython et Pillow
XML espace dans les noms de noeud, acces en pythonMini projet python (Maintenance machines)
Utilisation de PDF MergerImpossible d'exécuter un script Python
Utiliser une commande curl en Python[Python] Achat automatique AMAZON
IDLE Python 
Plus de sujets relatifs à : [Python] - Utilisation dictionnaire


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR