65 lines
1.8 KiB
Python
Executable file
65 lines
1.8 KiB
Python
Executable file
#! /usr/bin/env python
|
|
# -*- coding: iso-8859-15 -*-
|
|
|
|
# Ajout d'un whos et d'un tracage aux mails d'arpwatch
|
|
# Auteurs : Stéphane Glondu, Cyril Cohen
|
|
# Licence : GPLv2
|
|
|
|
import sys, os, sre, smtplib
|
|
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')
|
|
import locate_mac
|
|
|
|
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):
|
|
mac = locate_mac.format_mac(unformated_mac)
|
|
return "\n" + info_machine(mac) + trace_machine(mac) + "\n"
|
|
|
|
if __name__ == "__main__":
|
|
texte = sys.stdin.read()
|
|
# On récupère les destinataires dans les arguments (très ad hoc)
|
|
recipients = sys.argv[2].split(',')
|
|
# On complète le message
|
|
try:
|
|
macs = find_mac.findall(texte)
|
|
for mac in macs:
|
|
texte += get_machine(mac)
|
|
except:
|
|
# En cas d'exception, on envoie le traceback
|
|
import traceback
|
|
texte += u'\n'
|
|
texte += u'\n'.join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))
|
|
texte += u'\n-- \narpwatch_sendmail.py\n'
|
|
|
|
smtp = smtplib.SMTP()
|
|
smtp.connect()
|
|
smtp.sendmail("arpwatch@crans.org", recipients, texte.encode('latin1'))
|
|
smtp.quit()
|