I'm trying to write a java program that will connect via ssh and do some stuff on a server at work (redhat linux). My box is windows. I read about sshj and I'm trying to get the example to work. I've worked through most of the dependencies and now I have an error dealing with public/private keys and unfortunately I don't know much there either (yes, it's a perfect storm of newbie-ness!). Here's the error:
Exception in thread "main" net.schmizz.sshj.transport.TransportException: [HOST_KEY_NOT_VERIFIABLE] Could not verify ssh-rsa
host key with fingerprint 5f:d6:94:00:9e:ec:7e:34:6d:d0:d3:76:df:5e:dd:3d
for myserver
on port 22
Here's the code:
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.connection.channel.direct.Session.Command;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
/** This examples demonstrates how a remote command can be executed. */
public class sshBuddy {
public static void main(String... args)
throws IOException {
final SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
//ssh.addHostKeyVerifier("5f:d6:94:00:9e:ec:7e:34:6d:d0:d3:76:df:5e:dd:3d");
ssh.connect("myserver");
try {
ssh.authPublickey(System.getProperty("myusername"));
final Session session = ssh.startSession();
try {
final Command cmd = session.exec("ping -c 1 google.com");
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();
}
}
}
Any help would be appreciated, thanks!
Using Config File You can also define the strings to disable host key checking in the configuration file. You need to create a ~/. ssh/config file and disable strict host key checking by adding the content. This will disable host checking for all hosts you connect to.
In host key checking, ssh automatically maintains and checks a database containing identification for all hosts it has ever been used with. Host keys are stored in ~/. ssh/known_hosts in the user's home directory. Additionally, the /etc/ssh/ssh_known_hosts file is automatically checked for known hosts.
just try this
ssh.addHostKeyVerifier(new PromiscuousVerifier());
this should work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With