简体   繁体   English

Java psexec交互式远程命令行

[英]Java psexec interactive remote command line

I'm having an issue with psexec where it's not interactive. 我在psexec上遇到问题,它不是交互式的。 It returns as soon as it has run the command to open command prompt 运行命令打开命令提示符后,它将立即返回

Here is my Connection class: 这是我的Connection类:

package testProject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;


public class ConTest {

    private ProcessBuilder process;
    private Process connection;
    private String main_connection;;

    public ConTest(String host, String user, String password) {
        process = new ProcessBuilder("cmd.exe");
        process.redirectErrorStream(true);
        main_connection="<path to psexec>\psexec.exe \\\\" + host + 
                " -accepteula -nobanner -u " + user + " -p " + password +" cmd";
    }

    public void runCommand(String command) throws Exception{

        /* Variable Declaration */
        String          readline;
        PrintStream     output;
        BufferedReader  input;

        /* Variable Initialization */
        connection = process.start();
        output = new PrintStream(connection.getOutputStream());
        input = new BufferedReader(new InputStreamReader(connection.getInputStream()));

        /* Running the commands on the Host */

        output.println(main_connection);
        output.println(command);
        output.println("exit");
        output.close();

        /*print the output from the command*/
        while ((readline = input.readLine()) != null) {
            System.out.println(readline);
        }

        input.close();
        connection.waitFor();
    }
}

And then I'm calling it using the following 然后我用下面的代码称呼它

package testProject;

public class mainClass {

    public mainClass() {
    }

    public static void main(String[] args) throws Exception {

        ConTest con = new ConTest(<IP>, <Admin>, <Password>);
        con.runCommand("ping localhost");

    }
}

The output shows that it connects to the host but then it just disconnects before writing the ping localhost command 输出显示它已连接到主机,但是在编写ping localhost命令之前只是断开连接

Here is the output 这是输出

C:><path to psexec>\psexec.exe \\<IP> -accepteula -nobanner -u <Admin> -p <Password> cmd
Microsoft Windows [Version 6.1.7601]Connecting to <IP>...


Starting PSEXESVC service on <IP>...


Connecting with PsExec service on <IP>...


Starting cmd on <IP>...



cmd exited on <IP> with error code 0.

C:\>ping localhost

followed by the ping stats 其次是ping统计信息

How can I keep the command prompt the focus of the output stream so when I send more commands down the pipe they are executed on the remote machine not my local machine? 如何保持命令提示输出流的焦点,以便当我通过管道发送更多命令时,它们在远程计算机而不是本地计算机上执行?

我使用了paexec而不是psexec,它设法为我提供了一个交互式会话,希望这对以后的人有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM