1

I created an instance in the Google Compute Engine and I need connect using SSHJ, but I'm not getting. I can connect successfully using the AWS:

public String exec(String host, String keyPair, String script) throws IOException{
    File keyPairFile = new File(keyPair);
    SSHClient ssh = new SSHClient();
    ssh.addHostKeyVerifier(new PromiscuousVerifier());
    ssh.connect(host);

    PKCS8KeyFile keyFile = new PKCS8KeyFile();
    keyFile.init(keyPairFile);
    ssh.auth(USER_DEFAULT, new AuthPublickey(keyFile));

    Session session = null;
    try {
        logger.info("Conectando via ssh " + host + "...");
        session = ssh.startSession();
        final Command command = session.exec(script);
        String response = IOUtils.readFully(command.getInputStream()).toString();
        command.join(10, TimeUnit.SECONDS);
        return response;
    } finally {
        session.close();
        ssh.disconnect();
        ssh.close();
    }
}

Google Compute Engine to do?

I created a public key and a private key using puttygen.Then I accessed the console of my Google Compute Engine and imported the public key. That way I could access with putty using the private key.

When I try to access with SSHJ using the exec method defined above, the error occurs:

Exception in thread "main" net.schmizz.sshj.userauth.UserAuthException: Exhausted available authentication methods
    at net.schmizz.sshj.userauth.UserAuthImpl.authenticate(UserAuthImpl.java:114)
    at net.schmizz.sshj.SSHClient.auth(SSHClient.java:205)
    at net.schmizz.sshj.SSHClient.auth(SSHClient.java:190)
    at br.com.clouddeploy.service.SSHConnect.exec(SSHConnect.java:31)
    at br.com.clouddeploy.main.TestConecte.main(TestConecte.java:17)
Caused by: net.schmizz.sshj.userauth.UserAuthException: Problem getting public key from PKCS8KeyFile{resource=[PrivateKeyFileResource] E:\Essencial\SkyDrive\MESTRADO\Disciplinas\2014 e 2015\EDD\Pesquisa\google-teste.ppk}
    at net.schmizz.sshj.userauth.method.KeyedAuthMethod.putPubKey(KeyedAuthMethod.java:46)
    at net.schmizz.sshj.userauth.method.AuthPublickey.buildReq(AuthPublickey.java:62)
    at net.schmizz.sshj.userauth.method.AuthPublickey.buildReq(AuthPublickey.java:81)
    at net.schmizz.sshj.userauth.method.AbstractAuthMethod.request(AbstractAuthMethod.java:63)
    at net.schmizz.sshj.userauth.UserAuthImpl.authenticate(UserAuthImpl.java:92)
    ... 4 more
Caused by: java.io.IOException: Could not read key pair from: [PrivateKeyFileResource] E:\Essencial\SkyDrive\MESTRADO\Disciplinas\2014 e 2015\EDD\Pesquisa\google-teste.ppk
    at net.schmizz.sshj.userauth.keyprovider.PKCS8KeyFile.readKeyPair(PKCS8KeyFile.java:145)
    at net.schmizz.sshj.userauth.keyprovider.PKCS8KeyFile.getPublic(PKCS8KeyFile.java:72)
    at net.schmizz.sshj.userauth.method.KeyedAuthMethod.putPubKey(KeyedAuthMethod.java:44)
    ... 8 more

Any suggestion?

Tiago Rolim
  • 65
  • 1
  • 11
  • 1
    What's the error you get? – Andreas Veithen Aug 15 '15 at 16:15
  • I am in doubt in the parameter "keyPair". For the AWS cloud the way I spend my keyPair file (.pem) I download the AWS console. Already on Google Computer Engine, I'm creating my instance using jclouds, and he already creates a user "jclouds" and ssh-rsa key. I should download the ssh-rsa key to my local machine? How to do this procedure? – Tiago Rolim Aug 15 '15 at 18:02

1 Answers1

0

PuTTY .ppk keys are not PKCS#8 format. Your code is trying to read PKCS#8 key pair. Try converting your private key to the PKCS#8 or use different key formats/methods in your code from this SSHJ key provider list.

Kamran
  • 3,397
  • 26
  • 40