From 1b62a165dc4534c92806f8d166b5c46ff69b7a39 Mon Sep 17 00:00:00 2001 From: pauget Date: Sun, 5 Dec 2004 21:07:44 +0100 Subject: [PATCH] Changement dans la configuration des switchs darcs-hash:20041205200744-41617-38bb5d68e3ce60f2fdbc8baad9dfc7e714c00824.gz --- gestion/gen_confs/switchs.py | 148 +++++++++++++++++++++-------------- 1 file changed, 90 insertions(+), 58 deletions(-) diff --git a/gestion/gen_confs/switchs.py b/gestion/gen_confs/switchs.py index d982dbb7..e7ff73b3 100755 --- a/gestion/gen_confs/switchs.py +++ b/gestion/gen_confs/switchs.py @@ -21,6 +21,7 @@ sys.path.append('/usr/scripts/gestion') from hptools import hpswitch, sw_chbre from ldap_crans import crans_ldap from annuaires import chbre_prises, uplink_prises, reverse +from random import shuffle from gen_confs import * from time import localtime @@ -30,7 +31,7 @@ class switch(gen_config) : # Répertoire ou écire les fichiers de conf CONF_REP='/var/tftp_switchs/' # avec un / derrière - config = """; %(modele)s Configuration Editor; Created on release #H.07.32 + config = """; %(modele)s Configuration Editor; Created on release #H.08.53 hostname "%(switch)s" ;-------------------------------------------------------- Snmp @@ -64,16 +65,21 @@ vlan 1 ip igmp no ip igmp querier exit +vlan 2 + name "Wifi" + tagged %(ports_wifi)s + ip igmp blocked %(ports_wifi)s + exit ;-------------------------------------------------------- Accès d'adminsitration no telnet-server no web-management aaa authentication ssh login public-key +aaa authentication ssh enable public-key ip ssh ip ssh version 2 ip authorized-managers 138.231.136.0 255.255.255.0 ip authorized-managers 138.231.137.216 ip authorized-managers 138.231.137.215 -;STACKING_CONF ;-------------------------------------------------------- Spanning-tree spanning-tree ; Config des uplinks @@ -81,6 +87,15 @@ no spanning-tree %(uplinks)s edge-port ; Config des prises adhérent spanning-tree %(non_uplinks)s point-to-point-mac auto spanning-tree %(non_uplinks)s priority 15 +;-------------------------------------------------------- Serveurs radius +radius-server key %(radius_key)s +%(radius-serveurs)s +;-------------------------------------------------------- Filtrage mac +aaa port-access mac-based %(non_uplinks)s +aaa port-access mac-based %(non_uplinks)s addr-limit 32 +aaa port-access mac-based %(non_uplinks)s logoff-period 9999999 +aaa port-access mac-based addr-format multi-colon +;-------------------------------------------------------- Bricoles no cdp run no stack """ @@ -91,7 +106,9 @@ no stack no lacp exit """ - filtre_mac_template = "port-security %i learn-mode static address-limit 3 mac-address%s\n" + # Serveurs radius + rad_servs = [ '138.231.136.10' , '138.231.136.18', '138.231.136.6' ] + rad_template = "radius-server host %s\n" def __init__(self,truc): """ truc est soit : @@ -200,35 +217,16 @@ exit """ Génère le fichier de conf du switch donné """ warn = '' conn = hpswitch(switch) - def add_prise(dict, key, prise) : - """ 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 - 1, 3, 4, 5, 6, 7, 9, 10, 11, 12 dans l'ordre) - """ - if dict.has_key(key) : - l1 = dict[key].split(',')[-1] - l2 = dict[key].split('-')[-1] - if len(l1)>len(l2) : - # de la forme xxx-l2 - if int(l2)+1 == prise : - dict[key] = '-'.join(dict[key].split('-')[:-1]) + '-%i'% prise - else : - dict[key] += ',%i' % prise - else : - # de la forme xxx,l1 - if int(l1)+1 == prise : - dict[key] += '-%i' % prise - else : - dict[key] += ',%i' % prise - else : - dict[key] = str(prise) - - return dict - ### Récupération données du switch # Bat bat = switch[3].lower() - params = { 'switch' : switch, 'bat' : bat.upper() } + from secrets import radius_key + shuffle(self.rad_servs) + rad = self.rad_template * len(self.rad_servs) + params = { 'switch' : switch, 'bat' : bat.upper() , + 'uplinks' : [] , 'non_uplinks' : [] , + 'radius_key' : radius_key , + 'radius-serveurs' : rad[:-1] % tuple(self.rad_servs)} # Numéro du switch try : @@ -249,12 +247,10 @@ exit ### Configuration prises params['INTERFACES_CONF'] = '' - params['MAC_FILTER'] = '' # Pas pour l'instant # Dictionnaire prise -> chambre prise_chbres = reverse(bat) - mac_filter=[] self.aff.iter = nb_prises+1 for prise in range(1,nb_prises+1): @@ -266,11 +262,12 @@ exit if uplink_prises[bat].has_key(int(annu_prise)) : ### Prise d'uplink prise_params['nom'] = uplink_prises[bat][int(annu_prise)] - params = add_prise(params,'uplinks',prise) + params['uplinks'].append(prise) else : ### Prise adhérent - params = add_prise(params,'non_uplinks',prise) - + params['non_uplinks'].append(prise) + + # Interrogation de l'annuaire if prise_chbres.has_key(annu_prise) : chbres = prise_chbres[annu_prise] elif prise_chbres.has_key(annu_prise+'-') : @@ -285,41 +282,76 @@ exit ## Configuration de la prise adhérent if chbres : + # Nom prise_params['nom'] = 'Chambre' if len(chbres) > 1 : prise_params['nom'] += 's' - - macs = '' for chbre in chbres : prise_params['nom'] += '_' + chbre - - # Macs sur la prise - nb = 0 + + # Besoin d'activer la prise ? + act = 0 + for chbre in chbres : res = self.db.search('chbre=%s%s&paiement=ok' % (bat.upper(), chbre) ) - res = res['adherent'] + res['club'] - if nb>=3 : - warn += 'Trop de macs sur la prise %i\n' % prise - continue - if not res : - continue - elif len(res) == 1 : - res = res[0] - if 'bloq' in res.blacklist_actif() : continue - for m in res.machines() : - macs += ' ' + m.mac().encode('iso-8859-15').replace(':','') - nb += 1 - else : - warn += 'Plusieurs adherents dans la chambre %s%s\n' % (bat.upper(), chbre) - if not macs : + for res in res['adherent'] + res['club'] : + if 'bloq' not in res.blacklist_actif() : + act = 1 + break + + if not act : prise_params['etat']='\n disable' - else : - params['MAC_FILTER'] += self.filtre_mac_template % ( prise, macs ) params['INTERFACES_CONF'] += self.interface_template % prise_params - # Petites verif - if not params.has_key('uplinks') or not params.has_key('non_uplinks') : + # Petite verif + if not params['uplinks'] or not params['non_uplinks'] : raise RuntimeError('Switch sans uplink ou sans prise adhérent.') + + def mk_list(liste_prise) : + """ + transforme une liste de prises en une chaine pour le switch + exemple : 1, 3, 4, 5, 6, 7, 9, 10, 11, 12 => 1,3-7,9-12 + """ + liste_prise.sort() + + # initialisation + i = liste_prise.pop(0) + groupe = [ i, i ] + result = [] + liste_prise.append(9999) # apparaitra jamais dans la liste + + while liste_prise : + nouveau = liste_prise.pop(0) + if nouveau == groupe[1] + 1 : + groupe[1] += 1 + else : + # Ajout du groupe au résultat + if groupe[0] == groupe[1] : + result.append(str(groupe[0])) + else : + result.append('-'.join(map(str,groupe))) + # Réinit de groupe + groupe = [ nouveau, nouveau ] + + return ','.join(result) + + params['ports_wifi'] = list(params['uplinks']) + + # Borne wifi sur la prise ? + for m in self.db.search('host=*.wifi.crans.org&prise=%s%i*' % (bat.upper(), sw_num))['machine'] : + params['ports_wifi'].append(int(m.prise()[2:])) + + params['inv_ports_wifi'] = [] + for prise in range(1,nb_prises+1) : + if prise not in params['ports_wifi'] : + params['inv_ports_wifi'].append(prise) + + + # Conversion des listes + for key in [ 'ports_wifi', 'inv_ports_wifi', 'uplinks', 'non_uplinks' ] : + params[key] = mk_list(params[key]) + params['INTERFACES_CONF'] = params['INTERFACES_CONF'][:-1] + fd = self._open_conf(self.CONF_REP + switch + '.conf') fd.write(self.config % params) fd.close()