【问题标题】:net.schmizz.sshj.userauth.UserAuthException: Exhausted available authentication methodsnet.schmizz.sshj.userauth.UserAuthException:用尽可用的身份验证方法
【发布时间】:2013-03-26 04:12:22
【问题描述】:

第一次询问stackoverflow,也使用sshj。除了 sshj 提供的示例之外,我还没有找到任何好的资源来帮助使用这个 API。

我一直在尝试使用 sshj 进行远程端口转发,但遇到了这个错误。

Exception in thread "main" net.schmizz.sshj.userauth.UserAuthException: Exhausted available authentication methods

我使用虚拟机测试了身份验证,但没有使用公钥。我将使用它连接到我知道登录名的 EC2 实例。

public void startRemotePortForwardingConnection(LocalPortForwarder.Parameters parameters) throws IOException{
    sshClient.connect(parameters.getLocalHost());
    this.connectionStatus = CONNECTED;
    System.out.print("Connected to localhost" + NEWLINE);

    try {
        sshClient.authPassword(this.username, this.password);
        System.out.print("Authorized with as user " + username + " with password " + password + NEWLINE);

        // the local port we should be listening on
        RemotePortForwarder.Forward localPortToListenOn = new RemotePortForwarder.Forward(parameters.getLocalPort());
        System.out.print("localPortToListenOn initialized" + NEWLINE);

        // where we should forward the packets
        InetSocketAddress socketAddress = new InetSocketAddress(parameters.getRemoteHost(), parameters.getRemotePort());
        SocketForwardingConnectListener remotePortToForwardTo = new SocketForwardingConnectListener(socketAddress);
        System.out.print("remotePortToForwardTo initialized" + NEWLINE);

        //bind the forwarder to the correct ports
        sshClient.getRemotePortForwarder().bind(localPortToListenOn, remotePortToForwardTo);
        System.out.print("ports bound together" + NEWLINE);

        sshClient.getTransport().setHeartbeatInterval(30);
        sshClient.getTransport().join();
    }
    finally {
        sshClient.disconnect();
    }
}

可能不是最好的(甚至是正确的)方法。

【问题讨论】:

标签: ssh portforwarding sshj


【解决方案1】:

尝试使用 client.addHostKeyVerifier(new PromiscuousVerifier());。我发送了两个配置,第一个是公钥,第二个是用户名/密码。

private static SSHClient setupSshj(String remoteHost, String username, String password) throws Exception {
    SSHClient client = new SSHClient();
    client.addHostKeyVerifier(new PromiscuousVerifier());
    client.connect(remoteHost);
    client.authPassword(username, password);
    return client;
}

private static SSHClient setupSshWithPublicKey(String remoteHost, int port, String username, String publicKey) throws Exception {
    SSHClient client = new SSHClient();
    client.addHostKeyVerifier(new PromiscuousVerifier());
    client.connect(remoteHost, port);
    client.authPublickey(username, publicKey);
    return client;
}

【讨论】:

    【解决方案2】:

    我为之前的一个类似问题写了一个示例,您可以直接在 groovyconsole 中运行该示例,该示例将连接到 EC2 实例:https://stackoverflow.com/a/15800383/311525

    【讨论】:

      猜你喜欢
      • 2013-12-30
      • 2014-12-13
      • 2020-04-03
      • 2013-07-21
      • 1970-01-01
      • 1970-01-01
      • 2010-10-24
      • 2021-04-28
      • 1970-01-01
      相关资源
      最近更新 更多