我想使用我的 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/