1

I am having lots of problems with this.

I have the following code

            try {
        final SSHClient ssh = new SSHClient();
        PKCS8KeyFile keyFile = new PKCS8KeyFile();
        keyFile.init(new File(Thread.currentThread().getContextClassLoader().getResource("development.pem").toURI()));
        ssh.loadKnownHosts();
        ssh.addHostKeyVerifier("ec2-XX-XX-XX-XX.compute-1.amazonaws.com", 22, "ff:59:aa:24:42:b1:a0:9f:c9:4c:73:34:fb:95:53:c2:b8:37:a8:f8");
//      ssh.addHostKeyVerifier("ec2-XX-XX-XX-XX.compute-1.amazonaws.com", 22, "90:1e:4d:09:42:c4:16:8a:4c:dc:ae:c2:60:14:f9:ea");
        ssh.connect("ec2-XX-XX-XX-XX.compute-1.amazonaws.com");
        ssh.auth("ec2-user", new AuthPublickey(keyFile));
        Session session = ssh.startSession();
        Command sudo = session.exec("sudo su -");
        System.out.println("sudo=" +sudo.getOutputAsString());
        Command whoami = session.exec("whoami");
        System.out.println("whoami=" + whoami.getOutputAsString());
        } catch (Exception e) {
            e.printStackTrace();
        }

The first addHostKeyVerifier is using the fingerprint on the AWS console, the commented out one is the one that it keeps telling me it is failing against. Where am i meant to get the correct key from.

If i use the second key it passes verification then fails afterwards.

I am using SSHJ version 0.8.1

  • Did you try setting up an elastic IP? I ran into some slightly different code issues with connections trying to connect to a similar hostname, but once I set up my elastic IP to the EC2 instance, I was able to do my SSH connection. – DaBaer Dec 05 '12 at 14:51
  • No, i want to be able to connect to any of my servers. I ended up ditching sshj and changed to mindterm which i got up and running in about 15 minutes. – George Taylor Dec 06 '12 at 15:22
  • I answered a similar question here that may be useful http://stackoverflow.com/a/15800383/311525 – Scott May 18 '13 at 15:48

1 Answers1

1

This worked for me.

For your PEM file you need to use the OpenSSHKeyFile key provider.

  SSHClient ssh = new SSHClient();
  OpenSSHKeyFile keyFile = new OpenSSHKeyFile();
  File file = new File("c:\\full\\path\\to\\keyfile.pem");
  keyFile.init(file);

Personally, I just surpressed the host key verification to always return true. But I'm sure your way is more secure (if it works).

  ssh.loadKnownHosts();
  ssh.addHostKeyVerifier((a, b, c) -> true);

The username for AWS depends on your image. Very often it is "root". In my case, it was "ubuntu".

  ssh.connect("ec2-54-165-233-48.compute-1.amazonaws.com");
  ssh.auth("ubuntu", new AuthPublickey(keyFile));
  Session session = ssh.startSession();

(Note: I'm using version 0.26.0 though.)

bvdb
  • 22,839
  • 10
  • 110
  • 123