How to echo a string to SSH using JAVA
<p>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. </p>

<pre><code>echo 123456 12@13:14 ABCD abc1234 | ssh -T user@abc.xyz.com
</code></pre>

<p>How do I achieve the same with JAVA using JSCH or SSHJ or any other similar library </p>

<p>This is what I have tried so far using SSHJ but it did not work for me (Connection was successful but no results)</p>

<pre><code>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();
    }
}
</code></pre>