Sample code for 30+ languages & platforms
Java

Authenticate an SSH Tunnel with a Password

See more Socket/SSL/TLS Examples

Demonstrates Socket.SshAuthenticatePw, which authenticates an SSH tunnel session using a login and password. Call SshOpenTunnel first.

Background. Authenticate before opening forwarded channels. The password should come from a secure source and never be logged; public-key authentication is often preferred.

Chilkat Java Downloads

Java
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    boolean success = false;

    CkSocket socket = new CkSocket();

    success = socket.SshOpenTunnel("ssh.example.com",22);
    if (success == false) {
        System.out.println(socket.lastErrorText());
        return;
        }

    //  Authenticate the SSH tunnel session with a login and password.
    //  The SSH password should come from a secure source rather than being hard-coded.
    String sshPassword = "mySshPassword";
    success = socket.SshAuthenticatePw("sshUser",sshPassword);
    if (success == false) {
        System.out.println(socket.lastErrorText());
        return;
        }

    System.out.println("SSH authentication succeeded.");
  }
}