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 public class BasicSshServer
{
17 private Path hostKeyPath
;
19 private SshServer sshd
= null;
21 public BasicSshServer(Integer port
, Path hostKeyPath
) {
23 this.hostKeyPath
= hostKeyPath
;
28 sshd
= SshServer
.setUpDefaultServer();
30 if (hostKeyPath
== null)
31 throw new IllegalStateException("An SSH server key must be set");
32 sshd
.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(hostKeyPath
));
33 // sshd.setShellFactory(new ProcessShellFactory(new String[] { "/bin/sh", "-i",
35 String
[] shellCommand
= OS
.LOCAL
.getDefaultShellCommand();
36 // FIXME transfer args
37 // sshd.setShellFactory(new ProcessShellFactory(shellCommand));
38 sshd
.setShellFactory(new ProcessShellFactory(shellCommand
[0], shellCommand
));
39 sshd
.setCommandFactory(new ScpCommandFactory());
41 sshd
.setPublickeyAuthenticator(new DefaultAuthorizedKeysAuthenticator(true));
43 } catch (Exception e
) {
44 throw new RuntimeException("Cannot start SSH server on port " + port
, e
);
48 public void destroy() {
51 } catch (IOException e
) {
52 throw new RuntimeException("Cannot stop SSH server on port " + port
, e
);
56 public Integer
getPort() {
60 public void setPort(Integer port
) {
64 public Path
getHostKeyPath() {
68 public void setHostKeyPath(Path hostKeyPath
) {
69 this.hostKeyPath
= hostKeyPath
;
72 public static void main(String
[] args
) {
74 Path hostKeyPath
= Paths
.get("hostkey.ser");
77 port
= Integer
.parseInt(args
[0]);
79 hostKeyPath
= Paths
.get(args
[1]);
80 } catch (Exception e1
) {
84 BasicSshServer sshServer
= new BasicSshServer(port
, hostKeyPath
);
86 Runtime
.getRuntime().addShutdownHook(new Thread("Shutdown SSH server") {
94 synchronized (sshServer
) {
97 } catch (InterruptedException e
) {
103 public static void printUsage() {
104 System
.out
.println("java " + BasicSshServer
.class.getName() + " [port] [server key path]");