Search code examples
javanetwork-programmingsshjsch

How to get server's SSH version without authenticating in JSch?


I tried to use JSch:

@Test
public void test() throws Exception {
    var session = new JSch().getSession("host");
    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    var version = session.getServerVersion();
    System.out.println("version = " + version);
}

but I'm getting

java.lang.NullPointerException: Cannot read the array length because "str" is null

It seems that this implementation requires you to connect and authenticate.

How can I get this information with Java, without the need to authenticate?


This Python code works for me on a server that requires authentication yet I didn't have to authenticate in order to get the remote version.

import paramiko

ssh = paramiko.Transport(("host", 22))
ssh.start_client()
print(ssh.remote_version)
ssh.close()

Solution

  • The SSH identification string is sent in plain text right after opening the connection.

    So you do not need an SSH library to obtain the string.

    You can do with a simple code like shown here: Read Data from a Java Socket


    Or just try to login using dummy credentials with JSch and then read Session.getServerVersion.

    You do not even have to try to login. You can abort the connection right at the host key check:

    config.put("StrictHostKeyChecking", "yes");