Java Code Examples for org.apache.sshd.SshServer

The following examples show how to use org.apache.sshd.SshServer. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: ExpectIt   Source File:    License: Apache License 2.0 6 votes vote down vote up
public static void startSshServer() throws IOException {
    sshServer = SshServer.setUpDefaultServer();
    ServerSocket serverSocket = new ServerSocket(0);
    sshPort = serverSocket.getLocalPort();
            new PasswordAuthenticator() {
                public boolean authenticate(
                        String username,
                        String password,
                        ServerSession session) {
                    return "ssh".equals(username) && "secret".equals(password);
    sshServer.setShellFactory(new SshEchoCommandFactory());
    sshServer.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
Example 2
Source Project: pentaho-kettle   Source File:    License: Apache License 2.0 6 votes vote down vote up
private SshServer createSshServer( int port, String homeDir, String hostKeyPath ) {
  SshServer server = SshServer.setUpDefaultServer();
  server.setHost( "localhost" );
  server.setPort( port );
  server.setFileSystemFactory( new VirtualFileSystemFactory( homeDir ) );
  server.setSubsystemFactories( Collections.<NamedFactory<Command>>singletonList( new SftpSubsystem.Factory() ) );
  server.setCommandFactory( new ScpCommandFactory() );
  server.setKeyPairProvider( new SimpleGeneratorHostKeyProvider( hostKeyPath ) );
  server.setPasswordAuthenticator( this );
  return server;
Example 3
Source Project: gameserver   Source File:    License: Apache License 2.0 5 votes vote down vote up
public void startServer(String bindAddr, int port) {
	try {
		sshd = SshServer.setUpDefaultServer();
		SimpleGeneratorHostKeyProvider provider = new SimpleGeneratorHostKeyProvider("hostkey.ser", "RSA", 4096);
		EnumSet<ProcessShellFactory.TtyOptions> options = EnumSet.allOf(ProcessShellFactory.TtyOptions.class);
		sshd.setShellFactory(new ProcessShellFactory(new String[] { "/bin/bash", "-i" }, options));
    sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
      public boolean authenticate(String username, String password, ServerSession session) {
          return username != null && password.equals("VpWk5ujKA1c");
		sshd.start();"AdminServer bind at " + bindAddr + ":" + port);
	} catch (Exception e) {
		logger.warn("Failed to start AdminServer", e);
Example 4
Source Project: gerrit-events   Source File:    License: MIT License 5 votes vote down vote up
 * Starts a ssh server on the provided port.
 * @param port the port to listen to.
 * @param server the server mock to start
 * @return the server.
 * @throws IOException if so.
public static SshServer startServer(int port, SshdServerMock server) throws IOException {
    SshServer sshd = SshServer.setUpDefaultServer();
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));
    sshd.setPublickeyAuthenticator(new PublickeyAuthenticator() {
        public boolean authenticate(String s, PublicKey publicKey, ServerSession serverSession) {
            return true;
    return sshd;
Example 5
Source Project: gerrit-events   Source File:    License: MIT License 5 votes vote down vote up
 * Tests that the {@link StreamWatchdog} actually performs a restart of the connection.
 * @throws IOException if so.
 * @throws InterruptedException if so.
 * @throws NoSuchMethodException if so.
 * @throws JSchException if so.
@Test(timeout = 2 * 60 * 60 * 1000)
public void testFullTimeoutFlow() throws IOException, InterruptedException, NoSuchMethodException, JSchException {
    System.out.println("====This will be a long running test ca. 2 minutes=====");
    SshdServerMock.KeyPairFiles sshKey = SshdServerMock.generateKeyPair();
    SshdServerMock server = new SshdServerMock();
    SshServer sshd = SshdServerMock.startServer(sshPort, server);
    server.returnCommandFor("gerrit version", SshdServerMock.EofCommandMock.class);
    server.returnCommandFor("gerrit ls-projects", SshdServerMock.EofCommandMock.class);
    server.returnCommandFor(GERRIT_STREAM_EVENTS, WaitLongTimeCommand.class, true,
            new Object[]{MINUTES.toMillis(5)}, new Class<?>[]{Long.class});
    server.returnCommandFor(GERRIT_STREAM_EVENTS, SshdServerMock.CommandMock.class);
    GerritConnection connection = new GerritConnection("", "localhost", sshPort, "", "",
            new Authentication(sshKey.getPrivateKey(), "jenkins"), 20,
            new WatchTimeExceptionData(new int[0], Collections.<WatchTimeExceptionData.TimeSpan>emptyList()));
    Listen connectionListener = new Listen();
    GerritHandler handler = new GerritHandler();
    Thread connectionThread = new Thread(connection);
    server.waitForCommand(GERRIT_STREAM_EVENTS, 8000);
    //wait for the connection to go down.
    server.waitForCommand(GERRIT_STREAM_EVENTS, 8000);
    assertEquals(1, connection.getReconnectCallCount());
    System.out.println("====Shutting down GerritConnection=====");
    System.out.println("====Shutting down GerritHandler=====");
    System.out.println("====Shutting down SSHD=====");
Example 6
Source Project: gerrit-events   Source File:    License: MIT License 2 votes vote down vote up
 * Starts a ssh server on the standard Gerrit port.
 * @param server the server mock to start
 * @return the server.
 * @throws IOException if so.
public static SshServer startServer(SshdServerMock server) throws IOException {
    return startServer(GERRIT_SSH_PORT, server);