【问题标题】:JSCH execute command on windows machine and take the outputJSCH 在 Windows 机器上执行命令并获取输出
【发布时间】:2013-07-30 12:53:24
【问题描述】:

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

“无法在远程系统上执行命令或shell:无法执行 执行进程。”

谁能帮我解决这个问题?

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

【问题讨论】:

  • 您只需将connect() 方法移到setCommand() 之后

标签: java ssh jsch


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2016-10-21
    • 2023-03-19
    • 1970-01-01
    • 2019-11-29
    • 2014-06-29
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多