Factorisation de info_machine.

darcs-hash:20061211220244-68412-4f883702e90d2ed9d24b8c0fe41f62e035094960.gz
This commit is contained in:
glondu 2006-12-11 23:02:44 +01:00
parent 2f48dd6368
commit 867548324b
2 changed files with 46 additions and 50 deletions

View file

@ -1,18 +1,21 @@
#! /usr/bin/env python #! /usr/bin/env python
# -*- coding: iso-8859-15 -*- # -*- coding: iso-8859-15 -*-
import sys import sys, sre
sys.path.append('/usr/scripts/gestion')
from annuaires import reverse, all_switchs
from hptools import hpswitch
from time import sleep from time import sleep
from os import system from os import system
import threading import threading
# mise en forme d'une adresse mac sys.path.append('/usr/scripts/gestion')
from ldap_crans import crans_ldap
import whos
from annuaires import reverse, all_switchs
from hptools import hpswitch
# mise en forme d'une adresse mac
def format_mac(unformated_mac): def format_mac(unformated_mac):
return ":".join([i.zfill(2) for i in unformated_mac.split(":")]).lower() return str(":".join([i.zfill(2) for i in unformated_mac.split(":")]).lower())
# classe d'interrogation des switchs # classe d'interrogation des switchs
class interroge_switch (threading.Thread) : class interroge_switch (threading.Thread) :
@ -35,30 +38,47 @@ class interroge_switch (threading.Thread) :
if nom.find("uplink")==-1: if nom.find("uplink")==-1:
self.reponse = ("%-10s => prise %-2s : %s" % (self.switch.encode('iso-8859-15').replace('.adm.crans.org',''), str(prise), nom)) self.reponse = ("%-10s => prise %-2s : %s" % (self.switch.encode('iso-8859-15').replace('.adm.crans.org',''), str(prise), nom))
# interrogation des switchs en parallele
def get_trace(mac):
tableau=[] # Retourne les infos sur la machine (l'équivalent d'un whos, mais renvoie la
# chaîne de caractères)
def info_machine(mac):
s = u""
db = crans_ldap()
machines = db.search("mac=%s" % mac)['machine']
for m in machines:
r = whos.machine_details(m)
# On supprime les couleurs
r = sre.sub('\x1b\[1;([0-9]|[0-9][0-9])m', '', r)
s += r
if len(machines) == 0:
s = u"Recherche LDAP de la MAC %s : aucune machine trouvée\n" % mac
return s
# interrogation des switchs en parallele
def trace_machine(mac):
tableau = []
for switch in ['backbone'] + all_switchs(): for switch in ['backbone'] + all_switchs():
tableau.append(interroge_switch(switch, mac)) tableau.append(interroge_switch(switch, mac))
while len(threading.enumerate()) > 1 : while len(threading.enumerate()) > 1 :
sleep(1) sleep(1)
resultat ='tracage de %s\n' % mac resultat = u'Traçage de %s...\n' % mac
tracage='' tracage = u''
for t in tableau: for t in tableau:
if t.reponse: if t.reponse:
tracage += t.reponse +"\n" tracage += t.reponse + u"\n"
if tracage=='': if tracage == u'':
tracage = "mac inconnue des switchs" tracage = u"Adresse MAC inconnue des switchs\n"
return (resultat + tracage) return (resultat + tracage)
# on interroge les switchs et on fait un whos sur la mac # on interroge les switchs et on fait un whos sur la mac
if __name__ == '__main__': if __name__ == '__main__':
mac = format_mac(sys.argv[1]) mac = format_mac(sys.argv[1])
print get_trace(mac) print trace_machine(mac)
print system('/usr/scripts/gestion/whos.py -a mac=%s' % mac) print system('/usr/scripts/gestion/whos.py -a mac=%s' % mac)

View file

@ -8,43 +8,19 @@
import sys, os, sre, smtplib import sys, os, sre, smtplib
from commands import getstatusoutput from commands import getstatusoutput
sys.path.append('/usr/scripts/gestion')
from ldap_crans import crans_ldap
import whos
sys.path.append('/usr/scripts/gestion/tools') sys.path.append('/usr/scripts/gestion/tools')
import locate_mac from locate_mac import trace_machine, format_mac, info_machine
find_mac = sre.compile(r'[0-9A-Fa-f]{1,2}(?::[0-9A-Fa-f]{1,2}){5}') find_mac = sre.compile(r'[0-9A-Fa-f]{1,2}(?::[0-9A-Fa-f]{1,2}){5}')
def info_machine(mac):
s = u''
db = crans_ldap()
machines = db.search("mac=%s" % mac)['machine']
for m in machines:
adh = m.proprietaire()
r = whos.machine_details(m)
# On supprime les couleurs
r = sre.sub('\x1b\[1;([0-9]|[0-9][0-9])m', '', r)
s += r
if len(machines)==0 :
s += u"""
Recherche LDAP sur la MAC %s : aucune machine trouvee
""" % mac
return s
def trace_machine(mac):
return locate_mac.get_trace(mac)
def get_machine(unformated_mac): def get_machine(unformated_mac):
mac = locate_mac.format_mac(unformated_mac) mac = format_mac(unformated_mac)
return "\n" + info_machine(mac) + trace_machine(mac) + "\n" return u"\n" + info_machine(mac) + u"\n" + trace_machine(mac)
if __name__ == "__main__": if __name__ == "__main__":
texte = sys.stdin.read() texte = sys.stdin.read().decode('ISO-8859-15')
# On récupère les destinataires dans les arguments (très ad hoc) # On récupère les destinataires dans les arguments (très ad hoc)
recipients = sys.argv[2].split(',') recipients = sys.argv[2].split(',')
# On complète le message # On complète le message
@ -56,10 +32,10 @@ if __name__ == "__main__":
# En cas d'exception, on envoie le traceback # En cas d'exception, on envoie le traceback
import traceback import traceback
texte += u'\n' texte += u'\n'
texte += u'\n'.join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback)) texte += u''.join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))
texte += u'\n-- \narpwatch_sendmail.py\n' texte += '\n-- \narpwatch_sendmail.py\n'
smtp = smtplib.SMTP() smtp = smtplib.SMTP()
smtp.connect() smtp.connect()
smtp.sendmail("arpwatch@crans.org", recipients, texte.encode('latin1')) smtp.sendmail("arpwatch@crans.org", recipients, texte.encode('ISO-8859-15'))
smtp.quit() smtp.quit()