60 lines
1.7 KiB
Python
Executable file
60 lines
1.7 KiB
Python
Executable file
#! /usr/bin/env python
|
|
# -*- coding: iso-8859-15 -*-
|
|
|
|
# Ajout d'un whos aux mails d'arpwatch
|
|
# Auteur : Stéphane Glondu
|
|
# 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 = u"""
|
|
Recherche LDAP sur la MAC %(mac)s :
|
|
Nom de machine : %(nomMach)s , IP : %(ip)s, Prise : %(prise)s
|
|
Proprietaire : %(nomAdh)s, chambre %(chbre)s
|
|
""" % { "mac":mac, "nomMach":m.nom(), "ip":m.ip(), "prise":m.prise(),"nomAdh":adh.Nom(), "chbre":adh.chbre()}
|
|
s += r
|
|
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 += '\n'
|
|
texte += '\n'.join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))
|
|
texte += '\n-- \narpwatch_sendmail.py\n'
|
|
|
|
smtp = smtplib.SMTP()
|
|
smtp.connect()
|
|
smtp.sendmail("arpwatch@crans.org", recipients, texte)
|
|
smtp.quit()
|