I am using Jsch(Jcraft) library to establish a SSH connection with a SSH server as shown below :
JSch jsch = new JSch();
String user = "****";
String host = "****";
int port = 22;
String privateKey = "***.ppk";//Path to private key(The file is in .ppk format)
try
{
jsch.addIdentity(privateKey);
Session session = jsch.getSession(user, host, port);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
sftpChannel.connect();
/*file transfer code*/
sftpChannel.disconnect();
session.disconnect();
}
catch (Exception e)
{
e.printStackTrace();
}
The SSH connection is successfully established as "StrictHostKeyChecking" is disabled. If its enabled I get the following error :
com.jcraft.jsch.JSchException: UnknownHostKey: ******. RSA key f
is *************
at com.jcraft.jsch.Session.checkHost(Session.java:805)
at com.jcraft.jsch.Session.connect(Session.java:345)
at com.jcraft.jsch.Session.connect(Session.java:183)
I understand we need to set know host file in the code as shown below :
jsch.setKnownHosts(knownHostsFileName);
I am unable to generate known_hosts file via the command below :
ssh-keyscan <HOST> > known_hosts
It is throwing the following error :
'ssh-keyscan' is not recognized as an internal or external command,
operable program or batch file.
I have only public and private keys in .ppk format. I dont have the known_host file.
How do we create known_host file?
Why is ssh-keyscan command throwing error - not recognized as internal/external command?