Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中远程执行批处理文件?_Java_Sockets_Batch File_Ftp_Rmi - Fatal编程技术网

如何在java中远程执行批处理文件?

如何在java中远程执行批处理文件?,java,sockets,batch-file,ftp,rmi,Java,Sockets,Batch File,Ftp,Rmi,我必须从本地计算机执行远程计算机上存在的批处理文件 我正在通过ftp连接到远程机器,并尝试远程执行批处理文件。但无法做到这一点,也没有得到任何异常。下面是我的代码 public static void main(String[] args) throws SocketException, IOException, InterruptedException { static String server = "192.168.2.133"; static int port = 21;

我必须从本地计算机执行远程计算机上存在的批处理文件

我正在通过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");

    }

}
您可以从中获取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_文件夹。或者用一些花哨的东西,比如

If Exist "%SystemDrive%\temp_folder" (RD/S/Q "%SystemDrive%\temp_folder")

无法使用FTP远程执行文件。请看使用Microsoft的PSEXEC utility.ftp仅用于文件传输。最流行的远程执行工具是PSEXEC,尽管它在从调用时会出现问题。您可以查看SCHTASKS或WMIC帮助,了解如何在没有第三方工具的情况下远程执行脚本。你甚至把它包括在标签里。那你为什么要尝试使用FTP呢?