简体   繁体   中英

Apache Mina SSHD 1.0 Server exits immediately

I am using Apache Mina sshd-core-1.0.0 to start an SFTP daemon. The program however exits after the sshd.start() . There are no errors. However if I use sshd-core-0.0.14, the server starts just fine and I can initiate an SFTP session. Am I missing something with 1.0.0?

Code snippet with 1.0.0 (does not work)

public static void main(String[] args) throws IOException {
    SshServer sshd = SshServer.setUpDefaultServer();
    sshd.setPort(2222);
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File ("hostkey.ser")))   
    sshd.setPasswordAuthenticator(new AuthenticatorImpl());
    sshd.start();
}

Code snippet with 0.0.14 (works)

public static void main(String[] args) throws IOException {
    SshServer sshd = SshServer.setUpDefaultServer();
    sshd.setPort(2222);
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser")); 
    sshd.setPasswordAuthenticator(new AuthenticatorImpl());
    sshd.start();
}

The following is output when 1.0.0 runs. The code starts fine but terminates after the sshd.start() statement.

2015-12-16 19:57:38,510 DEBUG SFTPServer.main(SFTPServer.java:26) message
2015-12-16 19:57:38,767 INFO org.apache.sshd.common.util.SecurityUtils$BouncyCastleRegistration.call(SecurityUtils.java:145) Trying to register BouncyCastle as a JCE provider
2015-12-16 19:57:39,076 INFO org.apache.sshd.common.util.SecurityUtils$BouncyCastleRegistration.call(SecurityUtils.java:149) Registration succeeded
2015-12-16 19:57:39,105 DEBUG org.apache.sshd.common.io.nio2.Nio2Acceptor.bind(Nio2Acceptor.java:57) Binding Nio2Acceptor to address 0.0.0.0/0.0.0.0:2222
2015-12-16 19:57:39,114 INFO SFTPServer.main(SFTPServer.java:32) Started

The SshServer.Start only starts listening on the incoming port. It does not block. So the main is terminated immediately afterwards. This should not be any different in 0.0.14, though I cannot try.

You have to wait in the main explicitly to keep the server running.

See how SshServer.main is implemented (both in 0.0.14 and 1.0.0):

public static void main(String[] args) throws Exception {

    ...

    SshServer sshd = SshServer.setUpDefaultServer();

    ...

    sshd.start();

    Thread.sleep(Long.MAX_VALUE);
}

I had the same problem and it was because SSHD doesn't know which network library to use. I've added the Netty package:

<dependency>
    <groupId>org.apache.sshd</groupId>
    <artifactId>sshd-netty</artifactId>
    <version>2.6.0</version>
</dependency>

And it worked without the Thread.sleep() call. In the log you will see:

[main] INFO org.apache.sshd.common.io.DefaultIoServiceFactoryFactory - Using NettyIoServiceFactoryFactory

And then Netty start responding:

[nioEventLoopGroup-2-1] INFO io.netty.handler.logging.LoggingHandler - [id: 0x7fa79050] REGISTERED
[nioEventLoopGroup-2-1] INFO io.netty.handler.logging.LoggingHandler - [id: 0x7fa79050] BIND: 0.0.0.0/0.0.0.0:2222
[nioEventLoopGroup-2-1] INFO io.netty.handler.logging.LoggingHandler - [id: 0x7fa79050, L:/[0:0:0:0:0:0:0:0]:2222] ACTIVE

I didn't added any other Netty library in Maven.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM