6

first time asking on stackoverflow, and also using sshj. Besides the examples provided with sshj, I haven't really found any good resources to help using this API.

I've been trying to do remote port forwarding using sshj and have run into this error.

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

I tested the auth with a VM, but without using a public key. I would be using this to connect to an EC2 instance on which I know the login.

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

Probably not the best (or even right) way to do this.

user2243685
  • 61
  • 1
  • 1
  • 3
  • Possible duplicate of [SSHJ - Keypair login to EC2 instance](http://stackoverflow.com/questions/9283556/sshj-keypair-login-to-ec2-instance) – AncientSwordRage Sep 22 '16 at 13:38
  • In my case, I tried to connect to `localhost` (Docker container with an SFTP server, atmoz/sftp). It worked after I changed the host to `0.0.0.0`. – Leukipp Apr 28 '22 at 07:48
  • Had same issue, what fixed it for me is using the latest version. – lfmunoz Oct 20 '22 at 04:35

2 Answers2

0

I wrote an example to a similar prior question that you can run directly in groovyconsole that will connect to an EC2 instance: https://stackoverflow.com/a/15800383/311525

Community
  • 1
  • 1
Scott
  • 16,711
  • 14
  • 75
  • 120
0

Try to use client.addHostKeyVerifier(new PromiscuousVerifier());. I send two configuration, first with public key, secound with username/password.

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;
}
Tomasz
  • 884
  • 8
  • 12