Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to a remote machine with username and password using sshj java api?

Tags:

java

ssh

sshj

How to connect to a remote machine with username and password using sshj java api?

I tried this code. What is the problem with this code?

final SSHClient ssh = new SSHClient();
        ssh.connect("192.168.0.1");
        ssh.authPassword("abcde", "fgh".toCharArray());
        try {
            final Session session = ssh.startSession();
            try {
                final Command cmd = session
                        .exec("cd /home/abcde/Desktop/");
                System.out.println(IOUtils.readFully(cmd.getInputStream())
                        .toString());
                cmd.join(5, TimeUnit.SECONDS);
                System.out.println("\n** exit status: " + cmd.getExitStatus());
            } finally {
                session.close();
            }
        } finally {
            ssh.disconnect();
        }

It is throwing this following error.

net.schmizz.sshj.transport.TransportException: [HOST_KEY_NOT_VERIFIABLE] Could not verify ssh-rsa host key with fingerprint ******** for 192.168.0.1 on port 22

like image 940
rgksugan Avatar asked Mar 05 '26 14:03

rgksugan


1 Answers

You solve your problem by implementing HostKeyVerifier

class NullHostKeyVerifier implements HostKeyVerifier {
    @Override
    public boolean verify(String arg0, int arg1, PublicKey arg2) {
        return true;
    }        
}

and adding this fake implementation to your SSHClient instance configuration:

...
    final SSHClient ssh = new SSHClient();
    ssh.addHostKeyVerifier(new NullHostKeyVerifier());
...
like image 96
Carlos Rodrigues Avatar answered Mar 07 '26 05:03

Carlos Rodrigues