Search in sources :

Example 1 with CommandFactory

use of org.apache.sshd.server.command.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.setPublickeyAuthenticator(new CamelPublicKeyAuthenticator());
    sshServer.start();
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) Command(org.apache.sshd.server.command.Command) FileOutputStream(java.io.FileOutputStream) ProcessShellFactory(org.apache.sshd.server.shell.ProcessShellFactory) CommandFactory(org.apache.sshd.server.command.CommandFactory) File(java.io.File) FileInputStream(java.io.FileInputStream) Before(org.junit.Before)

Aggregations

File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 Command (org.apache.sshd.server.command.Command)1 CommandFactory (org.apache.sshd.server.command.CommandFactory)1 SimpleGeneratorHostKeyProvider (org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider)1 ProcessShellFactory (org.apache.sshd.server.shell.ProcessShellFactory)1 Before (org.junit.Before)1