【问题标题】:How to Execute Batch File Remotely in java?如何在java中远程执行批处理文件?
【发布时间】:2017-02-15 04:12:58
【问题描述】:

我必须从本地机器执行远程机器上的批处理文件。

我正在通过 ftp 连接到远程机器并尝试远程执行批处理文件。但不能这样做也没有任何异常。下面是我的代码

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");

    }

}

【问题讨论】:

  • 您无法使用 FTP 远程执行文件。看看使用 Microsoft 的 PSEXEC 实用程序。
  • ftp 仅用于文件传输。最流行的远程执行工具是 PSexec,尽管从 java 调用时会出现问题。您可以查看 SCHTASKS 或 WMIC 帮助,了解如何在没有第三方工具的情况下远程执行脚本。
  • 您的问题的一个答案是 RMI。您甚至将其包含在标签中。那你为什么要尝试使用 FTP?

标签: java sockets batch-file ftp rmi


【解决方案1】:

您可以从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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    相关资源
    最近更新 更多