[hptools] On améliore la fonction walk

This commit is contained in:
Pierre-Elliott Bécue 2013-05-19 17:52:18 +02:00
parent e2b39511a5
commit 5f98e6d528
2 changed files with 24 additions and 24 deletions

View file

@ -17,6 +17,7 @@ from os.path import exists
from os import system
import sys
from re import findall
import re
from config import vlans
path.append('/usr/scripts/gestion')
@ -145,11 +146,11 @@ class snmp :
self._engineid = '0000000b0000%s' % self.getBaseMac()
def __exec(self,cmd) :
s, r = getstatusoutput(cmd)
if s :
r=r.replace('snmpget: ','')
raise ConversationError(r,cmd)
return r
status, response = getstatusoutput(cmd)
if status:
response = response.replace('snmpget: ','')
print 'Erreur : '+response+' : '+cmd
return response
def get(self,oid) :
""" Retourne le résultat correspondant à l'oid demandé """
@ -182,12 +183,14 @@ class snmp :
""" Retourne le résultat de snmpwalk
le retour est un dictionnaire { oid : valeur }
"""
lignes = self.__exec('snmpwalk -O q %s %s' % (self.options, base_oid ) ).split('\n')
lignes = self.__exec('snmpwalk -Ox %s %s' % (self.options, base_oid ) ).split('\n')
result = {}
for l in lignes :
if l !="" :
oid, valeur = l.split(' ', 1)
for ligne in lignes:
try:
oid, valeur = ligne.split('Hex-STRING: ')
result[oid] = valeur
except:
pass
return result
#############################################################################################
@ -228,11 +231,11 @@ class hpswitch :
data = self.walk('STATISTICS-MIB::hpSwitchPortFdbAddress.%d' % int(prise))
macs = []
for value in data.itervalues():
mac = findall('".*"', value)[0][1:-1]
if ' ' in mac:
macs.append(":".join(mac.lower().split(" ")[:-1]))
else:
macs.append(":".join("%02x" % ord(digit) for digit in mac))
mac = value.replace(' ', '').lower().replace('"', '')
if not re.match('([0-9a-f]{2}){6}', mac):
mac = mac.encode('hex').lower()
mac = "%s:%s:%s:%s:%s:%s" % (mac[0:2], mac[2:4], mac[4:6], mac[6:8], mac[8:10], mac[10:12])
macs.append(mac)
return macs
except ValueError:
# Pas de MAC trouvée
@ -241,12 +244,7 @@ class hpswitch :
def where_is_mac(self, mac) :
"""Retrouve la prise correspondant à une adresse MAC donnée"""
if self.__debug : self.__logDest.write("HP DEBUG : where_is_mac(mac=%s)\n" % mac)
# On va transformer l'adresse MAC cherchée pour la mettre au format 0A 0A 0A 0A 0A 0A
mac = mac.upper()
mac = filter(lambda x: x in "0123456789ABCDEF", mac) # 0A0A0A0A0A0A
mac = "%s %s %s %s %s %s" % (mac[0:2], mac[2:4], mac[4:6],
mac[6:8], mac[8:10], mac[10:12])
mac = mac.lower()
# On interroge le switch
try:
data = self.walk('STATISTICS-MIB::hpSwitchPortFdbAddress')
@ -257,13 +255,15 @@ class hpswitch :
# On cherche dans data la bonne adresse MAC
for (onesnmp, onemac) in data.iteritems():
try:
onemac = findall('".*"', onemac)[0][1:-1]
if ' ' not in onemac:
onemac = " ".join("%02x" % ord(digit) for digit in mac)
onemac = onemac.replace(' ', '').lower().replace('"', '')
if not re.match('([0-9a-f]{2}){6}', onemac):
onemac = onemac.encode('hex').lower()
onemac = "%s:%s:%s:%s:%s:%s" % (onemac[0:2], onemac[2:4], onemac[4:6], onemac[6:8], onemac[8:10], onemac[10:12])
if onemac.startswith(mac):
return int(onesnmp.split(".")[1])
except:
pass
onemac = ""
# On a rien trouvé
return None