uttamgupta commented on issue #658: URL: https://github.com/apache/mina-sshd/issues/658#issuecomment-2635347384
Thanks for helping. I found my mistake and it worked - Corrected code - public class InteractiveExecCmd { public static void main(String[] args) throws Exception { SshClient client = SshClient.setUpDefaultClient(); client.start(); String hostname = "<hostIp>"; String password = "<password>"; ConnectFuture cf = client.connect("root", hostname, 22); ClientSession clientSession = cf.verify().getSession(); clientSession.addPasswordIdentity(password); clientSession.auth().verify(TimeUnit.SECONDS.toMillis(30)); ChannelExec ce; try { ce = clientSession.createExecChannel("bc"); ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream(); // ByteArrayInputStream inputStream = new ByteArrayInputStream(GenericUtils.EMPTY_BYTE_ARRAY); ce.setOut(out); ce.setErr(err); //ce.setIn(inputStream); OpenFuture openFuture = ce.open().verify(30, TimeUnit.SECONDS); Stack<String> cmds = new Stack<>(); cmds.add("4*2"); cmds.add("7+2"); cmds.add("7*2"); OutputStream stdin = ce.getInvertedIn(); Thread t1 = new Thread(() -> { // Code to be executed in the new thread try { while (!cmds.empty()) { String cmd = cmds.pop(); stdin.write((cmd + "\n").getBytes()); stdin.flush(); // Flush the input to ensure it's sent System.out.println("Sent: " + cmd); System.out.println("Out1 : " + ce.getOut().toString()); Thread.sleep(500); //stdin.write("exit\n".getBytes()); // Exit the bc session when done // stdin.flush(); //System.out.println("Out2 " + stdin); } } catch (IOException e) { throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } }); t1.start(); t1.join(); Set<ClientChannelEvent> events = ce.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), TimeUnit.SECONDS.toMillis(10)); System.out.println("Final Out: " + ce.getOut().toString()); } catch (IOException e) { throw new SshClientException("Failed to execute command", e); } finally { try { clientSession.close(); } catch (IOException e) { System.out.println("Got exception while closing session"); e.printStackTrace(); } } } } Output: Sent: 7*2 Out1 : 14 Sent: 7+2 Out1 : 14 9 Sent: 4*2 Out1 : 14 9 8 Final Out: 14 9 8 Disconnected from the target VM, address: '127.0.0.1:60926', transport: 'socket' Process finished with exit code 0 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@mina.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@mina.apache.org For additional commands, e-mail: dev-h...@mina.apache.org