Proyecto

General

Perfil

« Anterior | Siguiente » 

Revisión 365

Inicio del Proyecto

Ver diferencias:

ExportaCerts/trunk/Leeme.txt
EXPORTACERTS
================
CONTENIDO:
ExportaCerts.py: Programa principal. Obtiene datos de certificados de /var/lib/puppet/yaml/facts y del servidor LDAP, generando dos archivos csv
que posteriormente pueden ser importados en la aplicacion CONTROLIES para dar de alta de forma automatizada los portatiles de los centros.
Debeis modificar la IP que hace referencia al ldap, en la linea l = LdapConnection ("172.23.36.5"), y poner la de vuestro LDAP.
Asimismo, debeis añadir la linea "by * read" en /etc/ldap/slapd.conf del LDAP para permitir obtener el campo "employeeID" de forma anonima, como
sigue:
access to attrs=employeeNumber,jpegPhoto
by dn="cn=replica,dc=instituto,dc=extremadura,dc=es" read
by dn="cn=interno,dc=instituto,dc=extremadura,dc=es" read
by * read
LdapConnection.py: Utilidades para conectar y buscar en LDAP.
ExportaCerts/trunk/LdapConnection.py
##############################################################################
# -*- coding: utf-8 -*-
# Project: ControlIES
# Module: LdapConnection.py
# Purpose: Connection with ldap server
# Language: Python 2.5
# Date: 7-Feb-2011.
# Ver: 7-Feb-2011.
# Author: Manuel Mora Gordillo
# Copyright: 2011 - Manuel Mora Gordillo <manuito @no-spam@ gmail.com>
#
# ControlIES 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.
# ControlIES 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.
# You should have received a copy of the GNU General Public License
# along with ControlAula. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import ldap
class LdapConnection(object):
def __init__(self):
self.host = "172.23.36.5"
self.user = "admin"
self.passwd = "Sta1987teleco"
def setCredentials(self,host,user,passwd):
self.host = host
self.user = user
self.passwd = passwd
def validation(self):
if self.host == "":
return "host"
if self.user == "":
return "user"
if self.passwd == "":
return "password"
return "OK"
def process(self):
val = self.validation()
if val != "OK":
return val
auth = self.connect()
return auth
def connect2(self):
## first you must open a connection to the server
try:
self.connection = ldap.open("172.23.36.5")
self.connection.protocol_version = ldap.VERSION3
except ldap.LDAPError, e:
print e
def search2(self,baseDN,filter,retrieveAttributes):
searchScope = ldap.SCOPE_SUBTREE
try:
ldap_result_id = self.connection.search(baseDN+ ",dc=instituto,dc=extremadura,dc=es", ldap.SCOPE_SUBTREE, filter, retrieveAttributes)
result_set = []
while 1:
result_type, result_data = self.connection.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
def getConnect(self):
return self.connection
def add(self,baseDN,attr):
try:
self.connection.add_s(baseDN+",dc=instituto,dc=extremadura,dc=es", attr)
except ldap.ALREADY_EXISTS:
logging.getLogger().debug("LDAP already exists %s" % (baseDN))
except ldap.OPERATIONS_ERROR:
logging.getLogger().debug("LDAP operation error %s" % (baseDN))
except ldap.NO_SUCH_OBJECT:
logging.getLogger().debug("LDAP no such object %s" % (baseDN))
return True
def modify(self,baseDN,attr):
try:
self.connection.modify_s(baseDN+",dc=instituto,dc=extremadura,dc=es", attr)
except ldap.OPERATIONS_ERROR:
print "error"
except ldap.NO_SUCH_OBJECT:
print "no_such_object"
except Exception,e:
print e
return True
def delete(self,baseDN):
try:
self.connection.delete_s(baseDN+",dc=instituto,dc=extremadura,dc=es")
except ldap.OPERATIONS_ERROR:
print "error"
except ldap.NO_SUCH_OBJECT:
print "no_such_object"
return True
def close(self):
self.connection.unbind()
ExportaCerts/trunk/ExportaCerts.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
import os
import datetime
l = LdapConnection()
l.connect2()
#leer archivos
path = '/var/lib/puppet/yaml/facts/'
listing = os.listdir(path)
# ficheros de intercambio para profesores y alumnos
fp = open('./profesores.csv','w')
fa = open('./alumnos.csv','w')
for infile in listing:
# leer primero datos del archivo de certificado
f = open(path+infile)
quienes="alumno"
registro=""
line = f.readline()
serialnumber=""
marcamodelo="1"
username=infile.replace(".santaeulalia.yaml", "")
while line:
if "serialnumber" in line:
serialnumber=line.replace(" serialnumber: ", "")
serialnumber=serialnumber.replace(" ", "")
serialnumber=serialnumber.replace("\n","")
serialnumber=serialnumber.replace("serialnumber:", "")
if "manufacturer" in line:
manufacturer=line.replace(" manufacturer: ", "")
manufacturer=manufacturer.replace(" ", "")
manufacturer=manufacturer.replace('"',"")
manufacturer=manufacturer.replace("\n","")
marcamodelo="2"
if "Acer" in manufacturer:
marcamodelo="1"
line = f.readline()
# buscar en ldap datos restantes. Inicialmente, suponemos que se trata de un alumno
filter="(homeDirectory=/home/alumnos/" + username + ")"
search = l.search2("ou=People",filter,["cn","employeeNumber","homeDirectory"])
# si no, es un profe
if not search:
quienes="profesor"
filter="(homeDirectory=/home/profesor/" + username + ")"
search = l.search2("ou=People",filter,["cn","employeeNumber","homeDirectory"])
# añadir datos a linea
if search:
uid=search[0][0][1]['employeeNumber'][0]
logname=search [0][0][1]["cn"][0]
if serialnumber:
registro=registro+"PORTATIL, '"+serialnumber+"', "+marcamodelo + ", "
now = datetime.datetime(2009,5,5)
str_now = now.date().isoformat()
registro=registro+"HISTORICO, " + str_now + "', '" + uid + "', 2, '', 1, '" + username + "', '" + logname + "'"
if quienes=="alumno":
fa.write(registro+'\n')
else:
fp.write(registro+'\n')
print registro
f.close()
fp.close()
fa.close()
#print registro

Exportar a: Unified diff