JSCH execute command on windows machine and take the output

雨燕双飞 提交于 2019-12-11 05:07:25

问题


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

回答1:


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/



来源:https://stackoverflow.com/questions/17945960/jsch-execute-command-on-windows-machine-and-take-the-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!