1 package org
.argeo
.cms
.ssh
;
3 import java
.io
.IOException
;
4 import java
.nio
.file
.Path
;
5 import java
.nio
.file
.Paths
;
7 import org
.apache
.sshd
.scp
.server
.ScpCommandFactory
;
8 import org
.apache
.sshd
.server
.SshServer
;
9 import org
.apache
.sshd
.server
.config
.keys
.DefaultAuthorizedKeysAuthenticator
;
10 import org
.apache
.sshd
.server
.keyprovider
.SimpleGeneratorHostKeyProvider
;
11 import org
.apache
.sshd
.server
.shell
.ProcessShellFactory
;
12 import org
.argeo
.util
.OS
;
14 /** A simple SSH server with some defaults. Supports SCP. */
15 @SuppressWarnings("restriction")
16 public class BasicSshServer
{
18 private Path hostKeyPath
;
20 private SshServer sshd
= null;
22 public BasicSshServer(Integer port
, Path hostKeyPath
) {
24 this.hostKeyPath
= hostKeyPath
;
29 sshd
= SshServer
.setUpDefaultServer();
31 if (hostKeyPath
== null)
32 throw new IllegalStateException("An SSH server key must be set");
33 sshd
.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(hostKeyPath
));
34 // sshd.setShellFactory(new ProcessShellFactory(new String[] { "/bin/sh", "-i",
36 String
[] shellCommand
= OS
.LOCAL
.getDefaultShellCommand();
37 // FIXME transfer args
38 // sshd.setShellFactory(new ProcessShellFactory(shellCommand));
39 sshd
.setShellFactory(new ProcessShellFactory(shellCommand
[0], shellCommand
));
40 sshd
.setCommandFactory(new ScpCommandFactory());
42 sshd
.setPublickeyAuthenticator(new DefaultAuthorizedKeysAuthenticator(true));
44 } catch (Exception e
) {
45 throw new RuntimeException("Cannot start SSH server on port " + port
, e
);
49 public void destroy() {
52 } catch (IOException e
) {
53 throw new RuntimeException("Cannot stop SSH server on port " + port
, e
);
57 public Integer
getPort() {
61 public void setPort(Integer port
) {
65 public Path
getHostKeyPath() {
69 public void setHostKeyPath(Path hostKeyPath
) {
70 this.hostKeyPath
= hostKeyPath
;
73 public static void main(String
[] args
) {
75 Path hostKeyPath
= Paths
.get("hostkey.ser");
78 port
= Integer
.parseInt(args
[0]);
80 hostKeyPath
= Paths
.get(args
[1]);
81 } catch (Exception e1
) {
85 BasicSshServer sshServer
= new BasicSshServer(port
, hostKeyPath
);
87 Runtime
.getRuntime().addShutdownHook(new Thread("Shutdown SSH server") {
95 synchronized (sshServer
) {
98 } catch (InterruptedException e
) {
104 public static void printUsage() {
105 System
.out
.println("java " + BasicSshServer
.class.getName() + " [port] [server key path]");