Embedding SSHD in 5 minutesEmbedding SSHD in 5 minutesSSHD is designed to be easily embedded in your application as an SSH server. SSH Server needs to be configured before it can be started. Essentially, there are three steps for creating the Server
Lets look at all these steps. Refer to this class for details SshServer.java Creating an instance of SshServerThis is as simple as creating a new object SshServer sshd = SshServer.setUpDefaultServer(); It will configure the server with sensible defaults for ciphers, macs, key exchange algorithm, etc... Configuring the ServerThere are a few things that needs to be configured on the server before being able to actually use it: Portsshd.setPort(22); KeyPairProvidersshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser")); It's usually a good idea to give the host key generator a path, so that if you restart the sshd server, the same key will be used to authenticate the server. ShellFactoryThat's the part you will usually have to write to customize the SSHD server. The shell factory will be used to create a new shell each time a user logs in. SSHD provides a single implementation that you can use if you want. This implementation will create a process and delegate everything to it, so it's mostly useful to launch the OS native shell. sshd.setShellFactory(new ProcessShellFactory(new String[] { "/bin/sh", "-i", "-l" }); Note that the ShellFactory is not required. If none is configured, any request for a shell will be denied to users. CommandFactoryThe CommandFactory can be used in addition to the ShellFactory (it can also be used instead of the ShellFactory). The CommandFactory is used when direct commands are sent to the SSH server, as this is the case when running ssh localhost shutdown or scp xxx. SSHD provides a CommandFactory to support SCP that can be configure in the following way:
sshd.setCommandFactory(new ScpCommandFactory());
You can also use the ScpCommandFactory on top of your own CommandFactory:
sshd.setCommandFactory(new ScpCommandFactory(myCommandFactory));
Note that the CommandFactory is optional. If none is configured, any direct command sent by users will be rejected. Start the ServerOnce we have configured the Server, we need to call start(), to start the Server sshd.start(); |