【问题标题】:How to write file into Apache SSHD server?如何将文件写入 Apache SSHD 服务器?
【发布时间】:2021-02-02 00:04:14
【问题描述】:

解释:

我正在为我的同事构建一个测试工具。我有几个“模拟”内存后端,他们需要使用它们来运行集成测试。

因此,我需要运行能够上传/下载文件的 SSH 服务器。

基本用例如下:

  • 上传文件,通过Apache Camel路由下载文件
  • 上传文件,通过 Apache Camel 路由将文件传输到不同的文件夹,下载文件以验证转换、内容

问题:

我已经编写了 Apache SSHD 服务器:

服务器实现示例SshServer.java

package com.example.sshd;

import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.scp.ScpCommandFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.file.Files;

public class SSHServer {

    private SshServer sshServer;
    private static final Logger logger = LoggerFactory.getLogger(SSHServer.class);

    public SSHServer() throws IOException {
        sshServer = SshServer.setUpDefaultServer();
        sshServer.setHost("127.0.0.1");
        sshServer.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(Files.createTempFile("host_file", ".ser").toAbsolutePath()));
        sshServer.setCommandFactory(new ScpCommandFactory());
        sshServer.setPasswordAuthenticator((username, password, serverSession) -> {
            logger.debug("authenticating: {} password: {}", username, password);
            return username != null && "changeit".equals(password);
        });
    }

    public void startServer() throws IOException{
        sshServer.start();
    }

    public void stopServer() throws IOException {
        sshServer.stop();
    }
}

我试过了:

  • 查找 JavaDoc
  • 在 StackOverflow 上查找类似问题/解决方案
  • 阅读 GitHub 上的 Apache SSHD 文档

到目前为止,我找不到答案。我找到了这个post,看起来我也需要有 SSH 客户端。我在正确的轨道上吗?

  • 是否需要 SSH 客户端才能将文件写入 SSH 服务器?
  • 如何在运行 Apache SSHD 服务器时写入文件?

【问题讨论】:

  • 我会先实现文件读取能力,然后是写入。您不需要编写 SFTP 客户端来测试您的服务器,您可以使用现有的客户端,例如WinSCP。

标签: java apache-sshd


【解决方案1】:

显然是的。

由于我没有 SSH 客户端,所以我必须同时创建客户端和服务器。

Kotlin 文件上传示例:

fun upload(file: File) {
    val creator = ScpClientCreator.instance()
    val scpClient = creator.createScpClient(session())
    scpClient.upload(Paths.get(file.absolutePath), file.name)
}

SSH 服务器创建:

fun setupServer(host: String, port: Int, rootDir: File, userName: String, pwd: String): SshServer {
    val sshd = SshServer.setUpDefaultServer()
    sshd.keyPairProvider = SimpleGeneratorHostKeyProvider(File(rootDir, "sshd_key.ser").toPath())
    sshd.fileSystemFactory = VirtualFileSystemFactory(Paths.get(rootDir.absolutePath))
    sshd.port = port
    sshd.host = host
    sshd.passwordAuthenticator = PasswordAuthenticator { username: String, password: String, _: ServerSession? -> username == userName && password == pwd }
    sshd.commandFactory = ScpCommandFactory()
    val factoryList: MutableList<SubsystemFactory> = ArrayList()
    factoryList.add(SftpSubsystemFactory())
    sshd.subsystemFactories = factoryList
    return sshd
}

SSH 客户端创建:

fun setupClient(): SshClient {
    return SshClient.setUpDefaultClient()
}

// Session for scp client
fun session(): ClientSession {
    val session = client.connect(userName, server.host, server.port).verify().session
    session.addPasswordIdentity(password)
    session.auth().verify().isSuccess
    return session
}

【讨论】:

    猜你喜欢
    • 2015-09-02
    • 2015-10-01
    • 2013-05-20
    • 1970-01-01
    • 2019-05-05
    • 2015-06-22
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    相关资源
    最近更新 更多