2016-10-06 140 views
-1

我必須從本地機器執行遠程機器上存在的批處理文件。如何在java中遠程執行批處理文件?

我通過FTP連接到遠程計算機,並嘗試執行批處理文件remotely.but不能夠做到這一點也並不getiing任何異常。下面是我的代碼

public static void main(String[] args) throws SocketException, IOException, 
     InterruptedException { 
static String server = "192.168.2.133"; 
static int port = 21; 
static String user = "kamal"; 
static String pass = "12345"; 
static FTPClient ftpClient; 

    // TODO Auto-generated method stub 
    ftpClient = new FTPClient(); 
    ftpClient.connect(server, port); 
    ftpClient.login(user, pass); 
    ftpClient.enterLocalPassiveMode(); 
    ftpClient.changeWorkingDirectory("/"); 

    System.out.println(ftpClient.isConnected()); 
    // ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 

    System.out.println("ftpClient.printWorkingDirectory() " 
      + ftpClient.printWorkingDirectory()); 

    String[] str = ftpClient.listNames(ftpClient.printWorkingDirectory()); 
    for (String string : str) { 
     System.out.println(string); 
    } 

    // ftpClient.changeWorkingDirectory(ftpClient.printWorkingDirectory().concat("demo")); 
    System.out.println("ftpClient.printWorkingDirectory() " 
      + ftpClient.printWorkingDirectory()); 
    String psCommand = "C:/Users/kamlesh.kumar/Downloads/PSTools/PsExec.exe //" 
      + server + " -u " + user + " -p " + pass; 
    psCommand = psCommand + " " + "MEF.bat"; 
    // psCommand = psCommand + " " + commandToRunOnRemoteMachine + " " + 
    // parameters; 
    String[] cmd = new String[5]; 
    cmd[0] = "cmd.exe"; 
    cmd[1] = "/C"; 
    cmd[2] = psCommand; 
    cmd[3] = ""; 
    cmd[4] = ""; 
    // Run remote command 
    // ftpClient.changeWorkingDirectory(ftpClient.printWorkingDirectory().concat("\\\\192.168.2.135\\demo\\demo")); 
    // System.out.println(ftpClient.printWorkingDirectory()); 
    File f = new File(ftpClient.printWorkingDirectory()); 
    Process p = null; 
    try { 
     p = Runtime.getRuntime().exec(cmd, null, f); 
    } catch (Exception e) { 
     System.out.println(e.toString()); 
     e.printStackTrace(); 
    } 

    int exitStatus = 0; 
    try { 
     exitStatus = p.waitFor(); 
     System.out.println("exitStatus in try " + exitStatus); 
    } catch (Exception e) { 
     System.out.println("exitStatus" + exitStatus); 
     System.out.println(e.toString()); 
     e.printStackTrace(); 
    } finally { 
     System.out.println("exitStatus in finally " + exitStatus); 
    } 
    if (exitStatus == 0) { 
     System.out.println("Done"); 

    } 

} 
+1

你不能用FTP遠程執行文件。看看使用微軟的PSEXEC實用程序。 – Squashman

+0

ftp僅用於文件傳輸。最常用的遠程執行工具是PSexec,但從[java](http://forum.sysinternals.com/faq-common-pstools-issues_topic15920.html)調用時會遇到問題。您可以檢查SCHTASKS或WMIC幫助,以瞭解如何在沒有第三方工具的情況下遠程執行腳本。 – npocmaka

+0

您的問題的一個答案是RMI。你甚至將它包含在標籤中。那你爲什麼試圖使用FTP? – EJP

回答

0

您可以從https://technet.microsoft.com/en-us/sysinternals/pxexec.aspx得到PSEXEC和我之前所做的是製作一個批處理文件,它將要在遠程PC上運行的文件複製到該PC上,然後使用PSEXEC遠程執行它,並刪除該文件。我不知道這是你想要的,但它可能就足夠了。

set remotecpu="computer you're sending it to" 

robocopy "localpcdrive\location1\scripts\script you want ran" \\%remotecpu%\c$\temp_folder 


psexec \\%remotecpu% -u "username" -p "password" -i -d "C:\temp_folder\path to\script you want ran" 

如果我沒有記錯的話,這將執行該文件,然後你只是做一個DEL命令並刪除temp_folder你的腳本下。或者用一些像花式的東西

If Exist "%SystemDrive%\temp_folder" (RD/S/Q "%SystemDrive%\temp_folder") 
相關問題