Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSch SSH connection throws NPE when initializing reverse tunnel

Tags:

java

jsch

sshd

When I'm adding a reverse tunnel to a com.jcraft.jsch.Session object, the connection initialization fails with the following stacktrace:

com.jcraft.jsch.JSchException: java.lang.NullPointerException
        at com.jcraft.jsch.Session._setPortForwardingR(Session.java:2165)
        at com.jcraft.jsch.Session.setPortForwardingR(Session.java:1937)
        at com.jcraft.jsch.Session.setPortForwardingR(Session.java:1883)
        at com.project.client.handlers.SshClientHandler.<init>(SshClientHandler.java:41)
        at com.project.client.pcConnection.init(SdConnection.java:30)
        at Sdclient.main(Unknown Source)
Caused by: java.lang.NullPointerException
        at com.jcraft.jsch.Packet.padding(Packet.java:58)
        at com.jcraft.jsch.Session.encode(Session.java:892)
        at com.jcraft.jsch.Session._write(Session.java:1362)
        at com.jcraft.jsch.Session.write(Session.java:1357)
        at com.jcraft.jsch.Session._setPortForwardingR(Session.java:2160)
        ... 5 more

The full code there is

private static JSch sshConn = null;
private Session sshSession;
public SshClientHandler(int _sshLocalSp, int _sshRemoteSp) {

    JSch.setLogger(new JSCHLogger());
     sshConn = new JSch();
    try {
        createTemporarySshFiles();

        sshConn.setKnownHosts(GeneralMethods.getPreference(PcPreferencesEnum.SSH_KNOWN_HOSTS_FILE));
        sshConn.addIdentity(GeneralMethods.getPreference(PcPreferencesEnum.SSHC_PRIVATE_KEY_FILE), GeneralMethods.getPreference(PcPreferencesEnum.SSHC_PUBLIC_KEY_FILE), "".getBytes());

        sshSession = sshConn.getSession(GeneralMethods.getPropValue("pcclient.id"), "sshserver.project.com", 22);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        sshSession.setConfig(config);

        sshSession.setTimeout(15000);
        sshSession.setPassword("");
        //sshSession.setPortForwardingR("50000:localhost:22");
        sshSession.setPortForwardingR(50000, "127.0.0.1", 22);
        sshSession.connect();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
}

The connection estabishes successfully w/ publickey authentication when I remove the line

sshSession.setPortForwardingR(50000, "127.0.0.1", 22);

The SSH user has the right to connect to the local port 50000 on the remote machine. Here is a snippet from it's authorized_keys

no-pty,permitopen="localhost:50000",command="/bin/echo not-allowed-to-do-this",no-X11-forwarding ssh-rsa AAAA[...]

I switched arguments for setPortForwardingR back and forth, as - for example - some documents I found online use the remote machine as second argument, some use localhost, but with no success.

Watching auth.log on the remote server indicates that the connection is not even initiated. The NullPointerException gets thrown on the actual line of the setPortForwardingR call. I ensured that my local SSH server is running on the local port 22, and I can connect manually to it. I tried different ports (to my local MySQL server, e.g.), but it always fails with the same stacktrace.

I'm using jsch-0.1.52.jar.

like image 632
Dennis Winter Avatar asked May 08 '26 08:05

Dennis Winter


1 Answers

You have to call the .setPortForwardingR() only after the .connect().

See for example:
https://web.archive.org/web/20161220183323/http://www.jcraft.com/jsch/examples/Daemon.java

like image 51
Martin Prikryl Avatar answered May 09 '26 21:05

Martin Prikryl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!