scripts/surveillance/arpwatch_sendmail.py
Nicolas Dandrimont c54cfa7648 [arpwatch_sendmail] Un-flowage
Ignore-this: 5d9a5b8e070cd042a97e9aec5ac5f848

darcs-hash:20091011025111-ffbb2-a87900d8fce26fc8611f83382577f6bd72ae90a9.gz
2009-10-11 04:51:11 +02:00

49 lines
1.5 KiB
Python

#! /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, re, smtplib
from commands import getstatusoutput
sys.path.append('/usr/scripts/gestion/tools')
from locate_mac import trace_machine, format_mac, info_machine
find_mac = re.compile(r'[0-9A-Fa-f]{1,2}(?::[0-9A-Fa-f]{1,2}){5}')
def get_machine(unformated_mac):
mac = format_mac(unformated_mac)
return u"\n" + info_machine(mac) + u"\n" + trace_machine(mac)
if __name__ == "__main__":
texte = sys.stdin.read().decode('ISO-8859-15')
textes = texte.splitlines(True)
i = textes.index(u'\n')
textes[i-1:i-1] = [
u'MIME-Version: 1.0\n',
u'Content-Type: text/plain; charset=UTF-8\n',
u'Content-Transfer-Encoding: 8bit\n',
]
# 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:
textes.append(get_machine(mac))
except:
# En cas d'exception, on envoie le traceback
import traceback
textes.append(u'\n')
textes.append(u''.join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback)))
textes.append('\n-- \narpwatch_sendmail.py\n')
smtp = smtplib.SMTP()
smtp.connect()
smtp.sendmail("arpwatch@crans.org", recipients, u''.join(textes).encode('UTF-8'))
smtp.quit()