3

転送ファイル用にsshサーバーをモックしたい。それを行うには、Apache Mina SSHD を使用します。システムでの転送には JSch を使用します。秘密鍵付き。

JSch クライアント コード。

    JSch jSch = new JSch();
    jSch.addIdentity(privateKeyPath, passPhrase);
    Session session = jSch.getSession(usernamePlans, remoteHost);
    session.setHost(remoteHostPort);
    config.setProperty("StrictHostKeyChecking", "no");
    session.setConfig(config);
    session.setUserInfo(new StorageUserInfo(passPhrase));
    session.connect();

Apache ミナ SSHD コード

    sshd = SshServer.setUpDefaultServer();
    sshd.setPort(22999);

    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(privateKeyPath));
    sshd.setPasswordAuthenticator(new PasswordAuthenticator() {

        public boolean authenticate(String username, String password, ServerSession session) {
            // TODO Auto-generated method stub
            return true;
        }
    });

    CommandFactory myCommandFactory = new CommandFactory() {

        public Command createCommand(String command) {
            System.out.println("Command: " + command);
            return null;
        }
    };
    sshd.setCommandFactory(new ScpCommandFactory(myCommandFactory));

    List<NamedFactory<Command>> namedFactoryList = new ArrayList<>();

    namedFactoryList.add(new SftpSubsystem.Factory());
    sshd.setSubsystemFactories(namedFactoryList);

    sshd.start();

ファイル転送のためにJSchとApache Mina SSHDを接続するのを手伝ってくれる人はいますか?

スタックトレース

com.jcraft.jsch.JSchException: java.net.SocketException: Invalid argument or cannot assign requested address

Caused by: java.net.SocketException: Invalid argument or cannot assign requested address
4

1 に答える 1

1

ポートを設定するために setHost() を呼び出さないでください。文書に明確に記載されているとおりです。

 setHost
 public void setHost(String host)

Sets the host to connect to. This is normally called by JSch.getSession(java.lang.String, java.lang.String), so there is no need to call it, if you don't want to change this host. This should be called before connect().

setPort
public void setPort(int port)
Sets the port on the server to connect to. This is normally called by JSch.getSession(java.lang.String, java.lang.String), so there is no need to call it, if you don't want to change the port. This should be called before connect().

代わりに、getSession() を呼び出すときにポートを指定する必要があります。以下に示すようなもの

int port=22999;
Session session = jsch.getSession(user, host, port);
于 2016-12-05T10:53:13.500 に答える