java - Jsch ChannelExec:如何在命令后将密码传递给服务器?
问题描述
我想使用我的 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-
推荐阅读
- python - 基于客户端的会话和服务器端会话有什么区别?
- javascript - Javascript:.map 数组函数中的返回语句给出未定义
- windows - 从 Windows cmd 向 telnet 会话日志的每一行添加时间戳
- google-apps-script - 根据另一个工作表中的条件复制并粘贴行
- python - For循环内的while循环不适用于字符串匹配
- solr - 如何在 Windows 中添加 solr 内核?
- python - 如何使用 crontab 自动启动在 virtualenv 中运行的 python 脚本?
- tensorflow - 未能分配 X 字节统一内存;结果:CUDA_ERROR_OUT_OF_MEMORY:内存不足
- javascript - 在 React 中缩短导入
- sql - 从两个实体表Postgresql中随机填充关系表