I am trying to run kafka shell scripts on a remote server to retrieve the list of consumer-groups and offsets. This is an alternative way rather than using jmx through the jvm [http://openjdk.java.net/tools/svc/jconsole/jconsole}. I am using jsch and getting the unknown host key error.
There are potential solutions from other posted questions, but I am not sure how to and which host key to retrieve. I can see 3 host key-server files on the server but not sure which one to copy.
Here is the code and error:
JSch jsch = new JSch();
String keyString = "";
//byte [] key = Base64.getDecoder().decode("b6:83:60:7c:4a:98:00:ef:b9:4a:ba:b6:80:39:d6:42");
//HostKey hostkey = new HostKey(host,key);
//jsch.getHostKeyRepository().add(hostkey, null);
jsch.setKnownHosts(new FileInputStream("C:\\Users\\.ssh\\host"));
Session session = jsch.getSession(uname, host, 22);
Properties config = new Properties();
//config.put("StrickHostKeyChecking","no");
session.setConfig(config);
session.setPassword(pword);
session.connect();
This is the error produced on the session.connect() line:
com.jcraft.jsch.JSchException: UnknownHostKey: 192.168.xx.1xx. RSA key
fingerprint is b6:83:60:7c:4a:98:00:ef:b9:4a:ba:b6:80:39:d6:42
EDIT:
Tried the following changes with the ssh_host_rsa_key.pub file copied from server to client. Added the following:
JSch jsch = new JSch();
String keyString = "";
//byte [] key =
Base64.getDecoder().
decode("b6:83:60:7c:4a:98:00:ef:b9:4a:ba:b6:80:39:d6:42");
//HostKey hostkey = new HostKey(host,key);
//jsch.getHostKeyRepository().add(hostkey, null);
jsch.setKnownHosts("C:\\Users\\.ssh\\ssh_host_rsa_key.pub");
Session session = jsch.getSession(uname, host, 22);
Properties config = new Properties();
session.setConfig(config);
session.setPassword(pword);
session.connect();
Creates the same error of JSchException: UnknownHostKey...RSA key is fingerporint. Then tried the follow way to actually read the file contents:
String knownHostPublicKey = "";
String readLine = "";
BufferedReader fr = new BufferedReader(new
FileReader("C:\\Users\\.ssh\\ssh_host_rsa_key.pub"));
while((readLine = fr.readLine()) != null){
knownHostPublicKey = readLine;
System.out.println(knownHostPublicKey);
}
// jsch.setKnownHosts(new ByteArrayInputStream(knownHostPublicKey.getBytes()));
session.connect();