Répare stats vlans, utilise lc_ldap et hptools2
This commit is contained in:
parent
93d6b2d175
commit
ae803ba2c3
1 changed files with 51 additions and 52 deletions
|
@ -1,4 +1,4 @@
|
|||
#!/usr/bin/env python
|
||||
#!/bin/bash /usr/scripts/python.sh
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
u"""
|
||||
|
@ -11,25 +11,25 @@ S'il est impossbile de se connecter à un switch, on abandonne le batiment conce
|
|||
d'autre VLANs qu'"adherent", ainsi que les machines connectées sur ces prises.
|
||||
|
||||
Xavier Lagorce - 2009
|
||||
Gabriel Détraz - 2015
|
||||
|
||||
Réparé en 2015, utilise à présent lc_ldap et hptools2
|
||||
|
||||
Réparé en 2015, le script remarche excepté la recherche par mac, remplacée par défaut par
|
||||
la recherche du propriétaire de la chambre (création de l'option -m)
|
||||
"""
|
||||
|
||||
import sys, getopt
|
||||
from sys import path
|
||||
|
||||
path.append('/usr/scripts/gestion')
|
||||
|
||||
from annuaires_pg import chbre_prises, bat_switchs
|
||||
from affich_tools import *
|
||||
from ldap_crans import crans_ldap
|
||||
from ldap_crans import MachineCrans, MachineWifi, BorneWifi
|
||||
from hptools import hpswitch, ConversationError
|
||||
from gestion.annuaires_pg import chbre_prises, bat_switchs
|
||||
from gestion.affich_tools import *
|
||||
from gestion.hptools2 import HPSwitch
|
||||
from gestion.config import vlans
|
||||
|
||||
from lc_ldap import shortcuts
|
||||
|
||||
# Accès à la base ldap
|
||||
db = crans_ldap()
|
||||
ldap = shortcuts.lc_ldap_readonly()
|
||||
|
||||
# On mets les vlans dans l'ordre id/nom
|
||||
vlandict = dict((v,k) for k,v in vlans.items())
|
||||
|
||||
def scan_bats(bats, annuaire=chbre_prises, verbose=False):
|
||||
"""Fonction permettant de scanner un certain nombre de switchs
|
||||
|
@ -76,7 +76,7 @@ def scan_bats(bats, annuaire=chbre_prises, verbose=False):
|
|||
current_switch = int(prise[0])
|
||||
if verbose: print u"Connexion à " + switch_name + u" ..."
|
||||
try:
|
||||
switch = hpswitch(switch_name)
|
||||
switch = HPSwitch(switch_name)
|
||||
except ValueError:
|
||||
if verbose:
|
||||
print u"\tImpossible de se connecter à ce switch, abandon du bâtiment..."
|
||||
|
@ -87,12 +87,11 @@ def scan_bats(bats, annuaire=chbre_prises, verbose=False):
|
|||
|
||||
# On récupère les VLANs présents sur la prise
|
||||
try:
|
||||
vlans = switch.vlans(prise[1:])
|
||||
vlans = switch.get_vlans(int(prise[1:]))
|
||||
if verbose:
|
||||
print vlans
|
||||
print prise[1:]
|
||||
for vlan in vlans:
|
||||
print vlan
|
||||
except ConversationError:
|
||||
except:
|
||||
if verbose:
|
||||
print u"\tErreur de communication avec " + switch_name
|
||||
print u"\tAbandon du bâtiment..."
|
||||
|
@ -101,18 +100,18 @@ def scan_bats(bats, annuaire=chbre_prises, verbose=False):
|
|||
|
||||
if vlans:
|
||||
# Si la prise ne possède que le VLAN adherent, on ne va pas plus loin
|
||||
if len(vlans) == 1 and vlans[0] == u"adherent":
|
||||
vlans_stats[vlans[0]] = vlans_stats[vlans[0]] + 1
|
||||
if len(vlans) == 1 and vlans[0] == 1:
|
||||
vlans_stats[vlandict[vlans[0]]] = vlans_stats[vlandict[vlans[0]]] + 1
|
||||
else:
|
||||
# Sinon, on récupère tous les VLANs
|
||||
for vlan in vlans:
|
||||
if not (vlan in vlans_stats):
|
||||
vlans_stats[vlan] = 0
|
||||
vlans_stats[vlan] = vlans_stats[vlan] + 1
|
||||
vlans_stats[vlandict[vlan]] = 0
|
||||
vlans_stats[vlandict[vlan]] = vlans_stats[vlandict[vlan]] + 1
|
||||
# On récupère les données pour les afficher plus tard
|
||||
try:
|
||||
if macres:
|
||||
macs = switch.show_prise_mac(int(prise[1:]))
|
||||
macs = switch.show_port_macs(int(prise[1:]))[0]
|
||||
else:
|
||||
macs = []
|
||||
except ConversationError:
|
||||
|
@ -124,37 +123,37 @@ def scan_bats(bats, annuaire=chbre_prises, verbose=False):
|
|||
|
||||
if macs:
|
||||
for mac in macs:
|
||||
fm = db.search("mac=%s" % mac)
|
||||
if len(fm["machine"]) != 0:
|
||||
m = fm["machine"][0]
|
||||
if isinstance(m, MachineWifi): t = u"Wifi"
|
||||
elif isinstance(m, BorneWifi): t = u"Born"
|
||||
else : t = u"Fixe"
|
||||
adh = m.proprietaire()
|
||||
nom = adh.Nom()
|
||||
machine = ldap.search(u"macAddress=%s" % mac)
|
||||
try:
|
||||
machine = machine[0]
|
||||
adh = machine.proprio()
|
||||
chbre = bat.upper() + prises[prise]
|
||||
data.append([bat.upper() + prise,
|
||||
adh.chbre(),
|
||||
adh.Nom(),
|
||||
m.nom().split('.')[0],
|
||||
t,
|
||||
', '.join(sorted(vlans))])
|
||||
chbre,
|
||||
unicode(adh['nom'][0]),
|
||||
unicode(machine['host'][0]),
|
||||
machine.ldap_name,
|
||||
', '.join(sorted([vlandict[vlan] for vlan in vlans]))])
|
||||
except IndexError:
|
||||
pass
|
||||
|
||||
# Si aucune machine n'est connectée, on ajoute quand même une
|
||||
# entrée dans les résultats
|
||||
else:
|
||||
# On tente de trouver le nom de l'adherent :
|
||||
chbre = bat.upper() + prises[prise]
|
||||
fm = db.search("chbre=%s" % chbre)
|
||||
if len(fm['adherent']) != 0:
|
||||
nom = fm['adherent'][0].Nom()
|
||||
elif len(fm['club']) != 0:
|
||||
nom = fm['club'][0].Nom()
|
||||
data.append([bat.upper() + prise,
|
||||
chbre,
|
||||
nom,
|
||||
'-',
|
||||
'-',
|
||||
', '.join(sorted(vlans))])
|
||||
adh = ldap.search(u"chbre=%s" % chbre)
|
||||
try:
|
||||
adh = adh[0]
|
||||
nom = unicode(adh['nom'][0])
|
||||
data.append([bat.upper() + prise,
|
||||
chbre,
|
||||
nom,
|
||||
'-',
|
||||
'-',
|
||||
', '.join(sorted([vlandict[vlan] for vlan in vlans]))])
|
||||
except IndexError:
|
||||
pass
|
||||
|
||||
# On renvoie les résultats
|
||||
return (vlans_stats,data,erreurs)
|
||||
|
@ -217,7 +216,7 @@ if __name__ == "__main__":
|
|||
|
||||
bats = ''
|
||||
all_prises = False
|
||||
macres = False
|
||||
macres = True
|
||||
|
||||
for opt, val in options:
|
||||
if opt == '-a' or opt == '--all':
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue