Failed to load known_hosts exception using SSHJ

I get an exception when using SSHJ.

Here's how I implemented it:

public static void main(String[] args) throws IOException { // TODO Auto-generated method stub final SSHClient ssh = new SSHClient(); ssh.loadKnownHosts(); ssh.connect("serverName"); try{ ssh.authPublickey("myUserId"); final Session session = ssh.startSession(); try{ final Command cmd = session.exec("net send myMachineName Hello!!!"); System.out.println(cmd.getOutputAsString()); System.out.println("\n Exit Status: "+cmd.getExitStatus()); }finally{ session.close(); } }finally{ ssh.disconnect(); } } } 

But I get the following exception:

 Exception in thread "main" java.io.IOException: Could not load known_hosts at net.schmizz.sshj.SSHClient.loadKnownHosts(SSHClient.java:528) at SSHTEST.main(SSHTEST.java:25) 

What am I doing wrong?

+7
java ssh sshj
source share
3 answers

Remove the call to the loadKnownHosts () method, which by default is referred to as erickson under ~ / .ssh / known_hosts (you can also specify the location as an argument) and replace it with:

 ssh.addHostKeyVerifier("public-key-fingerprint"); 

To find out what a fingerprint is, a twisted path would have to connect without this statement - you will learn from the exception; -)

+5
source share

Use the following code

 final SSHClient ssh = new SSHClient(); ssh.addHostKeyVerifier( new HostKeyVerifier() { public boolean verify(String arg0, int arg1, PublicKey arg2) { return true; // don't bother verifying } } ); ssh.connect("LocalHost"); 
+15
source share

It looks like he is trying to read the file "known_hosts", but cannot find it, or possibly in an invalid format.

The well-known SSH host file writes the public key for different hosts in order to prevent spoofing attacks. It is usually located at ~ / .ssh / known_hosts. Try creating an empty file there and see if this suits the library.

The library documentation will probably list the necessary configuration files.

+2
source share

All Articles