sshj icon indicating copy to clipboard operation
sshj copied to clipboard

Local Port forwarding example does not work

Open aruld opened this issue 11 years ago • 3 comments

I am trying to run the LocalPF example that comes with the distribution, with public key auth, but does not work.

public class LocalPF {

    public static void main(String... args)
            throws IOException {
        SSHClient ssh = new SSHClient();

        ssh.loadKnownHosts();

        ssh.connect("localhost");
        try {

            ssh.authPublickey(System.getProperty("user.name"));

            /*
            * _We_ listen on localhost:8080 and forward all connections on to server, which then forwards it to
            * google.com:80
            */
            final LocalPortForwarder.Parameters params
                    = new LocalPortForwarder.Parameters("0.0.0.0", 8080, "google.com", 80);
            final ServerSocket ss = new ServerSocket();
            ss.setReuseAddress(true);
            ss.bind(new InetSocketAddress(params.getLocalHost(), params.getLocalPort()));
            try {
                ssh.newLocalPortForwarder(params, ss).listen();
            } finally {
                ss.close();
            }

        } finally {
            ssh.disconnect();
        }
    }

}

The debug output when I run this example: Any idea why it fails to run?

/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/bin/java -Didea.launcher.port=7543 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA 13.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/lib/javafx-doclet.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/lib/tools.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/lib/htmlconverter.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/lib/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Users/arul/github/sshclient-windows-bug/target/classes:/Users/arul/.m2/repository/com/hierynomus/sshj/0.11.0/sshj-0.11.0.jar:/Users/arul/.m2/repository/org/bouncycastle/bcpkix-jdk15on/1.51/bcpkix-jdk15on-1.51.jar:/Users/arul/.m2/repository/org/bouncycastle/bcprov-jdk15on/1.51/bcprov-jdk15on-1.51.jar:/Users/arul/.m2/repository/com/jcraft/jzlib/1.1.3/jzlib-1.1.3.jar:/Users/arul/.m2/repository/org/slf4j/slf4j-api/1.7.7/slf4j-api-1.7.7.jar:/Users/arul/.m2/repository/org/bouncycastle/bcprov-ext-jdk15on/1.51/bcprov-ext-jdk15on-1.51.jar:/Users/arul/.m2/repository/org/slf4j/slf4j-log4j12/1.7.10/slf4j-log4j12-1.7.10.jar:/Users/arul/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar:/Applications/IntelliJ IDEA 13.app/Contents/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain LocalPF
2015-02-16 17:14:40,235 [main] DEBUG net.schmizz.sshj.common.SecurityUtils - Trying to register BouncyCastle as a JCE provider
2015-02-16 17:14:40,830 [main] INFO  net.schmizz.sshj.common.SecurityUtils - BouncyCastle registration succeeded
2015-02-16 17:14:40,900 [main] INFO  net.schmizz.sshj.transport.TransportImpl - Client identity string: SSH-2.0-SSHJ_0_9_2
2015-02-16 17:14:40,907 [main] INFO  net.schmizz.sshj.transport.TransportImpl - Server identity string: SSH-2.0-OpenSSH_6.2
2015-02-16 17:14:40,907 [main] DEBUG net.schmizz.concurrent.Promise - Setting <<kex done>> to `null`
2015-02-16 17:14:40,907 [main] DEBUG net.schmizz.sshj.transport.KeyExchanger - Sending SSH_MSG_KEXINIT
2015-02-16 17:14:40,910 [main] DEBUG net.schmizz.concurrent.Promise - Setting <<kexinit sent>> to `SOME`
2015-02-16 17:14:40,910 [reader] DEBUG net.schmizz.sshj.transport.KeyExchanger - Received SSH_MSG_KEXINIT
2015-02-16 17:14:40,910 [main] DEBUG net.schmizz.concurrent.Promise - Awaiting <<kex done>>
2015-02-16 17:14:40,911 [reader] DEBUG net.schmizz.sshj.transport.KeyExchanger - Negotiated algorithms: [ kex=diffie-hellman-group14-sha1; sig=ssh-rsa; c2sCipher=aes128-ctr; s2cCipher=aes128-ctr; c2sMAC=hmac-sha1; s2cMAC=hmac-sha1; c2sComp=none; s2cComp=none ]
2015-02-16 17:14:40,944 [reader] DEBUG net.schmizz.sshj.transport.kex.DHG14 - Sending SSH_MSG_KEXDH_INIT
2015-02-16 17:14:40,951 [reader] DEBUG net.schmizz.sshj.transport.KeyExchanger - Received kex followup data
2015-02-16 17:14:40,951 [reader] DEBUG net.schmizz.sshj.transport.kex.DHG14 - Received SSH_MSG_KEXDH_REPLY
2015-02-16 17:14:40,983 [reader] DEBUG net.schmizz.sshj.transport.KeyExchanger - Trying to verify host key with net.schmizz.sshj.transport.verification.OpenSSHKnownHosts@6fcc5b5d
2015-02-16 17:14:40,983 [reader] DEBUG net.schmizz.sshj.transport.KeyExchanger - Sending SSH_MSG_NEWKEYS
2015-02-16 17:14:40,983 [reader] DEBUG net.schmizz.sshj.transport.KeyExchanger - Received SSH_MSG_NEWKEYS
2015-02-16 17:14:40,987 [reader] DEBUG net.schmizz.concurrent.Promise - Setting <<kexinit sent>> to `null`
2015-02-16 17:14:40,987 [reader] DEBUG net.schmizz.concurrent.Promise - Setting <<kex done>> to `SOME`
2015-02-16 17:14:40,987 [main] DEBUG net.schmizz.sshj.SSHClient - Key exchange took 0.08 seconds
2015-02-16 17:14:40,987 [main] DEBUG net.schmizz.sshj.SSHClient - Attempting to load key from: /Users/arul/.ssh/id_rsa
2015-02-16 17:14:41,001 [main] DEBUG net.schmizz.sshj.SSHClient - Attempting to load key from: /Users/arul/.ssh/id_dsa
2015-02-16 17:14:41,003 [main] DEBUG net.schmizz.concurrent.Promise - Setting <<service accept>> to `null`
2015-02-16 17:14:41,003 [main] DEBUG net.schmizz.sshj.transport.TransportImpl - Sending SSH_MSG_SERVICE_REQUEST for ssh-userauth
2015-02-16 17:14:41,003 [main] DEBUG net.schmizz.concurrent.Promise - Awaiting <<service accept>>
2015-02-16 17:14:41,004 [reader] DEBUG net.schmizz.concurrent.Promise - Setting <<service accept>> to `SOME`
2015-02-16 17:14:41,005 [main] DEBUG net.schmizz.sshj.transport.TransportImpl - Setting active service to ssh-userauth
2015-02-16 17:14:41,005 [main] DEBUG net.schmizz.concurrent.Promise - Setting <<authenticated>> to `null`
2015-02-16 17:14:41,005 [main] DEBUG net.schmizz.sshj.userauth.UserAuthImpl - Trying `publickey` auth...
2015-02-16 17:14:41,005 [main] DEBUG net.schmizz.sshj.userauth.method.AuthPublickey - Attempting authentication using PKCS8KeyFile{resource=[PrivateKeyFileResource] /Users/arul/.ssh/id_rsa}
2015-02-16 17:14:41,006 [main] DEBUG net.schmizz.concurrent.Promise - Awaiting <<authenticated>>
2015-02-16 17:14:41,011 [reader] DEBUG net.schmizz.concurrent.Promise - Setting <<authenticated>> to `false`
2015-02-16 17:14:41,011 [main] DEBUG net.schmizz.sshj.userauth.UserAuthImpl - `publickey` auth failed
2015-02-16 17:14:41,012 [main] DEBUG net.schmizz.concurrent.Promise - Setting <<authenticated>> to `null`
2015-02-16 17:14:41,012 [main] DEBUG net.schmizz.sshj.userauth.UserAuthImpl - Trying `publickey` auth...
2015-02-16 17:14:41,012 [main] DEBUG net.schmizz.sshj.userauth.method.AuthPublickey - Attempting authentication using PKCS8KeyFile{resource=[PrivateKeyFileResource] /Users/arul/.ssh/id_dsa}
2015-02-16 17:14:41,012 [main] DEBUG net.schmizz.concurrent.Promise - Awaiting <<authenticated>>
2015-02-16 17:14:41,013 [reader] DEBUG net.schmizz.concurrent.Promise - Setting <<authenticated>> to `false`
2015-02-16 17:14:41,013 [main] DEBUG net.schmizz.sshj.userauth.UserAuthImpl - `publickey` auth failed
2015-02-16 17:14:41,013 [main] INFO  net.schmizz.sshj.transport.TransportImpl - Disconnected - BY_APPLICATION
2015-02-16 17:14:41,013 [main] DEBUG net.schmizz.sshj.userauth.UserAuthImpl - Notified of net.schmizz.sshj.transport.TransportException: [BY_APPLICATION] Disconnected
2015-02-16 17:14:41,013 [main] DEBUG net.schmizz.sshj.transport.TransportImpl - Sending SSH_MSG_DISCONNECT: reason=[BY_APPLICATION], msg=[]
2015-02-16 17:14:41,014 [main] DEBUG net.schmizz.concurrent.Promise - Setting <<transport close>> to `SOME`
2015-02-16 17:14:41,014 [reader] DEBUG net.schmizz.sshj.transport.Reader - Stopping
Exception in thread "main" net.schmizz.sshj.userauth.UserAuthException: Exhausted available authentication methods
    at net.schmizz.sshj.SSHClient.auth(SSHClient.java:217)
    at net.schmizz.sshj.SSHClient.authPublickey(SSHClient.java:316)
    at net.schmizz.sshj.SSHClient.authPublickey(SSHClient.java:365)
    at net.schmizz.sshj.SSHClient.authPublickey(SSHClient.java:295)
    at LocalPF.main(LocalPF.java:31)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

aruld avatar Feb 16 '15 23:02 aruld

Judging from the error message, your public key auth is not working. Try setting it up with username/password auth.

hierynomus avatar Feb 17 '15 08:02 hierynomus

Thanks! I figured it out the problem with the keys. authorized_keys was incorrectly setup, was missing the public key of the localhost. Fixing it resolved this.

aruld avatar Feb 17 '15 15:02 aruld

Great! Glad you managed to solve it.

2015-02-17 16:25 GMT+01:00 Arul Dhesiaseelan [email protected]:

Thanks! I figured it out the problem with the keys. authorized_keys was incorrectly setup, was missing the public key of the localhost. Fixing it resolved this.

— Reply to this email directly or view it on GitHub https://github.com/hierynomus/sshj/issues/177#issuecomment-74684997.

hierynomus avatar Feb 18 '15 15:02 hierynomus