Proyecto

General

Perfil

Descargar (14,9 KB) Estadísticas
| Rama: | Revisión:
{
Adtos 1.6 Software de gestión de Aulas Tecnológicas
Copyright (C) 2008-20012, Manuel Narváez Martínez
Programación: Manuel Narváez Martínez <mnarvaez007@gmail.com>
Diseño gráfico: Ana María Zamora Moreno
Desarrollado con Lazarus 0.9.26

This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
}
unit clservidor;

{$mode objfpc}{$H+}

interface

uses
unix,Classes, SysUtils,IniFiles,process,clcomandosroot;
type

{ Tservidor }

Tservidor = class
private
fInterface:string;
fIP:String;
fMAC:string;
fHost:string;
fUsuarioAlumno:String;
fIPAdministrador:String;
farcOrdenesRoot:String;
procedure creaArchivoOrdenes();
procedure LeerArchivoOrdenes();
procedure LeerSetupAula();
procedure LeerSetupIni(archivo: string);
function NumOrdLet(num: integer): String;

public
constructor create (archivoini:string='/etc/adtosaula.conf');
property UsuarioAlumno:String read fUsuarioAlumno write fUsuarioAlumno;
property IPAdministrador:String read fIPAdministrador write fIPAdministrador;
property ArcOrdenesRoot:String read farcOrdenesRoot;
property IP:String read fIP write fIP;
property MAC:String read fMAC write fMAC;
property Host:String read fHost write fHost;
procedure ApagarEquipo();
function ObtenerHost():string;
procedure Ejecutar(comando: string);
function HallarDato(Cmd: string): string;
function ObtenerNombreUsuario(): string;
procedure EnviarDatosEquipo(datosEquipo1:String);
function ObtenerMiIp():string;
function ObtenerMiMAC():string;
end;

implementation
uses globales;

Procedure Tservidor.LeerSetupAula();
const
DIR_SETUP_AULALINEX_PORTATILES:String='/var/lib/aulalinex-profesor/aulalinex-profesor.conf';
DIR_SETUP_AULALINEX_TERMINALES:String='/var/lib/aulalinex-profesor-ltsp/setup.ini';
DIR_SETUP_AULALINEX_TERMINALES_AUTO:String='/var/lib/aulalinex-profesor-ltsp/temp_setup.ini';
var
auladinamica:Boolean;
ip1,ip2:String;
Mifichero:TMemIniFile;
i:integer;
nmord:integer;
begin
g_tipo_aula:=-1;
try
if FileExists(DIR_SETUP_AULALINEX_PORTATILES) then
begin
Mifichero:= TMemIniFile.create(DIR_SETUP_AULALINEX_PORTATILES);
with Mifichero do
begin
ip1:=readstring('IPS','Mesa 01','');
ip2:=readstring('IPS','Mesa 02','');
if (ip1 <>'') and (ip2 <>'') then
begin
g_tipo_aula:=0;
auladinamica:=ReadBool('Profesor','portatil',false);
if auladinamica then
begin
nmord:=54;
SetLength(g_ips_alumnos,nmord);
for i:=200 to 253 do
begin
g_ips_alumnos[i].nombre:='';
g_ips_alumnos[i].ip:='192.168.0.'+inttostr(i);
g_ips_alumnos[i].mac:='00:00:00:00:00:00';
g_ips_alumnos[i].encendido:=false;
end;
end
else
begin
nmord:= ReadInteger('Aula','Numero Ordenadores',36);
SetLength(g_ips_alumnos,nmord);
for i:=0 to High(g_ips_alumnos) do
begin
g_ips_alumnos[i].ip:=readstring('IPS','Mesa '+ NumOrdLet(i),'');
g_ips_alumnos[i].nombre:=readstring('Mesas','Mesa '+ NumOrdLet(i),'');
g_ips_alumnos[i].mac:=readstring('MAC','Mesa '+ NumOrdLet(i),'');
g_ips_alumnos[i].encendido:=false;
end;
end;
end;
Mifichero.Free;
end;
end;
if (g_tipo_aula=-1) and (FileExists(DIR_SETUP_AULALINEX_TERMINALES) )then
begin
Mifichero:= TMemIniFile.create(DIR_SETUP_AULALINEX_TERMINALES);
with Mifichero do
begin
ip1:=readstring('IPS','Mesa 01','192.168.0.1');
ip2:=readstring('IPS','Mesa 02','192.168.0.2');
if (ip1 <>'192.168.0.1') or ((ip2 <>'192.168.0.1') and (ip2 <>'192.168.0.2')) then
begin
g_tipo_aula:=1;
nmord:= ReadInteger('Aula','Numero Ordenadores',18);
SetLength(g_ips_alumnos,nmord);
for i:=0 to High(g_ips_alumnos) do
begin
g_ips_alumnos[i].ip:=readstring('IPS','Mesa '+ NumOrdLet(i),'');
g_ips_alumnos[i].nombre:=readstring('Mesas','Mesa '+ NumOrdLet(i),'Mesa '+ NumOrdLet(i));
g_ips_alumnos[i].mac:=readstring('MAC','Mesa '+ NumOrdLet(i),'');
g_ips_alumnos[i].encendido:=false;
end
end;
Mifichero.Free;
end;

end;
if (g_tipo_aula=-1) and (FileExists(DIR_SETUP_AULALINEX_TERMINALES_AUTO) )then
begin
Mifichero:= TMemIniFile.create(DIR_SETUP_AULALINEX_TERMINALES_AUTO);
with Mifichero do
begin
ip1:=readstring('IPS','Mesa 01','');
ip2:=readstring('IPS','Mesa 02','');
if (ip1 <>'') or (ip2 <>'') then
begin
g_tipo_aula:=1;
nmord:= ReadInteger('Aula','Numero Ordenadores',18);
SetLength(g_ips_alumnos,nmord);
for i:=0 to High(g_ips_alumnos) do
begin
g_ips_alumnos[i].ip:=readstring('IPS','Mesa '+ NumOrdLet(i),'');
g_ips_alumnos[i].nombre:=readstring('Mesas','Mesa '+ NumOrdLet(i),'Mesa '+ NumOrdLet(i));
g_ips_alumnos[i].mac:=readstring('MAC','Mesa '+ NumOrdLet(i),'');
g_ips_alumnos[i].encendido:=false;
end
end;
Mifichero.Free;
end;

end;
except
g_tipo_aula:=-1;
end;
end;
Procedure Tservidor.creaArchivoOrdenes();
const
ORDENES_ROOT:array[0..10] of String=('reboot','halt', 'puppetd -t', 'pkgsync','/etc/init.d/networking restart','/etc/init.d/dhcp3-server restart','ltsp-uptate-sshkeys', 'ltsp-update-image --arch i386','rm -f /var/lib/puppet/ssl/ca/signed/*', 'dpkg -reconfigure locales','/etc/init.d/serveradtosaula start');
PROCESOS_ROOT:array[0..13] of String=('who -a -H','last','free -mt', 'dmesg','lspci','df -H', 'fdisk -l','ifconfig','ls /etc/rc*.d', 'cat /var/log/syslog','tail -f /var/log/messages','cat /var/log/auth.log','cat /var/log/wtmp','cat /var/log/btmp' );
var
MiFichero:TMeminiFile;
i:integer;
begin
try

try
if not DirectoryExists('/var/lib/adtos') then CreateDir('/var/lib/adtos');
Mifichero:=TMeminifile.Create(farcOrdenesRoot);
with MIfichero do
begin
writeBool('Comandos libres', 'Permitidos',true);
writeBool('Destinos libres','Permitidos',true);
for i:=0 to high(ORDENES_ROOT) do
writeString('Comandos','Comando '+ NumOrdLet(i),ORDENES_ROOT[i]);
for i:=0 to high(PROCESOS_ROOT) do
writeString('Procesos','Proceso '+ NumOrdLet(i),PROCESOS_ROOT[i]);
UpdateFile;
end;
Ejecutar('chmod 644 ' + farcOrdenesRoot)
except
//writeln('Error al crear carpeta');
end;
finally
Mifichero.free;
end;
end;
function Tservidor.NumOrdLet(num:integer):String;
var
letraord:string;
begin
num:=succ(num);
if num<10 then
letraord:='0'+IntToStr(num)
else
letraord:=IntToStr(num);
result:=letraord;
end;
Procedure Tservidor.LeerArchivoOrdenes();
var
MiFichero:TMeminiFile;
k,j,i:integer;
proceso,orden:String;
begin
try
j:=0;
k:=0;
Mifichero:=TMeminifile.Create(farcOrdenesRoot);
SetLength(g_ordenes_root,100);
SetLength (g_procesos_root,100);
with MIfichero do
begin
g_ComandosLibres:=ReadBool('Comandos libres', 'Permitidos',true);
g_DestinosLibres:=ReadBool('Destinos libres','Permitidos',true);
for i:=0 to High(g_ordenes_root) do
begin
orden:=ReadString ('Comandos','Comando '+ NumOrdLet(i),'');
if trim(orden)<>'' then
begin
g_ordenes_root[j]:=orden;
inc(j)
end;
end;
for i:=0 to High(g_procesos_root) do
begin
proceso:=ReadString ('Procesos','proceso '+ NumOrdLet(i),'');
if trim(proceso)<>'' then
begin
g_procesos_root[k]:=proceso;
inc(k)
end;
end;
end;
finally
SetLength(g_ordenes_root,j);
SetLength(g_procesos_root,k);
Mifichero.free;
end;
end;
procedure Tservidor.LeerSetupIni(archivo:string);
var
FicheroIni:TMemIniFile;
begin
try
FicheroIni:=TMemIniFile.create(archivo);
try
with FicheroIni do
begin
fIPAdministrador:=ReadString('Administrador','IP','192.168.0.254');
fInterface:=ReadString('Red','Interface','eth0');
end;
finally
FicheroIni.free;
end;
except
//writeln('Error al cargar datos');
end;
end;
function Tservidor.ObtenerNombreUsuario():string;
begin
result:=g_usuario;
end;
function Tservidor.ObtenerHost():string;
const
ORD_HOST:string='hostname | cut -f1 -d"."';
begin
result:=HallarDato(ORD_HOST);
end;

procedure Tservidor.Ejecutar (comando:string);
begin;
unix.fpSystem(comando);
end;

function Tservidor.HallarDato(Cmd:string):String;
var
file1: TextFile;
s,t: ansistring;
n: longint;
j:integer;
begin

try
j:=0;
n:=popen(file1, Cmd, 'r');
if n=-1 then
begin
result:='';
Exit;
end;
t:='';
while not eof(file1) do begin
Readln(File1,s);
t:=t+s+Chr(10);
inc(j);
end;
pclose(file1);
result := trim(t);
except
result:= '';
//writeln('Error al hallar dato');
end;
end;
constructor Tservidor.create(archivoini:string='/etc/adtosaula.conf');
begin
inherited create;
farcOrdenesRoot:='/var/lib/adtos/ordenesadtos1.conf';
if not fileexists(farcOrdenesRoot) then
creaArchivoOrdenes();
fInterface:='eth0';
fIPAdministrador:='172.23.68.2';
if FileExists(archivoini) then
LeerSetupIni(archivoini);
fIP:=ObtenerMiIP();
fMAC:=ObtenerMiMAC();
fHost:=ObtenerHost();
sleep(200);
LeerArchivoOrdenes();
LeerSetupAula();
end;

procedure Tservidor.apagarEquipo;
const
ORDEN_APAGAR:string='/sbin/shutdown -h now';
begin
Ejecutar(ORDEN_APAGAR);
end;


function Tservidor.ObtenerMiIp():string;
var
mip:string;
begin
try
try
mip:=trim(HallarDato('/sbin/ifconfig ' +fInterface +' | grep "inet" |head -n 1 | cut -f2 -d":" | cut -f1 -d" "'));
except
mip:=''
end
finally
result:=mip;
end;
end;

function Tservidor.ObtenerMiMAC():string;
var
mimac:string;
begin
try
try
mimac:=Hallardato('/sbin/ifconfig ' +fInterface +' | grep "Ethernet" |head -n 1 | tr -s " " | cut -f5 -d" "');
except
mimac:=''
end;
finally
result:=mimac;
end;
end;
procedure TServidor.EnviarDatosEquipo(datosEquipo1:String);
var
orden_actualizar_datos:String;
begin
orden_actualizar_datos:='actualizarDatosEquipo '+ datosEquipo1;
try
with ThrComandosRoot.create(fIPAdministrador,311,orden_actualizar_datos,38001) do Resume;
except
//writeln('Error al enviar datos')
end;
end;

end.


(6-6/13)