>>  SSH user@host -i key.pem

There are many ways to do this - here is the simplest

// Do it ONCE in your main method

SshClient client = SshClient.setupDefaultClient();

client.start();

// to connect and authenticate in the code

try (ClientSession session = client.connect(user, host)

                        .verify(...timeout...)

                        .getSession()) {

session.setKeyIdentityProvider(
     new FileKeyPairProvider(...path to the PEM file...));

session.auth().verify(... timeout... );

// now you can open a shell channel and do interactive stuff,

// or SCP or SFTP or execute a singe command, or open

// a remote/local port forwarding - or all of the above...

}


// if you don't want to load the key file repeatedly you can do it once

// in the main method and then call (once)


client.setKeyIdentityProvider(...);

// instead of

session.setKeyIdentityProvider(...);


For the rest (advanced usage, setup, etc.), please read the documentation
at https://github.com/apache/mina-sshd/ and also check out the test code in
the project.

Lyor G.

Reply via email to