java - Jsch ChannelExec : How to pass a password to server after the command?

标签 java ssh jsch

我想使用我的 java 应用程序 Jsch 和 ChannelExec 中的“scp”命令。命令没问题,但如何传递密码?

直接在服务器上,它会像这样:

$ scp user@server:/myPath/* .
$ user@server's password:
(informations on files copied)

命令和密码位于 2 个单独的行中。

为了在java中重新创建它,我这样做了:

ChannelExec channelExec = (ChannelExec) session.openChannel("exec"); //$NON-NLS-1$
channelExec.setCommand(scpCommand);
channelExec.connect();
try (OutputStream outputStream = channelExec.getOutputStream()) {
    pause(5000);
    outputStream.write((password + "\n").getBytes()); //$NON-NLS-1$
    outputStream.flush();
}

(read result and terminate session)

但正如你猜到的那样,它不起作用。

您有解决此问题的线索吗? 我应该在命令中传递密码,并用特殊字符分隔吗? ('\n'也许我不知道) 或者还有其他解决方案吗?

感谢您的宝贵时间!

最佳答案

连接之前,请使用以下代码行。

channelExec.setPty(true);

有关更多详细信息,请参阅下面的文档。 https://epaul.github.io/jsch-documentation/simple.javadoc/com/jcraft/jsch/ChannelExec.html#setPtyType-java.lang.String-

关于java - Jsch ChannelExec : How to pass a password to server after the command?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56497589/

相关文章:

c# - 多平台开发的最佳持续集成设置

java - 有关数据库池和 connection.setReadOnly() 方法的行为

linux - 当 ssh 连接到服务器时,如何禁用问候消息?

git - 使用 bitbucket 部署 Capistrano - 权限被拒绝(公钥)

java - 流程执行Java JSsh

java - 来自本地安装的 jar 中的 Jersey 中的 NoClassDefFoundError

java - 如何检查使用@SessionAttributes创建的 session 是否存在(Spring MVC)

ssh - 如何从 IBM Bluemix 下载日志文件?

java - 通过Java中的SSH隧道连接到Mongo数据库

java - JSch 导致数据包大小超过最大允许错误