channelExec.setCommand() 在执行 shell 命令时不起作用

channelExec.setCommand() Not Working when executing shell commands

我正在远程服务器中执行 .sh 文件,但它无法正常工作。为此,我使用了 JSch 和 java。这就是我尝试的方式。

        ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
        InputStream in = channelExec.getInputStream();
        channelExec.setCommand("cd /home");

这是一个完整的示例 http://myitlearnings.com/java-code-to-run-a-remote-script-on-remote-host-using-ssh/

添加下面的语句,这样安全措施就通过了//默认情况下 StrictHostKeyChecking 设置为 yes 作为安全措施。

session.setConfig("StrictHostKeyChecking", "no");

添加完整的代码库(共享代码时不需要ip)和收到的错误消息。添加适当的异常处理,以便我们了解您遇到的错误是什么并可以更好地提供帮助