簡體   English   中英

Java psexec交互式遠程命令行

[英]Java psexec interactive remote command line

我在psexec上遇到問題,它不是交互式的。 運行命令打開命令提示符后,它將立即返回

這是我的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();
    }
}

然后我用下面的代碼稱呼它

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

    }
}

輸出顯示它已連接到主機,但是在編寫ping localhost命令之前只是斷開連接

這是輸出

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

其次是ping統計信息

如何保持命令提示輸出流的焦點,以便當我通過管道發送更多命令時,它們在遠程計算機而不是本地計算機上執行?

我使用了paexec而不是psexec,它設法為我提供了一個交互式會話,希望這對以后的人有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM