Skip to content

Instantly share code, notes, and snippets.

@juliobm
Created May 5, 2015 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliobm/c8339beb3edd546d432d to your computer and use it in GitHub Desktop.
Save juliobm/c8339beb3edd546d432d to your computer and use it in GitHub Desktop.
Detectar y avisar de nuevas concesiones IP en nuestra red windows DHCP
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import poplib, smtplib, datetime, os
import locale, unicodedata
from email.Parser import Parser
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import Encoders
def mandaemail(user,passw,smtp,aquien,subject,texto,adjunto):
msg = MIMEMultipart()
textomsg=MIMEText(texto)
msg['Subject']=subject
msg['From']=user
msg['To']=aquien
msg.attach(textomsg)
if adjunto<>"":
part = MIMEBase('application', "octet-stream")
part.set_payload( open(adjunto,"rb").read() )
Encoders.encode_base64(part)
(directorio, fichero) = os.path.split(adjunto)
part.add_header('Content-Disposition', 'attachment; filename="%s"'
% fichero)
msg.attach(part)
smtp=smtplib.SMTP(smtp)
smtp.login(user,passw)
smtp.sendmail(msg['From'],msg['To'],msg.as_string())
smtp.close()
def dhcp_log():
# el nombre del fichero log dhcp a pelo
locale.setlocale(locale.LC_TIME, '')
ruta = r'c:\windows\system32\dhcp'
dia = datetime.datetime.now().strftime('%a')
if datetime.datetime.now().isoweekday() == 3:
dia = 'Mier'
return ruta + '\DhcpSrvLog-' + dia + '.log'
#
fic_dhcp_hoy = dhcp_log()
#fic_dhcp_hoy = 'dhcpsrvlog-lun.log'
fic_fichados = 'dchp_fichados.txt'
dic={}
email=1
haynuevo=0
texnuevo=''
# Id., fecha, hora, descripción, dirección IP, nombre de host, dirección MAC
# cagemos id = 11 que tiene la mac
# leemos todos los pc que tenemos grabados
if os.path.exists(fic_fichados):
fic_abre_fichados = open(fic_fichados,"r")
lista=fic_abre_fichados.readlines()
for cada in lista:
cadena=cada.split(chr(9))[:-1]
if len(cadena)>0:
dic[cadena[0]]=cada[:-1]
fic_abre_fichados.close()
# vemos los pcs asignados del log dhcp
if os.path.exists(fic_dhcp_hoy):
fic_abre_dhcp_hoy = open(fic_dhcp_hoy,"r")
for row in fic_abre_dhcp_hoy.readlines():
fila=row.split(",")
try:
pc = fila[5]
#print fila
mac = fila[6]
ip = fila[4]
fecha = fila[1]
hora = fila[2]
id = fila[0]
except:
continue
if pc=='' and mac<>'':
pc=mac
if pc <> '' and not pc in dic:
if id=='11' or id=='10':
dic[pc] = pc + chr(9) + ip + chr(9) + mac + chr(9) + fecha + chr(9) + hora
haynuevo += 1
texnuevo = texnuevo + pc.split(".")[0] + '_' + mac + '+'
# grabamos nueva lista pcs grabados
if haynuevo>0:
fic_abre_fichados = open(fic_fichados,"w")
ordenado=sorted(dic.items())
#print ordenado
for cada in ordenado:
#print dic[cada[0]]
fic_abre_fichados.write(dic[cada [0]] + '\n' )
fic_abre_fichados.close()
# mandamos email de aviso
if email>0:
asunto = 'dhcp_nuevos_'+str(haynuevo)
texto = texnuevo
adjunto = fic_fichados
aquien='a@quienquieras.com'
mandaemail("remitente","passw","smtp",aquien,asunto,texto,adjunto)
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment