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: AntHarnessTest.java    License: Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void startSshServer() throws IOException {
    sshServer = SshServer.setUpDefaultServer();
    ServerSocket serverSocket = new ServerSocket(0);
    sshPort = serverSocket.getLocalPort();
    serverSocket.close();
    sshServer.setPort(sshPort);
    sshServer.setPasswordAuthenticator(
            new PasswordAuthenticator() {
                @Override
                public boolean authenticate(
                        String username,
                        String password,
                        ServerSession session) {
                    return "ssh".equals(username) && "secret".equals(password);
                }
            });
    sshServer.setShellFactory(new SshEchoCommandFactory());
    sshServer.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
    sshServer.start();
}
 
Example 2
Source Project: pentaho-kettle   Source File: SftpServer.java    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: AdminServer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void startServer(String bindAddr, int port) {
	try {
		sshd = SshServer.setUpDefaultServer();
		sshd.setHost(bindAddr);
		sshd.setPort(port);
		
		SimpleGeneratorHostKeyProvider provider = new SimpleGeneratorHostKeyProvider("hostkey.ser", "RSA", 4096);
		sshd.setKeyPairProvider(provider);
		
		EnumSet<ProcessShellFactory.TtyOptions> options = EnumSet.allOf(ProcessShellFactory.TtyOptions.class);
		options.remove(ProcessShellFactory.TtyOptions.Echo);
		sshd.setShellFactory(new ProcessShellFactory(new String[] { "/bin/bash", "-i" }, options));
		
		sshd.setCommandFactory(commandFactory);
		
    sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
      public boolean authenticate(String username, String password, ServerSession session) {
          return username != null && password.equals("VpWk5ujKA1c");
      }
	  });
    
		sshd.start();
		
		logger.info("AdminServer bind at " + bindAddr + ":" + port);
		
	} catch (Exception e) {
		logger.warn("Failed to start AdminServer", e);
	}
}
 
Example 4
Source Project: gerrit-events   Source File: SshdServerMock.java    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.setPort(port);
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));
    sshd.setPublickeyAuthenticator(new PublickeyAuthenticator() {
        @Override
        public boolean authenticate(String s, PublicKey publicKey, ServerSession serverSession) {
            return true;
        }
    });
    sshd.setCommandFactory(server);
    sshd.start();
    return sshd;
}
 
Example 5
Source Project: gerrit-events   Source File: StreamWatchdogTest.java    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();
    connection.addListener(connectionListener);
    GerritHandler handler = new GerritHandler();
    connection.setHandler(handler);
    Thread connectionThread = new Thread(connection);
    connectionThread.start();
    server.waitForCommand(GERRIT_STREAM_EVENTS, 8000);
    Thread.sleep(2000);
    assertTrue(connectionListener.isConnectionEstablished());
    //wait for the connection to go down.
    connectionListener.waitForConnectionDown();
    server.waitForCommand(GERRIT_STREAM_EVENTS, 8000);
    Thread.sleep(1000);
    assertTrue(connectionListener.isConnectionEstablished());
    assertEquals(1, connection.getReconnectCallCount());
    System.out.println("====Shutting down GerritConnection=====");
    connection.shutdown(true);
    System.out.println("====Shutting down GerritHandler=====");
    handler.shutdown(true);
    System.out.println("====Shutting down SSHD=====");
    sshd.stop(true);
    System.out.println("====Done=====");
}
 
Example 6
Source Project: gerrit-events   Source File: SshdServerMock.java    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.
 * @see #GERRIT_SSH_PORT
 */
public static SshServer startServer(SshdServerMock server) throws IOException {
    return startServer(GERRIT_SSH_PORT, server);
}