desarrollosconsejeria/adtos-aula/1.7-0/serveradtosaula/clejucutaasi.pas @ 23a384b9
23a384b9 | Elisa | {
|
|
Adtos 1.6 Software de gestión de Aulas Tecnológicas
|
|||
Copyright (C) 2008-2009, Consejería de Educación,Junta de Extremadura
|
|||
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 clejucutaasi ;
|
|||
{$mode objfpc}{$H+}
|
|||
interface
|
|||
uses
|
|||
Classes, IdComponent, process, HTTPDefs, IdTCPClient,SysUtils, Math;
|
|||
type
|
|||
{ ThrEjucutarAsi }
|
|||
ThrEjucutarAsi = class(TThread)
|
|||
private
|
|||
{ Private declarations }
|
|||
fhost:string;
|
|||
fcodigo:integer;
|
|||
fcomando:string;
|
|||
fcliente:TIdTcpClient;
|
|||
fpuerto:integer;
|
|||
fProceso:String;
|
|||
ftipo:integer;
|
|||
function CopiarCarpeta(Cmd: string): ansistring;
|
|||
function InstalaPaqueteDebian(Cmd:string):ansistring;
|
|||
function HallarDatos(Cmd:string):ansistring;
|
|||
function EjecutarSript(Cmd:string):ansistring;
|
|||
function EjecutaComando(Cmd:string):ansistring;
|
|||
protected
|
|||
procedure Execute; override;
|
|||
public
|
|||
constructor Create(host:string;codigo:integer;comando:string;puerto:integer;proceso:String;tipo:integer=0);
|
|||
end;
|
|||
implementation
|
|||
constructor ThrEjucutarAsi.Create(host:string;codigo:integer;comando:string;puerto:integer;proceso:String;tipo:integer=0);
|
|||
begin
|
|||
inherited Create(true);
|
|||
fcodigo:=codigo;
|
|||
fcomando:=comando;
|
|||
fpuerto:=puerto;
|
|||
FreeOnTerminate:=True;
|
|||
fhost:=host;
|
|||
ftipo:=tipo;
|
|||
fProceso:=proceso;
|
|||
end;
|
|||
procedure ThrEjucutarAsi.Execute;
|
|||
var
|
|||
salida,salidaconsola:ansistring;
|
|||
respuesta_enviada:Boolean;
|
|||
j:integer;
|
|||
begin
|
|||
try
|
|||
salida:=DateTimeToStr(now) + ': '+fProceso;
|
|||
case ftipo of
|
|||
0: salidaconsola:=HallarDatos(fProceso);
|
|||
1: salidaconsola:=EjecutaComando(fProceso);
|
|||
2: salidaconsola:=EjecutarSript(fProceso);
|
|||
3: salidaconsola:=InstalaPaqueteDebian(fProceso);
|
|||
4: salidaconsola:=CopiarCarpeta(fProceso);
|
|||
end;
|
|||
except
|
|||
salidaconsola:='Error al ejecutar proceso. Comando o parámetros no validos';
|
|||
salida:=DateTimeToStr(Now) + ': '+fProceso +' '+ salidaconsola;
|
|||
end;
|
|||
salidaconsola:=HTTPEncode(salidaconsola);
|
|||
//writeln(salidaconsola);
|
|||
salida:=DateTimeToStr(Now) + ': '+fProceso;
|
|||
respuesta_enviada:=false;
|
|||
j:=0;
|
|||
try
|
|||
fcliente:=TidTcpClient.create(nil);
|
|||
fcliente.Host:=fhost;
|
|||
fcliente.port:=fpuerto;
|
|||
while (( not respuesta_enviada) and (j<6)) do
|
|||
begin
|
|||
sleep(j *j* 1000);
|
|||
inc(j);
|
|||
try
|
|||
try
|
|||
fcliente.ConnectTimeout:=1000 +1000*j;
|
|||
fcliente.connect;
|
|||
fcliente.GetResponse(200);
|
|||
fcliente.sendCmd(fcomando,fcodigo);
|
|||
fcliente.IOHandler.WriteBufferOpen;
|
|||
fcliente.IOHandler.Write(Integer(Length(salidaconsola)));
|
|||
fcliente.IOHandler.Write(salidaconsola);
|
|||
fcliente.IOHandler.WriteBufferClose;
|
|||
respuesta_enviada:=true;
|
|||
salida:='echo "'+salida+'" >> /var/log/adtos.log';
|
|||
HallarDatos(salida);
|
|||
except
|
|||
if fcliente.Connected then fcliente.Disconnect;
|
|||
end;
|
|||
finally
|
|||
if fcliente.Connected then fcliente.Disconnect;
|
|||
end;
|
|||
end;
|
|||
finally
|
|||
fcliente.free;
|
|||
Terminate;
|
|||
end;
|
|||
end;
|
|||
function ThrEjucutarAsi.InstalaPaqueteDebian(Cmd:string):ansistring;
|
|||
var
|
|||
S: TStringList;
|
|||
P: TProcess;
|
|||
ssalida:ansistring;
|
|||
mproceso:string;
|
|||
begin
|
|||
mproceso:='sh -c '+QuotedStr(Cmd + ' 2>&1');
|
|||
S := TStringList.Create;
|
|||
P := TProcess.Create(nil);
|
|||
try
|
|||
try
|
|||
P.CommandLine := mproceso;
|
|||
P.Options := P.Options + [poWaitOnExit, poUsePipes];
|
|||
P.Execute;
|
|||
S.LoadFromStream(P.Output);
|
|||
ssalida:=S.text;
|
|||
except
|
|||
ssalida:='Error al instalar el paquete: Compruebe que es correcto e inténtelo de nuevo';
|
|||
end
|
|||
finally
|
|||
S.Free;
|
|||
P.Free;
|
|||
result:=ssalida;
|
|||
end;
|
|||
end;
|
|||
function ThrEjucutarAsi.HallarDatos(Cmd:string):ansistring;
|
|||
const
|
|||
READ_BYTES = 2048;
|
|||
var
|
|||
S: TStringList;
|
|||
M: TMemoryStream;
|
|||
P: TProcess;
|
|||
n: LongInt;
|
|||
BytesRead: LongInt;
|
|||
ssalida:ansistring;
|
|||
mproceso:string;
|
|||
begin
|
|||
mproceso:='sh -c '+QuotedStr(Cmd + ' 2>&1');
|
|||
S := TStringList.Create;
|
|||
M := TMemoryStream.Create;
|
|||
BytesRead := 0;
|
|||
P := TProcess.Create(nil);
|
|||
try
|
|||
try
|
|||
P.CommandLine := mproceso;
|
|||
P.Options := P.Options + [poWaitOnExit, poUsePipes];
|
|||
P.Execute;
|
|||
while P.Running do
|
|||
begin
|
|||
M.SetSize(BytesRead + READ_BYTES);
|
|||
n := P.Output.Read((M.Memory + BytesRead)^, READ_BYTES);
|
|||
if n > 0 then
|
|||
begin
|
|||
Inc(BytesRead, n);
|
|||
end
|
|||
else
|
|||
begin
|
|||
Sleep(200);
|
|||
end;
|
|||
end;
|
|||
repeat
|
|||
M.SetSize(BytesRead + READ_BYTES);
|
|||
n := P.Output.Read((M.Memory + BytesRead)^, READ_BYTES);
|
|||
if n > 0 then
|
|||
begin
|
|||
Inc(BytesRead, n);
|
|||
end;
|
|||
until n <= 0;
|
|||
M.SetSize(BytesRead);
|
|||
S.LoadFromStream(M);
|
|||
ssalida:=trim(S.text);
|
|||
except
|
|||
ssalida:='Error al realizar la consuta: orden o parámetros no validos';
|
|||
end;
|
|||
finally
|
|||
S.Free;
|
|||
P.Free;
|
|||
M.Free;
|
|||
result:=ssalida;
|
|||
end;
|
|||
end;
|
|||
function ThrEjucutarAsi.EjecutarSript(Cmd:string):ansistring;
|
|||
var
|
|||
ssalida:ansistring;
|
|||
mproceso:string;
|
|||
P: TProcess;
|
|||
begin
|
|||
ssalida:='';
|
|||
mproceso:='sh -c '+QuotedStr(Cmd); ;
|
|||
try
|
|||
try
|
|||
P := TProcess.Create(nil);
|
|||
P.CommandLine := mproceso;
|
|||
P.Options := P.Options + [ poUsePipes];
|
|||
P.Execute;
|
|||
ssalida:=trim('El script se ha ejecutado en este equipo');
|
|||
except
|
|||
ssalida:='Error al ejecutar el script';
|
|||
end;
|
|||
finally
|
|||
result:=ssalida;
|
|||
P.Free;
|
|||
end;
|
|||
end;
|
|||
function ThrEjucutarAsi.EjecutaComando(Cmd:string):ansistring;
|
|||
var
|
|||
ssalida:ansistring;
|
|||
mproceso:string;
|
|||
P: TProcess;
|
|||
begin
|
|||
ssalida:='';
|
|||
mproceso:='sh -c '+QuotedStr(Cmd); ;
|
|||
try
|
|||
try
|
|||
P := TProcess.Create(nil);
|
|||
P.CommandLine := mproceso;
|
|||
P.Options := P.Options + [ poUsePipes];
|
|||
P.Execute;
|
|||
ssalida:=trim('El comando se ha ejecutado en este equipo');
|
|||
except
|
|||
ssalida:='Error al ejecutar el comando: orden o parámetros no validos';
|
|||
end;
|
|||
finally
|
|||
result:=ssalida;
|
|||
P.Free;
|
|||
end;
|
|||
end;
|
|||
function ThrEjucutarAsi.CopiarCarpeta(Cmd:string):ansistring;
|
|||
var
|
|||
ssalida:ansistring;
|
|||
mproceso:string;
|
|||
P: TProcess;
|
|||
begin
|
|||
ssalida:='';
|
|||
mproceso:='sh -c '+QuotedStr(Cmd); ;
|
|||
try
|
|||
try
|
|||
P := TProcess.Create(nil);
|
|||
P.CommandLine := mproceso;
|
|||
P.Options := P.Options + [ poUsePipes];
|
|||
P.Execute;
|
|||
ssalida:=trim('La carpeta se ha compiado en el directorio selecionado');
|
|||
except
|
|||
ssalida:='Error al copiar carpeta';
|
|||
end;
|
|||
finally
|
|||
result:=ssalida;
|
|||
P.Free;
|
|||
end;
|
|||
end;
|
|||
end.
|