org.apache.sshd.server.shell.ProcessShellFactory Java Examples

The following examples show how to use org.apache.sshd.server.shell.ProcessShellFactory. 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 File: MessageShellFactory.java    From artifactory_ssh_proxy with Apache License 2.0 6 votes vote down vote up
public MessageShellFactory(File messageFile) throws IOException {
    super(new String[] {}, EnumSet.of(ProcessShellFactory.TtyOptions.ONlCr));

    if (!messageFile.exists()) {
        throw new FileNotFoundException("Message file '" + messageFile.getPath() + "' not found");
    }

    if (!messageFile.canRead()) {
        throw new IOException("Message file '" + messageFile.getPath() + "' cannot be read");
    }

    // otherwise read the file.
    try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(messageFile)))) {
        String line;
        StringBuilder messageBuilder = new StringBuilder(8192);
        while (null != (line = br.readLine())) {
            messageBuilder.append(line);
        }

        this.messageBytes = messageBuilder.toString().getBytes(StandardCharsets.UTF_8);
    }
}
 
Example #2
Source File: AdminServer.java    From gameserver with 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 #3
Source File: EmbeddedSSHServer.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
public EmbeddedSSHServer(Path homeDir, int port) {
    this.sshServer = SshServer.setUpDefaultServer();
    this.sshServer.setPort(port);
    this.sshServer.setCommandFactory(new EchoCommandFactory());
    this.sshServer.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
    this.sshServer.setPasswordAuthenticator((username, password, serverSession) -> username.equals("admin") && password.equals("admin"));
    this.sshServer.setPublickeyAuthenticator((s, publicKey, serverSession) -> true);
    this.homeDir = homeDir;

    if (EnvironmentUtils.isWindows()) {
        sshServer.setShellFactory(new ProcessShellFactory(new String[] { "cmd.exe " }));
    } else {
        sshServer.setShellFactory(new ProcessShellFactory(new String[] { "/bin/sh", "-i", "-s" }));
    }
}
 
Example #4
Source File: TestSSHInfrastructureV2.java    From scheduling with GNU Affero General Public License v3.0 4 votes vote down vote up
@BeforeClass
public static void startSSHServer() throws Exception {
    // Disable bouncy castle to avoid versions conflict
    System.setProperty("org.apache.sshd.registerBouncyCastle", "false");

    sshd = SshServer.setUpDefaultServer();

    SimpleGeneratorHostKeyProvider keyProvider = new SimpleGeneratorHostKeyProvider();
    keyProvider.setAlgorithm("RSA");
    sshd.setKeyPairProvider(keyProvider);

    List<NamedFactory<UserAuth>> userAuthFactories = new ArrayList<>(1);
    userAuthFactories.add(new UserAuthPasswordFactory());
    sshd.setUserAuthFactories(userAuthFactories);

    sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
        @Override
        public boolean authenticate(String username, String password, ServerSession session) {
            return username != null && username.equals(password);
        }
    });

    CommandFactory cf = new CommandFactory() {
        @Override
        public Command createCommand(String command) {
            String[] splitCommand;
            if (OsUtils.isUNIX()) {
                splitCommand = SSHInfrastructureHelper.splitCommand(command);
            } else if (OsUtils.isWin32()) {
                splitCommand = SSHInfrastructureHelper.splitCommandWithoutRemovingQuotes(command);
            } else {
                throw new IllegalStateException("Operating system is not recognized");
            }
            StringBuilder rebuiltCommand = new StringBuilder();
            for (String commandPiece : splitCommand) {
                rebuiltCommand.append(commandPiece).append(" ");
            }
            rebuiltCommand.trimToSize();

            if (OsUtils.isUNIX()) {
                return new ProcessShellFactory(new String[] { "/bin/sh", "-c",
                                                              rebuiltCommand.toString() }).create();
            } else {
                return new ProcessShellFactory(new String[] { "cmd.exe", "/C",
                                                              rebuiltCommand.toString() }).create();
            }
        }
    };

    sshd.setCommandFactory(cf);

    sshd.start();

    port = sshd.getPort();

    javaExePath = System.getProperty("java.home") + File.separator + "bin" + File.separator +
                  (OsUtils.isWin32() ? "java.exe" : "java");
    javaExePath = "\"" + javaExePath + "\"";

    infraParams = new Object[] { ("localhost " + NB_NODES + "\n").getBytes(), //hosts
                                 60000, //timeout
                                 0, //attempts
                                 10, //wait between failures
                                 port, //ssh server port
                                 "toto", //ssh username
                                 "toto", //ssh password
                                 new byte[0], // optional ssh private key
                                 new byte[0], // optional ssh options file
                                 javaExePath, //java path on the remote machines
                                 PAResourceManagerProperties.RM_HOME.getValueAsString(), //Scheduling path on remote machines
                                 OperatingSystem.getOperatingSystem(), "" }; // extra java options

    policyParameters = new Object[] { AccessType.ALL.toString(), AccessType.ALL.toString(), "20000" };

}
 
Example #5
Source File: MessageShellFactory.java    From artifactory_ssh_proxy with Apache License 2.0 4 votes vote down vote up
public MessageShellFactory(String message) {
    super(new String[] {}, EnumSet.of(ProcessShellFactory.TtyOptions.ONlCr));
    this.messageBytes = message.getBytes(StandardCharsets.UTF_8);
}