2009-12-09 123 views
1

嗨我正在一个delphi应用程序。我需要从本地机器执行delphi应用程序中的远程机器上的perl脚本。我需要自动执行这个过程,而不需要手工进行处理。现在我将清楚地解释这个过程,目前运行perl脚本,我只需打开腻子窗口,连接到远程机器并执行perl脚本。 perl脚本反过来调用存储过程并更新表。如何从delphi应用程序运行远程机器上的perl脚本?

现在我想通过点击一个按钮来自动执行上面解释的过程。所以当我点击一个按钮时,它应该调用一个连接到远程机器的函数,然后执行perl脚本。我清楚吗?请帮忙解决这个问题。我需要尽快在德尔福这个代码。

+0

只需手动自动执行。 – 2009-12-09 08:12:06

+0

哪个部位有问题?连接到远程机器,运行程序或特别运行* Perl脚本* – 2009-12-09 16:12:50

回答

2

两个步骤:

  • 使用Delphi的模块/功能,可以通过SSH
  • 运行Perl脚本。
+0

会很棒,如果您发布链接到这样一个模块 – NickSoft 2011-04-12 14:48:15

0

你可以看看Capistrano - 它是专为这种自动化而设计的。你应该只需要:

  1. download和安装Ruby的Windows
  2. download并安装RubyGems的
  3. 创建Capistrano的配方来调用Perl脚本远程
  4. 从您的Delphi应用程序调用Capistrano的
-1

我不知道Perl。但如果我理解正确,它是一种类似于PHP的网页脚本语言。我也面临类似的情况,但与PHP。所以我最终在我的Delphi应用程序中使用Indy打电话给一个php脚本。我不知道是否可以将相同类型的逻辑应用于perl。这里是一些逻辑的片段。

var 
    IdHTTP: TIdHTTP; 
    IdSSLIOHandlerSocket1: TIdSSLIOHandlerSocketOpenSSL; 
begin 
    try 
    IdSSLIOHandlerSocket1 := TIdSSLIOHandlerSocketOpenSSL.create(nil); 
    IdHTTP := TIdHTTP.create(nil); 
    idhttp.handleredirects := True; 
    with IdSSLIOHandlerSocket1 do begin 
     SSLOptions.Method := sslvSSLv3; 
     SSLOptions.Mode := sslmUnassigned; 
     SSLOptions.VerifyMode := []; 
     SSLOptions.VerifyDepth := 2; 
    end; 
    with IdHTTP do begin 
     IOHandler := IdSSLIOHandlerSocket1; 
     ProxyParams.BasicAuthentication := False; 
     Request.ContentType := 'text/html'; 
     request.connection := 'keep-alive'; 
     Request.Accept := 'text/html, */*'; 
    end; 
    result := idhttp.get('http://www.mysite.com/myscript.php'); 
    finally 
    IdHTTP.free; 
    IdSSLIOHandlerSocket1.free; 
    end; 
end; 
+0

Perl不是一种网页脚本语言:“Perl是一种高级的,通用的,解释的动态编程语言。” (http://en.wikipedia.org/wiki/Perl) - 您可以使用Perl编写Web应用程序,但不是每个Perl脚本都是一个Web应用程序:) – mjn 2009-12-09 14:19:18

+0

感谢您的所有答复。一旦我完成了这个自动化过程,我会让你知道。非常感谢。如果您有其他解决方案,请与我分享。 – JVNR 2009-12-11 08:03:03

0

如果远程机器运行Windows,PsExec可能是一个解决方案。

PSEXEC是一种重量轻 的telnet更换,让你 其他系统上的

执行过程

也有类似的工具,如WinExe其Windows主机

0

是上远程执行程序运行Windows的远程机器?如果是这样,你总是可以从Delphi中调用“psexec”。或者您可以使用WMI来远程执行进程(假设远程主机正在运行某个版本的Windows)

这是德尔福的一个完整示例,taken from here。您需要WbemScripting_TLB单元,您可以通过在Delphi 2007中使用“组件|导入组件|导入类型库”菜单选项安装类型库%windir%\ System32 \ wbem \ wbemdisp.tlb来创建该单元。

unit Unit1; 

interface 

uses 
    Windows, 
    Messages, 
    SysUtils, 
    Variants, 
    Classes, 
    Graphics, 
    Controls, 
    Forms, 
    Dialogs, 
    StdCtrls, 
    ComCtrls; 

type 
    TForm1 = class(TForm) 
    btnExecute: TButton; 
    edtProgramToExecute: TEdit; 
    Label2: TLabel; 
    Label3: TLabel; 
    Label4: TLabel; 
    Label5: TLabel; 
    edtRemoteMachine: TEdit; 
    edtUser: TEdit; 
    edtPassword: TEdit; 
    procedure btnExecuteClick(Sender: TObject); 
    private 
    { Private declarations } 
    public 
    { Public declarations } 
    end; 

var 
    Form1: TForm1; 

implementation 

{$R *.dfm} 

uses WbemScripting_TLB; 

function remoteExecute(programName: string; machine: string = ''; user: string = ''; 
    password: string = ''): string; 
var 
    SWbemLocator1: TSWbemLocator; 
    Service: ISWbemServices; 
    InParam, 
    OutParam, SObject: ISWbemObject; 
    Method: ISWbemMethod; 
    SProp1 
    , SProp2, MyProperty 
    : ISWbemProperty; 
    s, methodName: string; 
    PropValue: OleVariant; 
begin 
    methodName := 'Create'; 
    // CoInitialize(nil); 
    SWbemLocator1 := TSWbemLocator.Create(nil); 
    if machine = '' then 
    machine := '.'; 
    Service := SWbemLocator1.ConnectServer(machine, 'root\CIMV2', user, password, '', '', 
    0, nil); 
    Service.Security_.Set_ImpersonationLevel(wbemImpersonationLevelImpersonate); 
    SObject := Service.Get('Win32_Process', 0, nil); 

    Method := SOBject.Methods_.Item(methodName, 0); 
    InParam := Method.InParameters.SpawnInstance_(0); 

    MyProperty := InParam.Properties_.Add('CommandLine', wbemCimtypeString, False, 0); 
    PropValue := programName; 
    MyProperty.Set_Value(PropValue); 

    MyProperty := InParam.Properties_.Add('CurrentDirectory', wbemCimtypeString, False, 0); 
    PropValue := Null; 
    MyProperty.Set_Value(PropValue); 

    MyProperty := InParam.Properties_.Add('ProcessStartupInformation', wbemCimtypeObject, 
    False, 0); 
    PropValue := Null; 
    MyProperty.Set_Value(PropValue); 

    OutParam := SObject.ExecMethod_(methodName, InParam, 0, nil); 
    // OutParam:= SObject.ExecMethod_(methodName, nil, 0, nil); 
    SProp1 := outParam.Properties_.Item('ReturnValue', 0); 
    SProp2 := outParam.Properties_.Item('ProcessId', 0); 
    case SProp1.Get_Value of 
    0: s := 'Successful completion.'; 
    2: s := 'Access denied.'; 
    3: s := 'Insufficient privilege.'; 
    8: s := 'Unknown failure.'; 
    9: s := 'Path not found.'; 
    21: s := 'Invalid parameter.'; 
    else 
    s := 'Unknown reply code!'; 
    end; 
    SWbemLocator1.Free; 
    service := nil; 
    SObject := nil; 
    OutParam := nil; 
    SProp1 := nil; 
    result := s + '(PID=' + inttostr(SProp2.Get_Value) + ')'; 
    // CoUninitialize; 
end; 

procedure TForm1.btnExecuteClick(Sender: TObject); 
begin 
    statusbar1.simpletext := remoteExecute(edit1.text, edit2.text, edit3.text, edit4.text); 
end; 

end. 

你也可以这样做在VBScript:

Here's a VBScript snippet that demonstrates how this would work. 
' This script provides a function for executing a command on a remote computer 
' This uses WMI and requires that you have administrative righs on the remote machine 
' 

Dim strComputer, strCommandLineToRun 

'change the period to an IP Address or computer name 
strComputer = "." 'example: strComputer = "192.168.1.105" 

'this is the path to the file on the computer whose name/IP address is stored in the strComputer variable 
strCommandLineToRun = "c:\windows\system32\calc.exe" 


' This calls the function to run the process on a remote computer 
RemoteExecute strComputer,"","",strCommandLineToRun 


Function RemoteExecute(strServer, strUser, strPassword, CmdLine) 
    Const Impersonate = 3 

    RemoteExecute = -1 

    Set Locator = CreateObject("WbemScripting.SWbemLocator") 
    Set Service = Locator.ConnectServer(strServer, "root\cimv2", strUser, strPassword) 

    Service.Security_.ImpersonationLevel = Impersonate 
    Set Process = Service.Get("Win32_Process") 

    result = Process.Create(CmdLine, , , ProcessId) 

    If (result <> 0) Then 
     WScript.Echo "Creating Remote Process Failed: " & result 
     Wscript.Quit 
    End If 

    RemoteExecute = ProcessId 
End Function 
3

在自动化你已经做手工的精神,你可以使用Plink工具自带的腻子。它接受各种命令行选项,包括用户名,主机,密码和要运行的命令。您也可以在保存的Putty会话中指定大多数选项。有关更多信息,请参见the Putty documentation。您可以使用CreateProcess从您的程序运行该命令。

var 
    cmd: string; 
begin 
    cmd := 'plink -batch -ssh -pw secret [email protected] /home/user/command.pl'; 
    UniqueString(cmd); 
    CreateProcess(nil, PChar(cmd), ...); 

如果您需要运行的命令有参数,则可能需要引用整个命令。如果你有多个命令要运行,你应该把它们放在一个文件中,然后使用Plink的-m选项。

+0

感谢您的回复。我将在我的应用程序中执行此操作,并告诉您是否在此面临一些问题。 – JVNR 2009-12-11 08:01:17

相关问题