Detabification
darcs-hash:20050305154151-d1718-05f9b3218f0000213870da1dd5ccb231bf5a4380.gz
This commit is contained in:
parent
ef05652581
commit
26d494dd1e
1 changed files with 344 additions and 344 deletions
|
@ -22,8 +22,8 @@ except :
|
|||
# Si a pas le droit de lire config_snmp_secrete
|
||||
# on va tenter de tout faire en snmpv1 et communauté public
|
||||
def config_snmp_secrete(snmp,switch) :
|
||||
snmp = snmp(switch,version='1',community='public')
|
||||
return snmp.get, snmp.set, snmp.walk
|
||||
snmp = snmp(switch,version='1',community='public')
|
||||
return snmp.get, snmp.set, snmp.walk
|
||||
|
||||
#############################################################################################
|
||||
### Définitions de classes utiles
|
||||
|
@ -49,155 +49,155 @@ class ssh :
|
|||
__ssh_out = '' # Retour de la connexion ssh
|
||||
|
||||
def __init__(self,host) :
|
||||
""" Ouverture d'une connexion ssh vers le switch choisi """
|
||||
self.switch = host
|
||||
self.log=open(self.__ssh_log,'w')
|
||||
""" Ouverture d'une connexion ssh vers le switch choisi """
|
||||
self.switch = host
|
||||
self.log=open(self.__ssh_log,'w')
|
||||
|
||||
if self.__debug : self.__logDest.write("SSH DEBUG : __init__(host=%s)\n" % host)
|
||||
self.__host = host
|
||||
self.__retour, self.__input, self.__err = popen3("/usr/bin/ssh -tt %s" % host)
|
||||
sleep(1)
|
||||
if self.__debug : self.__logDest.write("SSH DEBUG : __init__(host=%s)\n" % host)
|
||||
self.__host = host
|
||||
self.__retour, self.__input, self.__err = popen3("/usr/bin/ssh -tt %s" % host)
|
||||
sleep(1)
|
||||
|
||||
# Création de threads de lecture de la connexion
|
||||
r = threading.Thread(target=self.__read_retour,args=(threading.currentThread(),))
|
||||
r.start()
|
||||
e = threading.Thread(target=self.__read_err,args=(threading.currentThread(),))
|
||||
e.start()
|
||||
# Création de threads de lecture de la connexion
|
||||
r = threading.Thread(target=self.__read_retour,args=(threading.currentThread(),))
|
||||
r.start()
|
||||
e = threading.Thread(target=self.__read_err,args=(threading.currentThread(),))
|
||||
e.start()
|
||||
|
||||
# On passe l'intro, passe manager et en environnement de configuration
|
||||
self.send_cmd('\nenable\nconfigure')
|
||||
# On passe l'intro, passe manager et en environnement de configuration
|
||||
self.send_cmd('\nenable\nconfigure')
|
||||
|
||||
def __read_retour(self,parent) :
|
||||
while parent.isAlive() :
|
||||
c = self.__retour.read(1)
|
||||
self.__ssh_out += c
|
||||
self.log.write(c)
|
||||
self.log.flush()
|
||||
while parent.isAlive() :
|
||||
c = self.__retour.read(1)
|
||||
self.__ssh_out += c
|
||||
self.log.write(c)
|
||||
self.log.flush()
|
||||
|
||||
def __read_err(self,parent) :
|
||||
err = ''
|
||||
while parent.isAlive :
|
||||
char = self.__err.read(1)
|
||||
if char == '\n' :
|
||||
# Il y a eu une erreur
|
||||
if err.lower() in [ 'connection to %s closed by remote host.' % self.switch ,
|
||||
'connection to %s closed.' % self.switch ] :
|
||||
raise ConnectionClosed
|
||||
else :
|
||||
print err
|
||||
err = ''
|
||||
else :
|
||||
err += char
|
||||
err = ''
|
||||
while parent.isAlive :
|
||||
char = self.__err.read(1)
|
||||
if char == '\n' :
|
||||
# Il y a eu une erreur
|
||||
if err.lower() in [ 'connection to %s closed by remote host.' % self.switch ,
|
||||
'connection to %s closed.' % self.switch ] :
|
||||
raise ConnectionClosed
|
||||
else :
|
||||
print err
|
||||
err = ''
|
||||
else :
|
||||
err += char
|
||||
|
||||
def __del__(self) :
|
||||
"""Ferme la connexion : envoi logout et attend """
|
||||
if self.__debug : self.__logDest.write("SSH DEBUG : __del__()\n")
|
||||
try :
|
||||
self.send_cmd('\nlogout')
|
||||
except ConnectionClosed :
|
||||
# C'est le but
|
||||
pass
|
||||
"""Ferme la connexion : envoi logout et attend """
|
||||
if self.__debug : self.__logDest.write("SSH DEBUG : __del__()\n")
|
||||
try :
|
||||
self.send_cmd('\nlogout')
|
||||
except ConnectionClosed :
|
||||
# C'est le but
|
||||
pass
|
||||
|
||||
def send_cmd(self,cmd,timeout=15):
|
||||
""" Envoi une commande, attend le prompt et retourne la réponse """
|
||||
if self.__debug : self.__logDest.write("SSH DEBUG : __send_cmd(%s)\n" % cmd.strip() )
|
||||
""" Envoi une commande, attend le prompt et retourne la réponse """
|
||||
if self.__debug : self.__logDest.write("SSH DEBUG : __send_cmd(%s)\n" % cmd.strip() )
|
||||
|
||||
self.__ssh_out = '' # oubli de ce qu'il y a avant
|
||||
self.log.flush()
|
||||
self.__ssh_out = '' # oubli de ce qu'il y a avant
|
||||
self.log.flush()
|
||||
|
||||
# Envoi de la commande
|
||||
self.__input.write(cmd+'\n')
|
||||
self.__input.flush()
|
||||
# Envoi de la commande
|
||||
self.__input.write(cmd+'\n')
|
||||
self.__input.flush()
|
||||
|
||||
# Premier retour
|
||||
count=0
|
||||
t = ''
|
||||
while 1:
|
||||
sleep(1)
|
||||
out = self.__ssh_out
|
||||
# On a récupéré un prompt ?
|
||||
while_break = 0
|
||||
for n in range(1,200) :
|
||||
try : t = out[-n-7:-n]
|
||||
except : continue
|
||||
if t == ' [y/n]?' :
|
||||
self.__input.write('y')
|
||||
self.__input.flush()
|
||||
sleep(1)
|
||||
elif t == 'onfig)#' :
|
||||
if self.__debug : self.__logDest.write("SSH DEBUG : __send_cmd -> OK\n")
|
||||
while_break=1
|
||||
elif t == 'ntrol-C' :
|
||||
# Faut appuyer sur une touche
|
||||
if self.__debug : self.__logDest.write("SSH DEBUG : __send_cmd -> MORE\n")
|
||||
self.__input.write(' ')
|
||||
self.__input.flush()
|
||||
sleep(1)
|
||||
else :
|
||||
continue
|
||||
break
|
||||
# Premier retour
|
||||
count=0
|
||||
t = ''
|
||||
while 1:
|
||||
sleep(1)
|
||||
out = self.__ssh_out
|
||||
# On a récupéré un prompt ?
|
||||
while_break = 0
|
||||
for n in range(1,200) :
|
||||
try : t = out[-n-7:-n]
|
||||
except : continue
|
||||
if t == ' [y/n]?' :
|
||||
self.__input.write('y')
|
||||
self.__input.flush()
|
||||
sleep(1)
|
||||
elif t == 'onfig)#' :
|
||||
if self.__debug : self.__logDest.write("SSH DEBUG : __send_cmd -> OK\n")
|
||||
while_break=1
|
||||
elif t == 'ntrol-C' :
|
||||
# Faut appuyer sur une touche
|
||||
if self.__debug : self.__logDest.write("SSH DEBUG : __send_cmd -> MORE\n")
|
||||
self.__input.write(' ')
|
||||
self.__input.flush()
|
||||
sleep(1)
|
||||
else :
|
||||
continue
|
||||
break
|
||||
|
||||
if while_break : break
|
||||
# Rien de bien, le switch es un peu lent, on attend
|
||||
if self.__debug : self.__logDest.write("SSH DEBUG : __send_cmd -> WAIT (%s) \n" % t)
|
||||
count += 1
|
||||
if count > timeout :
|
||||
# Il y a un problème
|
||||
raise ConnectionTimout
|
||||
if while_break : break
|
||||
# Rien de bien, le switch es un peu lent, on attend
|
||||
if self.__debug : self.__logDest.write("SSH DEBUG : __send_cmd -> WAIT (%s) \n" % t)
|
||||
count += 1
|
||||
if count > timeout :
|
||||
# Il y a un problème
|
||||
raise ConnectionTimout
|
||||
|
||||
return self.__ssh_out
|
||||
return self.__ssh_out
|
||||
|
||||
class snmp :
|
||||
""" Classe de communication SNMP """
|
||||
def __init__(self,host,version=None,community=None,authentication_protocol=None, authentication_pass=None, username=None, privacy_pass=None) :
|
||||
""" host doit être la machine sur laquelle se connecter
|
||||
version est la verion du protocole snmp à utiliser : 1, 2c ou 3
|
||||
le reste des données doit être ou non fourni suivant la version
|
||||
""" host doit être la machine sur laquelle se connecter
|
||||
version est la verion du protocole snmp à utiliser : 1, 2c ou 3
|
||||
le reste des données doit être ou non fourni suivant la version
|
||||
|
||||
pour v1 et v2c seule la communauté est prise en compte
|
||||
pour v3 authentication_protocol, authentication_pass et username sont requis si accès en authNoPriv
|
||||
si privacy_pass est fourni accès en authPriv
|
||||
"""
|
||||
self.host = host
|
||||
self.version = version
|
||||
if version == '1' or version == '2c' :
|
||||
self.options = "-v %s -c '%s' %s " % ( version, community, host )
|
||||
elif version =='3' :
|
||||
self.options = "-v 3 -u %s -a %s -A '%s' -l authNoPriv" % ( username, authentication_protocol, authentication_pass )
|
||||
if privacy_pass :
|
||||
self.options += " -x DES -X '%s' -l authPriv" % privacy_pass
|
||||
self.options += " %s " % host
|
||||
else :
|
||||
raise ValueError('Version incorrecte')
|
||||
pour v1 et v2c seule la communauté est prise en compte
|
||||
pour v3 authentication_protocol, authentication_pass et username sont requis si accès en authNoPriv
|
||||
si privacy_pass est fourni accès en authPriv
|
||||
"""
|
||||
self.host = host
|
||||
self.version = version
|
||||
if version == '1' or version == '2c' :
|
||||
self.options = "-v %s -c '%s' %s " % ( version, community, host )
|
||||
elif version =='3' :
|
||||
self.options = "-v 3 -u %s -a %s -A '%s' -l authNoPriv" % ( username, authentication_protocol, authentication_pass )
|
||||
if privacy_pass :
|
||||
self.options += " -x DES -X '%s' -l authPriv" % privacy_pass
|
||||
self.options += " %s " % host
|
||||
else :
|
||||
raise ValueError('Version incorrecte')
|
||||
|
||||
def __exec(self,cmd) :
|
||||
s, r = getstatusoutput(cmd)
|
||||
if s :
|
||||
raise ConversationError(r)
|
||||
return r
|
||||
s, r = getstatusoutput(cmd)
|
||||
if s :
|
||||
raise ConversationError(r)
|
||||
return r
|
||||
|
||||
def get(self,oid) :
|
||||
""" Retourne le résultat correspondant à l'oid demandé """
|
||||
return self.__exec('snmpget -O vq %s %s ' % ( self.options, oid ) )
|
||||
""" Retourne le résultat correspondant à l'oid demandé """
|
||||
return self.__exec('snmpget -O vq %s %s ' % ( self.options, oid ) )
|
||||
|
||||
def set(self,oid,typ,val) :
|
||||
""" Change la valeur le l'oid donné.
|
||||
type est le type de la valeur
|
||||
val est la valeur à écrire
|
||||
"""
|
||||
return self.__exec('snmpset -O vq %s %s %s %s' % (self.options, oid, typ, val ) )
|
||||
""" Change la valeur le l'oid donné.
|
||||
type est le type de la valeur
|
||||
val est la valeur à écrire
|
||||
"""
|
||||
return self.__exec('snmpset -O vq %s %s %s %s' % (self.options, oid, typ, val ) )
|
||||
|
||||
def walk(self,base_oid) :
|
||||
""" 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')
|
||||
result = {}
|
||||
for l in lignes :
|
||||
if l !="" :
|
||||
oid, valeur = l.split(' ', 1)
|
||||
result[oid] = valeur
|
||||
return result
|
||||
""" 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')
|
||||
result = {}
|
||||
for l in lignes :
|
||||
if l !="" :
|
||||
oid, valeur = l.split(' ', 1)
|
||||
result[oid] = valeur
|
||||
return result
|
||||
|
||||
#############################################################################################
|
||||
### Gestion des switchs proprement dite
|
||||
|
@ -219,49 +219,49 @@ class hpswitch :
|
|||
__conn_ssh = None
|
||||
|
||||
def __init__(self,switch) :
|
||||
""" Switch doit être le nom du switch """
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : __init__(switch=%s)\n" % switch )
|
||||
self.switch = switch.lower()
|
||||
""" Switch doit être le nom du switch """
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : __init__(switch=%s)\n" % switch )
|
||||
self.switch = switch.lower()
|
||||
|
||||
# Config snmp
|
||||
self.get, self.set, self.walk = config_snmp_secrete(snmp,switch)
|
||||
# Config snmp
|
||||
self.get, self.set, self.walk = config_snmp_secrete(snmp,switch)
|
||||
|
||||
def __ssh(self,cmd,timout=15) :
|
||||
if not self.__conn_ssh :
|
||||
self.__conn_ssh = ssh(self.switch)
|
||||
if not self.__conn_ssh :
|
||||
self.__conn_ssh = ssh(self.switch)
|
||||
|
||||
return self.__conn_ssh.send_cmd(cmd,timout)
|
||||
return self.__conn_ssh.send_cmd(cmd,timout)
|
||||
|
||||
def set_prise(self,prise,action) :
|
||||
"""
|
||||
Effectue l'action action donnée sur la (les) prise(s) donnée(s)
|
||||
"""
|
||||
Effectue l'action action donnée sur la (les) prise(s) donnée(s)
|
||||
|
||||
'prise' peut être un ensembre de prises :
|
||||
ex : '1-3,6' effectuera l'action sur les prises 1,2,3 et 6
|
||||
'prise' peut être un ensembre de prises :
|
||||
ex : '1-3,6' effectuera l'action sur les prises 1,2,3 et 6
|
||||
|
||||
'action' peut être principalement
|
||||
enable/disable
|
||||
speed-duplex [auto-100/auto-10]
|
||||
voir manuel des swtichs pour plus d'actions possibles
|
||||
"""
|
||||
if not prise : prise = self.prise
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : set_prise(prise=%s,action=%s)\n" %( prise,action))
|
||||
return self.__ssh( "interface ethernet %s %s" % (prise,action) )
|
||||
'action' peut être principalement
|
||||
enable/disable
|
||||
speed-duplex [auto-100/auto-10]
|
||||
voir manuel des swtichs pour plus d'actions possibles
|
||||
"""
|
||||
if not prise : prise = self.prise
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : set_prise(prise=%s,action=%s)\n" %( prise,action))
|
||||
return self.__ssh( "interface ethernet %s %s" % (prise,action) )
|
||||
|
||||
def show_stats(self,prise='') :
|
||||
""" Retourne les stats de(s) prise(s) choisie(s)."""
|
||||
if not prise : prise = self.prise
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : show_prise_status(prise=%s)\n" % prise)
|
||||
""" Retourne les stats de(s) prise(s) choisie(s)."""
|
||||
if not prise : prise = self.prise
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : show_prise_status(prise=%s)\n" % prise)
|
||||
|
||||
if prise :
|
||||
return self.__ssh("show interfaces ethernet %s" % prise)
|
||||
else :
|
||||
return self.__ssh("show interfaces")
|
||||
if prise :
|
||||
return self.__ssh("show interfaces ethernet %s" % prise)
|
||||
else :
|
||||
return self.__ssh("show interfaces")
|
||||
|
||||
def show_prise_mac(self,prise='') :
|
||||
""" Retourne le(s) adresse(s) MAC présentes sur la prise."""
|
||||
if not prise : prise = self.prise
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : show_prise_mac(prise=%s)\n" % prise)
|
||||
""" Retourne le(s) adresse(s) MAC présentes sur la prise."""
|
||||
if not prise : prise = self.prise
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : show_prise_mac(prise=%s)\n" % prise)
|
||||
try:
|
||||
data = self.walk('STATISTICS-MIB::hpSwitchPortFdbAddress.%d' % int(prise))
|
||||
return map(lambda x:":".join(x[1:-2].lower().split(" ")),data.values())
|
||||
|
@ -271,7 +271,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)
|
||||
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
|
||||
|
@ -291,211 +291,211 @@ class hpswitch :
|
|||
|
||||
|
||||
def set_prise_mac(self,prise='',mac='') :
|
||||
""" Défini les adresses mac autorisées sur une prise.
|
||||
""" Défini les adresses mac autorisées sur une prise.
|
||||
|
||||
/!\ format de la mac : xxxxxx-xxxxxx (x hexa)
|
||||
On peut aussi en mettre plusieus séparées par des espaces.
|
||||
Si mac est précédé de -, enlève les adresses (si juste - les vire toutes)
|
||||
/!\ format de la mac : xxxxxx-xxxxxx (x hexa)
|
||||
On peut aussi en mettre plusieus séparées par des espaces.
|
||||
Si mac est précédé de -, enlève les adresses (si juste - les vire toutes)
|
||||
|
||||
Si mac n'est pas fourni retourne le(s) adresse(s) MAC autorisées sur la prise.
|
||||
Si prise n'est pas fourni retourne la config de sécurité de tous les ports
|
||||
"""
|
||||
if not prise : prise = self.prise
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : set_prise_mac(prise=%s,mac=%s)\n" % (prise,mac))
|
||||
Si mac n'est pas fourni retourne le(s) adresse(s) MAC autorisées sur la prise.
|
||||
Si prise n'est pas fourni retourne la config de sécurité de tous les ports
|
||||
"""
|
||||
if not prise : prise = self.prise
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : set_prise_mac(prise=%s,mac=%s)\n" % (prise,mac))
|
||||
|
||||
if not mac :
|
||||
return self.__sudo("show port-security %s" % prise)
|
||||
if not mac :
|
||||
return self.__sudo("show port-security %s" % prise)
|
||||
|
||||
if mac[0]=='-' :
|
||||
no='no '
|
||||
mac=mac[1:]
|
||||
else :no=''
|
||||
if mac[0]=='-' :
|
||||
no='no '
|
||||
mac=mac[1:]
|
||||
else :no=''
|
||||
|
||||
return self.__ssh("%sport-security ethernet %s mac-address %s" % (no, prise,mac) )
|
||||
return self.__ssh("%sport-security ethernet %s mac-address %s" % (no, prise,mac) )
|
||||
|
||||
def show_interfaces(self,prise='') :
|
||||
""" Retourne la liste des interfaces ainsi que leur état
|
||||
Si prise est spécifié n'affiche que la prise demandée """
|
||||
if not prise : prise = self.prise
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : show_interfaces(prise=%s)\n" % prise)
|
||||
a = self.__ssh("show interfaces brief")
|
||||
if a and prise :
|
||||
a=a.split('\n')
|
||||
try : a='\n'.join(a[:5]+[a[prise+4]],'\n')
|
||||
except : a=0
|
||||
return a
|
||||
""" Retourne la liste des interfaces ainsi que leur état
|
||||
Si prise est spécifié n'affiche que la prise demandée """
|
||||
if not prise : prise = self.prise
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : show_interfaces(prise=%s)\n" % prise)
|
||||
a = self.__ssh("show interfaces brief")
|
||||
if a and prise :
|
||||
a=a.split('\n')
|
||||
try : a='\n'.join(a[:5]+[a[prise+4]],'\n')
|
||||
except : a=0
|
||||
return a
|
||||
|
||||
def update(self) :
|
||||
""" Upload de la config courrante
|
||||
Téléchargment de la nouvelle config
|
||||
Reboot."""
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : update()\n")
|
||||
""" Upload de la config courrante
|
||||
Téléchargment de la nouvelle config
|
||||
Reboot."""
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : update()\n")
|
||||
|
||||
# Sauvegarde
|
||||
self.__ssh("copy running-config tftp %s %s unix" % (self.IP_tftp, self.switch + '.running') )
|
||||
try :
|
||||
print self.__ssh("copy tftp startup-config %s %s unix" % (self.IP_tftp, self.switch+'.conf') )
|
||||
except ConnectionClosed :
|
||||
# C'est normal : le switch reboote
|
||||
pass
|
||||
# Sauvegarde
|
||||
self.__ssh("copy running-config tftp %s %s unix" % (self.IP_tftp, self.switch + '.running') )
|
||||
try :
|
||||
print self.__ssh("copy tftp startup-config %s %s unix" % (self.IP_tftp, self.switch+'.conf') )
|
||||
except ConnectionClosed :
|
||||
# C'est normal : le switch reboote
|
||||
pass
|
||||
|
||||
def upgrade(self) :
|
||||
""" Changement de firmware, le firmware est sur la machine
|
||||
ayant le serveur tftfp et a pour nom firmware26xx """
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : upgrate()\n")
|
||||
print self.__ssh("copy tftp flash %s %s" % (self.IP_tftp, self.firmware_file) , 120) # Long timout
|
||||
""" Changement de firmware, le firmware est sur la machine
|
||||
ayant le serveur tftfp et a pour nom firmware26xx """
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : upgrate()\n")
|
||||
print self.__ssh("copy tftp flash %s %s" % (self.IP_tftp, self.firmware_file) , 120) # Long timout
|
||||
|
||||
def reboot(self) :
|
||||
""" Reboote le switch """
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : reboot()\n")
|
||||
try :
|
||||
self.__ssh("boot")
|
||||
except ConnectionClosed :
|
||||
# C'est normal : le switch reboote
|
||||
pass
|
||||
""" Reboote le switch """
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : reboot()\n")
|
||||
try :
|
||||
self.__ssh("boot")
|
||||
except ConnectionClosed :
|
||||
# C'est normal : le switch reboote
|
||||
pass
|
||||
|
||||
# Fonction utilisant le SNMP
|
||||
|
||||
def multicast(self,ip='') :
|
||||
""" Donne la liste des ports du swich inscrits au flux multicast donné
|
||||
Si aucun flux donné teste tous les flux multicast possibles.
|
||||
""" Donne la liste des ports du swich inscrits au flux multicast donné
|
||||
Si aucun flux donné teste tous les flux multicast possibles.
|
||||
|
||||
Retourne un dictionnaire : { adresse du flux : [ ports inscrits ] }
|
||||
"""
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : multicast(ip=%s)\n" % ip)
|
||||
if ip :
|
||||
data = self.walk('STATISTICS-MIB::hpIgmpStatsPortIndex2.%s' % ip)
|
||||
return { ip : data.values() }
|
||||
Retourne un dictionnaire : { adresse du flux : [ ports inscrits ] }
|
||||
"""
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : multicast(ip=%s)\n" % ip)
|
||||
if ip :
|
||||
data = self.walk('STATISTICS-MIB::hpIgmpStatsPortIndex2.%s' % ip)
|
||||
return { ip : data.values() }
|
||||
|
||||
else :
|
||||
data = self.walk('STATISTICS-MIB::hpIgmpStatsPortIndex2')
|
||||
result = {}
|
||||
# On veut tout
|
||||
for oid, port in data.items() :
|
||||
try : ip = '.'.join(oid.split('.')[2:6])
|
||||
except : continue
|
||||
result.setdefault(ip,[])
|
||||
result[ip].append(port)
|
||||
else :
|
||||
data = self.walk('STATISTICS-MIB::hpIgmpStatsPortIndex2')
|
||||
result = {}
|
||||
# On veut tout
|
||||
for oid, port in data.items() :
|
||||
try : ip = '.'.join(oid.split('.')[2:6])
|
||||
except : continue
|
||||
result.setdefault(ip,[])
|
||||
result[ip].append(port)
|
||||
|
||||
return result
|
||||
return result
|
||||
|
||||
def nb_prises(self) :
|
||||
""" Retourne le nombre de prises du switch """
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : nb_prises()\n")
|
||||
return int(self.version().split()[4][2:4])
|
||||
""" Retourne le nombre de prises du switch """
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : nb_prises()\n")
|
||||
return int(self.version().split()[4][2:4])
|
||||
|
||||
def version(self) :
|
||||
""" Retourne la version du firmware du switch """
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : version()\n")
|
||||
return self.get('SNMPv2-MIB::sysDescr.0')
|
||||
""" Retourne la version du firmware du switch """
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : version()\n")
|
||||
return self.get('SNMPv2-MIB::sysDescr.0')
|
||||
|
||||
def enable(self,prise=0) :
|
||||
""" Active une prise """
|
||||
if not prise : prise = self.prise
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : enable(prise=%s)\n" % prise)
|
||||
return self.set('IF-MIB::ifAdminStatus.%d' % int(prise), 'i', 1)
|
||||
""" Active une prise """
|
||||
if not prise : prise = self.prise
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : enable(prise=%s)\n" % prise)
|
||||
return self.set('IF-MIB::ifAdminStatus.%d' % int(prise), 'i', 1)
|
||||
|
||||
def disable(self,prise=0) :
|
||||
""" Désactive une prise """
|
||||
if not prise : prise = self.prise
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : disable(prise=%s)\n" % prise)
|
||||
return self.set('IF-MIB::ifAdminStatus.%d' % int(prise), 'i', 2)
|
||||
""" Désactive une prise """
|
||||
if not prise : prise = self.prise
|
||||
if self.__debug : self.__logDest.write("HP DEBUG : disable(prise=%s)\n" % prise)
|
||||
return self.set('IF-MIB::ifAdminStatus.%d' % int(prise), 'i', 2)
|
||||
|
||||
def __is(self,oid,prise) :
|
||||
if not prise : prise = self.prise
|
||||
prise = str(prise)
|
||||
if prise=='all' :
|
||||
nb = 0
|
||||
for oid,etat in self.walk(oid).items() :
|
||||
if etat == 'up' and int(oid.split('.')[1])<51 :
|
||||
# Le <51 est ici pour éviter de compter les ports fictifs
|
||||
nb += 1
|
||||
return nb
|
||||
prise = prise.replace('-','')
|
||||
return self.get(oid + '.' + prise) == 'up'
|
||||
if not prise : prise = self.prise
|
||||
prise = str(prise)
|
||||
if prise=='all' :
|
||||
nb = 0
|
||||
for oid,etat in self.walk(oid).items() :
|
||||
if etat == 'up' and int(oid.split('.')[1])<51 :
|
||||
# Le <51 est ici pour éviter de compter les ports fictifs
|
||||
nb += 1
|
||||
return nb
|
||||
prise = prise.replace('-','')
|
||||
return self.get(oid + '.' + prise) == 'up'
|
||||
|
||||
def is_enable(self,prise=0) :
|
||||
""" Retoune True ou False suivant si la prise est activée ou non
|
||||
Si prise=all retourne le nombre de prises activées sur le switch """
|
||||
""" Retoune True ou False suivant si la prise est activée ou non
|
||||
Si prise=all retourne le nombre de prises activées sur le switch """
|
||||
if prise != 'all': prise = int(prise)
|
||||
return self.__is('IF-MIB::ifAdminStatus',prise)
|
||||
return self.__is('IF-MIB::ifAdminStatus',prise)
|
||||
|
||||
def is_up(self,prise=0) :
|
||||
""" Retoune True ou False suivant si la prise est up
|
||||
Si prise=all retourne le nombre de prises up sur le switch """
|
||||
""" Retoune True ou False suivant si la prise est up
|
||||
Si prise=all retourne le nombre de prises up sur le switch """
|
||||
if prise != 'all': prise = int(prise)
|
||||
return self.__is('IF-MIB::ifOperStatus',prise)
|
||||
return self.__is('IF-MIB::ifOperStatus',prise)
|
||||
|
||||
def nom(self,nom=None,prise=0) :
|
||||
""" Retourne ou attribue le nom à la prise fournie """
|
||||
if not prise : prise = self.prise
|
||||
oid = 'IF-MIB::ifAlias.%d' % int(prise)
|
||||
if nom==None :
|
||||
return self.get(oid)
|
||||
else :
|
||||
self.set(oid, 's' , nom)
|
||||
""" Retourne ou attribue le nom à la prise fournie """
|
||||
if not prise : prise = self.prise
|
||||
oid = 'IF-MIB::ifAlias.%d' % int(prise)
|
||||
if nom==None :
|
||||
return self.get(oid)
|
||||
else :
|
||||
self.set(oid, 's' , nom)
|
||||
|
||||
def eth_mode(self,mode=None,prise=0) :
|
||||
""" Fixe ou retourne le mode d'une prise
|
||||
mode est un tuple : (vitesse, duplex) ou simplement "auto"
|
||||
vitesse est : 10 100 ou 1000
|
||||
duplex est FD, HD ou auto
|
||||
"""
|
||||
if not prise : prise = self.prise
|
||||
oid = 'CONFIG-MIB::hpSwitchPortFastEtherMode.%d' % int(prise)
|
||||
""" Fixe ou retourne le mode d'une prise
|
||||
mode est un tuple : (vitesse, duplex) ou simplement "auto"
|
||||
vitesse est : 10 100 ou 1000
|
||||
duplex est FD, HD ou auto
|
||||
"""
|
||||
if not prise : prise = self.prise
|
||||
oid = 'CONFIG-MIB::hpSwitchPortFastEtherMode.%d' % int(prise)
|
||||
|
||||
if mode == None :
|
||||
return self.get(oid)
|
||||
if mode == None :
|
||||
return self.get(oid)
|
||||
|
||||
# Conversion du mode
|
||||
if mode == 'auto' :
|
||||
code = 5
|
||||
else :
|
||||
code = { 'HD' : 2 , 'FD' : 4 , 'AUTO' : 8 }[mode[1].upper()]
|
||||
if mode[0] == 10 :
|
||||
code -= 1
|
||||
elif mode[0] == 1000 :
|
||||
if code == 8 : code += 1
|
||||
elif code == 2 : raise ValueError('Mode invelide %s' % mode)
|
||||
# Conversion du mode
|
||||
if mode == 'auto' :
|
||||
code = 5
|
||||
else :
|
||||
code = { 'HD' : 2 , 'FD' : 4 , 'AUTO' : 8 }[mode[1].upper()]
|
||||
if mode[0] == 10 :
|
||||
code -= 1
|
||||
elif mode[0] == 1000 :
|
||||
if code == 8 : code += 1
|
||||
elif code == 2 : raise ValueError('Mode invelide %s' % mode)
|
||||
else: code += 1
|
||||
|
||||
self.set(oid,'i',code)
|
||||
self.set(oid,'i',code)
|
||||
|
||||
class sw_chbre(hpswitch) :
|
||||
def __init__(self,chbre) :
|
||||
# On retrouve la chbre dans l'annuaire
|
||||
self.chbre = chbre
|
||||
try :
|
||||
bat = chbre[0].lower()
|
||||
prise = chbre_prises[bat][chbre[1:]]
|
||||
self.prise_brute = prise
|
||||
self.switch = 'bat%s' % bat
|
||||
num_switch = int(prise[0])
|
||||
if num_switch != 0 :
|
||||
self.switch += '-%i' % num_switch
|
||||
if prise[-1] == '-' :
|
||||
#Prise en 10
|
||||
self.prise = int(prise[1:-1])
|
||||
self.prise10Mb = True
|
||||
else :
|
||||
self.prise = int(prise[1:])
|
||||
self.prise10Mb = False
|
||||
except :
|
||||
raise ValueError('Chambre %s inconnue' % chbre)
|
||||
# On retrouve la chbre dans l'annuaire
|
||||
self.chbre = chbre
|
||||
try :
|
||||
bat = chbre[0].lower()
|
||||
prise = chbre_prises[bat][chbre[1:]]
|
||||
self.prise_brute = prise
|
||||
self.switch = 'bat%s' % bat
|
||||
num_switch = int(prise[0])
|
||||
if num_switch != 0 :
|
||||
self.switch += '-%i' % num_switch
|
||||
if prise[-1] == '-' :
|
||||
#Prise en 10
|
||||
self.prise = int(prise[1:-1])
|
||||
self.prise10Mb = True
|
||||
else :
|
||||
self.prise = int(prise[1:])
|
||||
self.prise10Mb = False
|
||||
except :
|
||||
raise ValueError('Chambre %s inconnue' % chbre)
|
||||
|
||||
# Config snmp
|
||||
self.get, self.set, self.walk = config_snmp_secrete(snmp,self.switch)
|
||||
# Config snmp
|
||||
self.get, self.set, self.walk = config_snmp_secrete(snmp,self.switch)
|
||||
|
||||
|
||||
def reconfigure(self) :
|
||||
""" Reconfigure la prise (nom et vitesse) """
|
||||
in10 = self.eth_mode().find('10Mbits') != 1
|
||||
""" Reconfigure la prise (nom et vitesse) """
|
||||
in10 = self.eth_mode().find('10Mbits') != 1
|
||||
|
||||
if self.prise10Mb and not in10 :
|
||||
self.eth_mode(('10','auto'))
|
||||
elif not self.prise10Mb and in10 :
|
||||
self.eth_mode('auto')
|
||||
if self.prise10Mb and not in10 :
|
||||
self.eth_mode(('10','auto'))
|
||||
elif not self.prise10Mb and in10 :
|
||||
self.eth_mode('auto')
|
||||
|
||||
nom = 'Chambre_%s' % self.chbre.capitalize()
|
||||
if nom != self.nom() :
|
||||
self.nom(nom)
|
||||
nom = 'Chambre_%s' % self.chbre.capitalize()
|
||||
if nom != self.nom() :
|
||||
self.nom(nom)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue