I am trying to execute a remote query over SSH using pubic key/private key based authentication. Following command works fine and gives me the required output as a string on a bash shell after sharing the public keys between local host and the remote server.
echo 123456 12@13:14 ABCD abc1234 | ssh -T user@abc.xyz.com
How do I achieve the same with JAVA using JSCH or SSHJ or any other similar library
This is what I have tried so far using SSHJ but it did not work for me (Connection was successful but no results)
public static void main(String... args)throws IOException {
final SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("abc.xyz.com");
try {
ssh.authPublickey("user");
final Session session = ssh.startSession();
try {
final Command cmd = session.exec("123456 12@13:14 ABCD abc1234");
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();
ssh.close();
}
}