javasshsshj

How should I use sshj to connect with ssh, which makes me very confused


Although there are examples in sshj's github, I don’t understand what I’m doing. I use the following code to try to connect to my host, and that’s it

 @Test
    public void sshTest() throws IOException {
        SSHClient client = new SSHClient();
        String username = "username";
        File privateKey = new File("D:\\SSH\\remote_new");
        client.addHostKeyVerifier(new PromiscuousVerifier());
        client.connect("myMostIp", 22);
        KeyProvider keys = client.loadKeys(privateKey.getPath());
        client.authPublickey(username, keys);
        client.close();
    }

The result of running is like this

net.schmizz.sshj.userauth.UserAuthException: Exhausted available authentication methods


Caused by: net.schmizz.sshj.userauth.UserAuthException: Problem getting public key from PKCS8KeyFile{resource=[PrivateKeyFileResource] D:\SSH\matcloud_new}

    ... 66 more
Caused by: java.io.IOException: unrecognised object: OPENSSH PRIVATE KEY
    at org.bouncycastle.openssl.PEMParser.readObject(Unknown Source)
    ... 71 more

Format of the key file

-----BEGIN OPENSSH PRIVATE KEY-----
XXXXXXXXX
-----END OPENSSH PRIVATE KEY-----

I don't know what to do, jsch does not support key files in this format


Solution

  • The most recent release is 0.30.0 although as I said before 0.27.0 (from 2 years ago) or higher should fully support OpenSSH-newfmt files. – dave_thompson_085

    use it https://github.com/hierynomus/sshj#user-content-release-history