2009-12-09 65 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

相關問題