我应该如何使用sshj来连接ssh,这让我很困惑

disbfnqx  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(586)

虽然sshj的github中有一些例子,但我不明白我在做什么。我使用以下代码尝试连接到我的主机,仅此而已

@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();
    }

跑步的结果是这样的

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

密钥文件的格式

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

我不知道该怎么办,jsch不支持这种格式的密钥文件

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题