Search in sources :

Example 1 with CommandFactory

use of org.apache.sshd.server.CommandFactory in project testcases by coheigea.

the class SSHTest method setup.

@Before
public void setup() throws Exception {
    int port = AvailablePortFinder.getNextAvailable(10000);
    // Write port number to configuration file in target
    String basedir = System.getProperty("basedir");
    if (basedir == null) {
        basedir = new File(".").getCanonicalPath();
    }
    // Read in camel configuration file and substitute in the correct port
    File f = new File(basedir + "/src/test/resources/camel-ssh.xml");
    FileInputStream inputStream = new FileInputStream(f);
    String content = IOUtils.toString(inputStream, "UTF-8");
    inputStream.close();
    content = content.replaceAll("portno", "" + port);
    File f2 = new File(basedir + "/target/test-classes/camel-ssh.xml");
    FileOutputStream outputStream = new FileOutputStream(f2);
    IOUtils.write(content, outputStream, "UTF-8");
    outputStream.close();
    sshServer = SshServer.setUpDefaultServer();
    sshServer.setPort(port);
    // Generate a key
    sshServer.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(Paths.get("target/generatedkey.pem")));
    // Simple CommandFactory to run commands in a process
    sshServer.setCommandFactory(new CommandFactory() {

        public Command createCommand(String command) {
            return new ProcessShellFactory(command.split(";")).create();
        }
    });
    // sshServer.setShellFactory(new ProcessShellFactory(new String[] { "/bin/sh", "-i", "-l" }));
    sshServer.setPasswordAuthenticator(new CamelPasswordAuthenticator());
    sshServer.start();
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) Command(org.apache.sshd.server.Command) FileOutputStream(java.io.FileOutputStream) ProcessShellFactory(org.apache.sshd.server.shell.ProcessShellFactory) CommandFactory(org.apache.sshd.server.CommandFactory) File(java.io.File) FileInputStream(java.io.FileInputStream) Before(org.junit.Before)

Example 2 with CommandFactory

use of org.apache.sshd.server.CommandFactory in project scheduling by ow2-proactive.

the class TestSSHInfrastructureV2 method startSSHServer.

@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[] { // hosts
    ("localhost " + NB_NODES + "\n").getBytes(), // timeout
    60000, // attempts
    0, // wait between failures
    10, // ssh server port
    port, // ssh username
    "toto", // ssh password
    "toto", // optional ssh private key
    new byte[0], // optional ssh options file
    new byte[0], // java path on the remote machines
    javaExePath, // Scheduling path on remote machines
    PAResourceManagerProperties.RM_HOME.getValueAsString(), OperatingSystem.getOperatingSystem(), "", "", "", "", // extra java options and startup script parameters
    "" };
    policyParameters = new Object[] { AccessType.ALL.toString(), AccessType.ALL.toString(), "20000" };
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) UserAuthPasswordFactory(org.apache.sshd.server.auth.password.UserAuthPasswordFactory) ServerSession(org.apache.sshd.server.session.ServerSession) ArrayList(java.util.ArrayList) CommandFactory(org.apache.sshd.server.CommandFactory) ScpCommandFactory(org.apache.sshd.server.scp.ScpCommandFactory) PasswordAuthenticator(org.apache.sshd.server.auth.password.PasswordAuthenticator) NamedFactory(org.apache.sshd.common.NamedFactory) ProcessShellFactory(org.apache.sshd.server.shell.ProcessShellFactory) BeforeClass(org.junit.BeforeClass)

Aggregations

CommandFactory (org.apache.sshd.server.CommandFactory)2 SimpleGeneratorHostKeyProvider (org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider)2 ProcessShellFactory (org.apache.sshd.server.shell.ProcessShellFactory)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1 NamedFactory (org.apache.sshd.common.NamedFactory)1 Command (org.apache.sshd.server.Command)1 PasswordAuthenticator (org.apache.sshd.server.auth.password.PasswordAuthenticator)1 UserAuthPasswordFactory (org.apache.sshd.server.auth.password.UserAuthPasswordFactory)1 ScpCommandFactory (org.apache.sshd.server.scp.ScpCommandFactory)1 ServerSession (org.apache.sshd.server.session.ServerSession)1 Before (org.junit.Before)1 BeforeClass (org.junit.BeforeClass)1