apache-mina - 下面的代码是否正确连接到远程 Linux 主机并使用 Apache Mina 完成一些任务?
问题描述
我想从 Jsch 切换到 Apache Mina 来查询远程 Linux 主机并完成一些任务。我需要实现远程主机的列表文件、更改目录、获取文件内容、将文件放入远程主机等,
我能够使用 session.executeRemoteCommand() 成功连接并执行一些 shell 命令。
public byte[] getRemoteFileContent(String argDirectory, String fileName)
throws SftpException, IOException {
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
StringBuilder cmdBuilder = new StringBuilder("cat" + SPACE + remoteHomeDirectory);
cmdBuilder.append(argDirectory);
cmdBuilder.append(fileName);
_session.executeRemoteCommand(cmdBuilder.toString(), stdout, null, null);
return stdout.toByteArray();
}
public void connect()
throws IOException {
_client = SshClient.setUpDefaultClient();
_client.start();
ConnectFuture connectFuture = _client.connect(_username, _host, portNumber);
connectFuture.await();
_session = connectFuture.getSession();
shellChannel = _session.createShellChannel();
_session.addPasswordIdentity(_password);
// TODO : fix timeout
_session.auth().verify(Integer.MAX_VALUE);
_channel.waitFor(ccEvents, 200);
}
我有以下问题,
- 如何在 API 级别(不是 Shell 命令级别)轻松地将 ZIP 文件发送到远程主机?以及 API 级别的所有其他操作。
- 我可以通过证书保护本地主机和远程之间的连接吗?
- 到目前为止,我正在使用 SSHD-CORE 和 SSHD-COMMON 2.2.0 版。这些库是否足够,还是我需要包含任何其他库?
- executeRemoteCommand() 是无状态的,我怎样才能保持状态?
解决方案
我需要 sshd-sftp 及其 API 来完成文件传输工作。下面的代码得到了正确的 API, sftpClient = SftpClientFactory.instance().createSftpClient(clientSession); 在 sftpClinet 上,我调用了 read() 和 write() 方法来完成任务。这完全回答了我的问题。
推荐阅读
- java - 1 小时后将 Firebase 布尔值更改为 false
- android - ParseError at [row,col]:[18,9] 消息:预期的开始或结束标记我在运行时收到此错误
- r - 在两列或三列中查找包含相同值的行
- flutter - 无法使用 VSCode 在颤振项目的 dart 文件中设置断点
- machine-learning - 用于直方图匹配的简单 Pytorch 模型不更新其参数
- android - 为什么 Firebase 不让我退出?
- python - 如何在 PySimpleGUI 中显示 discord.py 机器人的控制台
- excel - 比较第三列中的两行的条件公式
- firebase - 如何访问所有文档,我的所有文档在 Firestore 中只有子集合
- mysql - MySQL 获得最大的 varchar,如“500x400”