gpt4 book ai didi

java - JSCH 在 Windows 机器上执行命令并获取输出

转载 作者:行者123 更新时间:2023-12-01 04:31:23 25 4
gpt4 key购买 nike

在这里,我使用以下代码从本地 Windows 计算机在远程 Windows 服务器上执行命令。但我收到错误为

"Unable to execute command or shell on remote system: Failed to Execute process."

有人可以帮我解决这个问题吗?

 String user = username;
String pass = password;
String host = ip;
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);

session.setPassword(pass);
session.connect();



Channel channel = session.openChannel("exec");
channel.connect();
((ChannelExec)channel).setCommand("cmd.exe /c \"echo %cd%\"");
InputStream outputstream_from_the_channel = channel.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(outputstream_from_the_channel));
String jarOutput;
System.out.println("1");

while ((jarOutput = reader.readLine()) != null) {

System.out.println("Inside while loop");
System.out.println(jarOutput + "\n");

}

System.out.println("2");
reader.close();

最佳答案

您需要在主机(String host = ip)Windows 上安装 cygwin 才能使用 jsch。关注此网站:https://dbaportal.eu/2015/03/05/installing-openssh-cygwin-1-7-35-on-windows-2012-r2/

关于java - JSCH 在 Windows 机器上执行命令并获取输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17945960/

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