JSCH在Windows机器上执行命令并获取输出 [英] JSCH execute command on windows machine and take the output
本文介绍了JSCH在Windows机器上执行命令并获取输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在这里,我正在使用以下代码从本地Windows机器在远程Windows服务器上执行命令.但是我得到了错误
Here i am executing command on remote windows server from my local windows machine with below code . But i am getting error as
无法在远程系统上执行命令或外壳程序:失败 执行过程."
"Unable to execute command or shell on remote system: Failed to Execute process."
有人可以帮助我解决这个问题吗?
can anybody help me here to come out of this problem?
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();
推荐答案
您需要在主机(字符串 host = ip)窗口上安装cygwin才能使用jsch. 请访问此网站: https://dbaportal.eu/2015/03/05/installing-openssh-cygwin-1-7-35-on-windows-2012-r2/
You need to install cygwin on the host (String host = ip) windows to use jsch. Follow this site: https://dbaportal.eu/2015/03/05/installing-openssh-cygwin-1-7-35-on-windows-2012-r2/
这篇关于JSCH在Windows机器上执行命令并获取输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文