Revisión 86
Añadido por Francisco Damián Méndez Palma hace alrededor de 14 años
EnciendeEquipos/NetworkUtils.py | ||
---|---|---|
##############################################################################
|
||
# -*- coding: utf-8 -*-
|
||
# Project: EnciendeEquipos
|
||
# Module: NetworkUtils.py
|
||
# Purpose: Network utilities
|
||
# Language: Python 2.5
|
||
# Date: 17-Jan-2010.
|
||
# Ver: 17-Jan-2010.
|
||
# Author: José L. Redrejo Rodríguez
|
||
# Copyright: 2009 - José L. Redrejo Rodríguez <jredrejo @nospam@ debian.org>
|
||
#
|
||
# EnciendeEquipos is free software: you can redistribute it and/or modify
|
||
# it under the terms of the GNU General Public License as published by
|
||
# the Free Software Foundation, either version 3 of the License, or
|
||
# (at your option) any later version.
|
||
# EnciendeEquipos is distributed in the hope that it will be useful,
|
||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
# GNU General Public License for more details.
|
||
#
|
||
##############################################################################
|
||
|
||
import socket,struct
|
||
|
||
def startup(address):
|
||
|
||
addr_byte = address.split(':')
|
||
hw_addr = struct.pack('BBBBBB', int(addr_byte[0], 16),
|
||
int(addr_byte[1], 16),
|
||
int(addr_byte[2], 16),
|
||
int(addr_byte[3], 16),
|
||
int(addr_byte[4], 16),
|
||
int(addr_byte[5], 16))
|
||
|
||
msg = '\xff' * 6 + hw_addr * 16
|
||
|
||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||
try:
|
||
s.sendto(msg, ('<broadcast>', 9))
|
||
s.sendto(msg, ('<broadcast>', 7))
|
||
s.sendto(msg, ('192.168.0.255', 9))
|
||
s.sendto(msg, ('192.168.0.255', 7))
|
||
except:
|
||
s.sendto(msg, ('<broadcast>', 2000))
|
||
s.sendto(msg, ('192.168.0.255', 2000))
|
||
s.close()
|
EnciendeEquipos/trunk/Leeme.txt | ||
---|---|---|
ENCIENDE EQUIPOS
|
||
================
|
||
|
||
CONTENIDO:
|
||
|
||
EnciendeEquipos.py: Programa principal. Realiza consulta anonima a LDAP, obteniendo las MAC's de los equipos que quieren encenderse. Utiliza dos filtros
|
||
distintos para realizar la consulta: uno se refiere a los equipos del diurno y otro a los del nocturno, podeis modificarlos a vuestro antojo e incluir los
|
||
equipos que querais. La consulta para obtener las MAC'S la hace en la rama host del LDAP. Debeis modificar la IP que hace referencia al ldap, en la linea
|
||
l = LdapConnection ("172.23.36.5"), y poner la de vuestro LDAP.
|
||
|
||
NetworkUtils.py: Contiene funcion para el encendido de un equipo, a partir de su mac. Esta funcion ha sido programada por J.L. Redrejo.
|
||
|
||
LdapConnection.py: Utilidades para conectar y buscar en LDAP.
|
||
|
||
|
||
INSTRUCCIONES DE USO:
|
||
|
||
Copiar carpeta EnciendeEquipos en /usr/bin del servidor. Modificar crontab para que se lance en el momento del dia que nos interese:
|
||
|
||
20 8 * * 1-5 root /usr/bin/python /usr/bin/EnciendeEquipos/EnciendeEquipos.py (para el dia)
|
||
|
||
35 17 * * 1-5 root /usr/bin/python /usr/bin/EnciendeEquipos/EnciendeEquipos.py Noche (para la noche)
|
EnciendeEquipos/EnciendeEquipos.py | ||
---|---|---|
##############################################################################
|
||
# -*- coding: utf-8 -*-
|
||
# Project: EnciendeEquipos
|
||
# Module: EnciendeEquipos.py
|
||
# Purpose: Wakes up classrom servers in an IES environment
|
||
# Language: Python 2.5
|
||
# Date: 23-Feb-2011.
|
||
# Ver: 23-Feb-2011.
|
||
# Author: Francisco Mendez Palma
|
||
# Copyright: 2011 - Francisco Mendez Palma <fmendezpalma @no-spam@ gmail.com>
|
||
#
|
||
# EnciendeEquipos is free software: you can redistribute it and/or modify
|
||
# it under the terms of the GNU General Public License as published by
|
||
# the Free Software Foundation, either version 3 of the License, or
|
||
# (at your option) any later version.
|
||
# EnciendeEquipos is distributed in the hope that it will be useful,
|
||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
# GNU General Public License for more details.
|
||
#
|
||
##############################################################################
|
||
|
||
from LdapConnection import LdapConnection
|
||
import NetworkUtils
|
||
import sys
|
||
|
||
# Capturo argumento para saber que ordenadores encender: Nocturno ("Noche") o Diurno (vacio)
|
||
if len (sys.argv)==2:
|
||
cuando=sys.argv[1]
|
||
else:
|
||
cuando="Dia"
|
||
|
||
#Conexion anonima: solo ip del ldap
|
||
l = LdapConnection("172.23.36.5")
|
||
l.connect()
|
||
|
||
#Segun momento del dia, encendemos distintos equipos
|
||
if cuando=="Noche":
|
||
filter="(|(cn=d-profesores*)(cn=a02-pro)(cn=a03-pro)(cn=a15-pro)(cn=a16-pro)(cn=a17-pro)(cn=a18-pro)(cn=a32-pro)(cn=a33-pro)(cn=a34-pro)(cn=a35-pro))"
|
||
else:
|
||
filter="(|(cn=*-pro*)(cn=d-profesores*))"
|
||
|
||
search = l.search("ou=hosts",filter,["macAddress"])
|
||
|
||
#Obtenemos macs y lanzamos encendido
|
||
for elto in search:
|
||
mac=elto[0][1]["macAddress"][0]
|
||
NetworkUtils.startup(mac)
|
EnciendeEquipos/LdapConnection.py | ||
---|---|---|
##############################################################################
|
||
# -*- coding: utf-8 -*-
|
||
# Project: EnciendeEquipos
|
||
# Module: LdapConnection.py
|
||
# Purpose: Anonymous Connection with ldap server
|
||
# Language: Python 2.5
|
||
# Date: 23-Feb-2011.
|
||
# Ver: 23-Feb-2011.
|
||
# Author: Francisco Mendez Palma
|
||
# Copyright: 2011 - Francisco Mendez Palma <fmendezpalma @no-spam@ gmail.com>
|
||
#
|
||
# EnciendeEquipos is free software: you can redistribute it and/or modify
|
||
# it under the terms of the GNU General Public License as published by
|
||
# the Free Software Foundation, either version 3 of the License, or
|
||
# (at your option) any later version.
|
||
# EnciendeEquipos is distributed in the hope that it will be useful,
|
||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
# GNU General Public License for more details.
|
||
#
|
||
##############################################################################
|
||
|
||
import ldap
|
||
|
||
class LdapConnection(object):
|
||
|
||
def __init__(self,host):
|
||
self.host = host
|
||
|
||
def connect(self):
|
||
## first you must open a connection to the server
|
||
|
||
try:
|
||
self.connect=ldap.open(self.host)
|
||
## searching doesn't require a bind in LDAP V3. If you're using LDAP v2, set the next line appropriately
|
||
## and do a bind as shown in the above example.
|
||
# you can also set this to ldap.VERSION2 if you're using a v2 directory
|
||
# you should set the next option to ldap.VERSION2 if you're using a v2 directory
|
||
self.protocol_version = ldap.VERSION3
|
||
except ldap.LDAPError, e:
|
||
return False
|
||
# handle error however you like
|
||
|
||
return True
|
||
|
||
def search(self,baseDN,filter,retrieveAttributes):
|
||
|
||
try:
|
||
ldap_result_id = self.connect.search(baseDN+",dc=instituto,dc=extremadura,dc=es", ldap.SCOPE_SUBTREE, filter, retrieveAttributes)
|
||
result_set = []
|
||
while 1:
|
||
result_type, result_data = self.connect.result(ldap_result_id, 0)
|
||
if (result_data == []):
|
||
break
|
||
else:
|
||
if result_type == ldap.RES_SEARCH_ENTRY:
|
||
result_set.append(result_data)
|
||
return result_set
|
||
except ldap.LDAPError, e:
|
||
print e
|
Exportar a: Unified diff
Encendido de Servidores de Aula