java - 使用 SSHJ 连接到 Google Compute Engine 实例

标签 java google-compute-engine sshj

我在 Google 计算引擎中创建了一个实例,我需要使用 SSHJ 进行连接,但我没有得到。我可以使用 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 要做什么?

我使用 puttygen 创建了公钥和私钥。然后我访问了 Google 计算引擎的控制台并导入了公钥。这样我就可以使用私钥通过 putty 进行访问。

当我尝试使用上面定义的exec方法通过SSHJ访问时,出现错误:

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

有什么建议吗?

最佳答案

PuTTY .ppk key 不是 PKCS#8 格式。您的代码正在尝试读取 PKCS#8 key 对。尝试将您的私钥转换为 PKCS#8 或在您的代码中使用不同的 key 格式/方法 SSHJ key provider list .

关于java - 使用 SSHJ 连接到 Google Compute Engine 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32025208/

相关文章:

java - Look&Feel 忽略了自定义 TableCellRenderer

java - Python-Django 项目可以在云端(gae)和本地运行,无需互联网连接

google-cloud-platform - Google Compute Engine 上的 FreeBSD

kubernetes - 如何更改整个 Kubernetes 集群时区

mysql - 间歇性2013, "Lost connection to MySQL server at '读取初始通信包',系统错误: 0"to CloudSQL

java - 如何使用 JAVA 将字符串回显到 SSH

java - 仅打印一次特定的打印语句

java - 如何使用 SSHJ 通过 SCP 流式传输文件?

java - 使用 SSHJ 连接到 AWS EC2 实例

java - 识别文件扩展名,以不同颜色打印到JTextArea