Gestion switchs par snmp.
darcs-hash:20040919204104-41617-9d15a60e1df1bdf65ac3854b9b94a21474a883ea.gz
This commit is contained in:
parent
3b643b222b
commit
af2c3f643e
1 changed files with 48 additions and 56 deletions
|
@ -17,24 +17,20 @@
|
||||||
|
|
||||||
import string, sys, os, commands, smtplib
|
import string, sys, os, commands, smtplib
|
||||||
|
|
||||||
import snmp
|
|
||||||
|
|
||||||
sys.path.append('/usr/scripts/gestion')
|
sys.path.append('/usr/scripts/gestion')
|
||||||
|
from hptools import hpswitch, sw_chbre
|
||||||
from ldap_crans import crans_ldap, ann_scol
|
from ldap_crans import crans_ldap, ann_scol
|
||||||
from annuaires import chbre_prises, uplink_prises, reverse
|
from annuaires import chbre_prises, uplink_prises, reverse
|
||||||
from gen_confs import *
|
from gen_confs import *
|
||||||
from time import localtime
|
from time import localtime
|
||||||
# from qqch import switchs # Liste des switchs
|
# Liste des switchs
|
||||||
bat_switchs = [ 'b', 'c', 'h', 'i', 'g' ]
|
bat_switchs = [ 'b', 'c' , 'h' , 'i' , 'g' , 'j' ]
|
||||||
|
|
||||||
sys.path.append('/CRANS/code')
|
|
||||||
from hptools import hp
|
|
||||||
|
|
||||||
class switch(gen_config) :
|
class switch(gen_config) :
|
||||||
# Répertoire ou écire les fichiers de conf
|
# Répertoire ou écire les fichiers de conf
|
||||||
CONF_REP='/var/tftp_switchs/' # avec un / derrière
|
CONF_REP='/var/tftp_switchs/' # avec un / derrière
|
||||||
|
|
||||||
config = """; J4899A Configuration Editor; Created on release #H.07.32
|
config = """; %(modele)s Configuration Editor; Created on release #H.07.32
|
||||||
|
|
||||||
hostname "%(switch)s"
|
hostname "%(switch)s"
|
||||||
;-------------------------------------------------------- Snmp
|
;-------------------------------------------------------- Snmp
|
||||||
|
@ -68,8 +64,8 @@ vlan 1
|
||||||
ip igmp
|
ip igmp
|
||||||
no ip igmp querier
|
no ip igmp querier
|
||||||
exit
|
exit
|
||||||
%(MAC_FILTER)s
|
|
||||||
;-------------------------------------------------------- Accès d'adminsitration
|
;-------------------------------------------------------- Accès d'adminsitration
|
||||||
|
no telnet-server
|
||||||
no web-management
|
no web-management
|
||||||
aaa authentication ssh login public-key
|
aaa authentication ssh login public-key
|
||||||
ip ssh
|
ip ssh
|
||||||
|
@ -86,8 +82,6 @@ no spanning-tree %(uplinks)s edge-port
|
||||||
spanning-tree %(non_uplinks)s point-to-point-mac auto
|
spanning-tree %(non_uplinks)s point-to-point-mac auto
|
||||||
spanning-tree %(non_uplinks)s priority 15
|
spanning-tree %(non_uplinks)s priority 15
|
||||||
no cdp run
|
no cdp run
|
||||||
;-------------------------------------------------------- Avec mot de passe ;)
|
|
||||||
password manager
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
interface_template = """interface %(prise)i%(etat)s
|
interface_template = """interface %(prise)i%(etat)s
|
||||||
|
@ -100,8 +94,8 @@ exit
|
||||||
|
|
||||||
def __init__(self,truc):
|
def __init__(self,truc):
|
||||||
""" truc est soit :
|
""" truc est soit :
|
||||||
* une liste de chambres => reconfig de ces chambres
|
* une _liste_ de chambres => reconfig de ces chambres
|
||||||
* un nom de switch => reconfig de ce swith"""
|
* un _tulpe_ de noms de switch => reconfig de ces swiths"""
|
||||||
self.db = crans_ldap() # connexion LDAP
|
self.db = crans_ldap() # connexion LDAP
|
||||||
if type(truc) == list :
|
if type(truc) == list :
|
||||||
# On enlève la chambre "CRA" et "????"
|
# On enlève la chambre "CRA" et "????"
|
||||||
|
@ -115,8 +109,15 @@ exit
|
||||||
return 'switchs'
|
return 'switchs'
|
||||||
|
|
||||||
def restart(self) :
|
def restart(self) :
|
||||||
# Rien à faire ici
|
if self.chbre :
|
||||||
pass
|
# Tout est déja fait
|
||||||
|
return
|
||||||
|
####### Vu qu'il n'y a pas de serveur tftp ici
|
||||||
|
# on excécute pas le truc en dessous
|
||||||
|
#for switch in self.switch :
|
||||||
|
# self.aff = anim('\treboot de %s' % switch)
|
||||||
|
# sw = hptools.switch(switch)
|
||||||
|
# sw.update()
|
||||||
|
|
||||||
def gen_conf(self) :
|
def gen_conf(self) :
|
||||||
if self.chbres :
|
if self.chbres :
|
||||||
|
@ -124,47 +125,36 @@ exit
|
||||||
for chbre in self.chbres :
|
for chbre in self.chbres :
|
||||||
self.configure_chbre(chbre)
|
self.configure_chbre(chbre)
|
||||||
elif self.switch :
|
elif self.switch :
|
||||||
self.configure_switch(self.switch)
|
for switch in self.switch :
|
||||||
|
self.configure_switch(switch)
|
||||||
|
|
||||||
def configure_chbre(self,chbre) :
|
def configure_chbre(self,chbre) :
|
||||||
""" Recontigure la chambre fournie chambre """
|
""" Recontigure la chambre fournie chambre """
|
||||||
bat = chbre[0].lower()
|
|
||||||
a = self.db.search('chbre=%s&paiement=%i' % (chbre,ann_scol))
|
|
||||||
a = a['adherent'] + a['club']
|
|
||||||
if a and 'bloq' not in a[0].blacklist_actif() :
|
|
||||||
# Il faut activer la prise
|
|
||||||
anim('\tactivation chbre %s' % chbre)
|
|
||||||
action = 'enable'
|
|
||||||
else :
|
|
||||||
# Il faut désactiver la prise
|
|
||||||
anim('\tdésactivation chbre %s' % chbre)
|
|
||||||
action = 'disable'
|
|
||||||
|
|
||||||
try :
|
try :
|
||||||
if bat in bat_switchs :
|
bat = chbre[0].lower()
|
||||||
# Action sur le switch
|
prise = sw_chbre(chbre)
|
||||||
prise = chbre_prises[bat][chbre[1:].lower()]
|
a = self.db.search('chbre=%s&paiement=%i' % (chbre,ann_scol))
|
||||||
sw=hp(bat,int(prise[0]))
|
a = a['adherent'] + a['club']
|
||||||
if prise[-1]=='-' :
|
sujet = ''
|
||||||
prise=prise[:-1]
|
if a and 'bloq' not in a[0].blacklist_actif() :
|
||||||
sw.set_prise(int(prise[1:4]),'speed-duplex auto-10')
|
# Il faut activer la prise
|
||||||
r = sw.set_prise(int(prise[1:4]),action)
|
anim('\tactivation chbre %s' % chbre)
|
||||||
sw.close()
|
if bat in bat_switchs : prise.enable()
|
||||||
if not r :
|
else : sujet = "Chambre %s à brancher." % chbre
|
||||||
raise RuntimeError('Erreur de communiquation')
|
|
||||||
else :
|
else :
|
||||||
# Mail au bat
|
# Il faut désactiver la prise
|
||||||
|
anim('\tdésactivation chbre %s' % chbre)
|
||||||
|
if bat in bat_switchs : prise.disable()
|
||||||
|
else : sujet = "Chambre %s à débrancher." % chbre
|
||||||
|
|
||||||
|
if sujet :
|
||||||
|
# Il faut envoyer un mail
|
||||||
To = "clef%s@crans.org" % bat
|
To = "clef%s@crans.org" % bat
|
||||||
From = To
|
From = To
|
||||||
conn=smtplib.SMTP('localhost')
|
conn=smtplib.SMTP('localhost')
|
||||||
txt_mail = "From: Crans scripts <%(From)s>\n"
|
txt_mail = "From: Crans scripts <%(From)s>\n"
|
||||||
txt_mail+= "To: %(To)s\n"
|
txt_mail+= "To: %(To)s\n"
|
||||||
txt_mail+= "Subject: (CRANS) "
|
txt_mail+= "Subject: (CRANS) %s\n\nMerci." % sujet
|
||||||
if action == 'disable':
|
|
||||||
txt_mail+= "Chambre %s à débrancher." % chbre
|
|
||||||
else:
|
|
||||||
txt_mail+= "Chambre %s à brancher." % chbre
|
|
||||||
txt_mail+= '\n\nMerci.'
|
|
||||||
conn.sendmail(From, To , txt_mail % { 'From' : From, 'To' : To })
|
conn.sendmail(From, To , txt_mail % { 'From' : From, 'To' : To })
|
||||||
conn.quit()
|
conn.quit()
|
||||||
print OK
|
print OK
|
||||||
|
@ -197,7 +187,7 @@ exit
|
||||||
def __configure_switch(self,switch) :
|
def __configure_switch(self,switch) :
|
||||||
""" Génère le fichier de conf du switch donné """
|
""" Génère le fichier de conf du switch donné """
|
||||||
warn = ''
|
warn = ''
|
||||||
conn = snmp.hptools(switch)
|
conn = hpswitch(switch)
|
||||||
def add_prise(dict, key, prise) :
|
def add_prise(dict, key, prise) :
|
||||||
""" ajoute la prise à la liste de prise du dictionnaire dict
|
""" ajoute la prise à la liste de prise du dictionnaire dict
|
||||||
dict[key] est ensuite de la forme 1,3-7,9-12 (ajout des prises
|
dict[key] est ensuite de la forme 1,3-7,9-12 (ajout des prises
|
||||||
|
@ -234,18 +224,20 @@ exit
|
||||||
except :
|
except :
|
||||||
sw_num = 0
|
sw_num = 0
|
||||||
|
|
||||||
# Nombre de prises
|
# Nombre de prises et modèle
|
||||||
nb_prises = conn.nb_prises()
|
nb_prises = conn.nb_prises()
|
||||||
if not nb_prises :
|
modele = conn.version()
|
||||||
raise RuntimeError("Erreur : impossible de déterminer le nombre de ports")
|
if not nb_prises or not modele :
|
||||||
|
raise RuntimeError("Erreur : impossible de déterminer les caractéristiques du switch.")
|
||||||
params['nb_prises'] = nb_prises
|
params['nb_prises'] = nb_prises
|
||||||
|
params['modele'] = modele.split()[1]
|
||||||
|
|
||||||
# IP
|
# IP
|
||||||
params['ip'] = commands.getoutput("host %s" % switch).split()[-1]
|
params['ip'] = commands.getoutput("host %s" % switch).split()[-1]
|
||||||
|
|
||||||
### Configuration prises
|
### Configuration prises
|
||||||
params['INTERFACES_CONF'] = ''
|
params['INTERFACES_CONF'] = ''
|
||||||
params['MAC_FILTER'] = ''
|
params['MAC_FILTER'] = '' # Pas pour l'instant
|
||||||
|
|
||||||
# Dictionnaire prise -> chambre
|
# Dictionnaire prise -> chambre
|
||||||
prise_chbres = reverse(bat)
|
prise_chbres = reverse(bat)
|
||||||
|
@ -271,7 +263,7 @@ exit
|
||||||
chbres = prise_chbres[annu_prise]
|
chbres = prise_chbres[annu_prise]
|
||||||
elif prise_chbres.has_key(annu_prise+'-') :
|
elif prise_chbres.has_key(annu_prise+'-') :
|
||||||
# Prise en 10
|
# Prise en 10
|
||||||
prise_params['speed'] = ' speed-duplex auto-10\n'
|
prise_params['speed'] = '\n speed-duplex auto-10'
|
||||||
chbres = prise_chbres[annu_prise+'-']
|
chbres = prise_chbres[annu_prise+'-']
|
||||||
else :
|
else :
|
||||||
# Prise non référencée dans l'annuaire
|
# Prise non référencée dans l'annuaire
|
||||||
|
@ -323,11 +315,11 @@ exit
|
||||||
return warn
|
return warn
|
||||||
|
|
||||||
if __name__ == '__main__' :
|
if __name__ == '__main__' :
|
||||||
if '-h' in sys.argv or '--help' in sys.argv or len(sys.argv) != 2 :
|
if '-h' in sys.argv or '--help' in sys.argv or len(sys.argv) == 1 :
|
||||||
print "%s <switch>" % sys.argv[0].split('/')[-1].split('.')[0]
|
print "%s <switch>" % sys.argv[0].split('/')[-1].split('.')[0]
|
||||||
print "Génération du fichier de configuration de switch donnée."
|
print "Génération du fichier de configuration des switchs donnés."
|
||||||
sys.exit(255)
|
sys.exit(255)
|
||||||
|
|
||||||
sw = switch(sys.argv[1])
|
sw = switch(tuple(sys.argv[1:]))
|
||||||
sw.debug = 1
|
sw.debug = 1
|
||||||
sw.reconfigure()
|
sw.reconfigure()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue