0

解释:

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

因此,我需要运行能够上传/下载文件的 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 文档

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

  • 我需要 SSH 客户端将文件写入 SSH 服务器吗?
  • 如何在运行 Apache SSHD 服务器时写入文件?
4

1 回答 1

0

显然是的。

由于我没有 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
}
于 2021-02-01T16:04:14.900 回答