gpt4 book ai didi

Java psexec 交互式远程命令行

转载 作者:行者123 更新时间:2023-12-02 02:43:31 26 4
gpt4 key购买 nike

我在使用 psexec 时遇到问题,它不具有交互性。一旦运行打开命令提示符的命令,它就会返回

这是我的连接类:

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,它成功地为我提供了一个交互式 session ,希望这对将来的人有帮助

关于Java psexec 交互式远程命令行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45038994/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com