uttamgupta commented on issue #658:
URL: https://github.com/apache/mina-sshd/issues/658#issuecomment-2637760010

   Thanks for helping. Code below is working now.
   
   public class InteractiveExecCmd {
       public static void main(String[] args) throws Exception {
   
           SshClient client = SshClient.setUpDefaultClient();
           client.start();
           String hostname = "<hostIP>";
           String password = "<pwd>";
           ConnectFuture cf = client.connect("root", hostname, 22);
           ClientSession clientSession = cf.verify().getSession();
           clientSession.addPasswordIdentity(password);
           clientSession.auth().verify(TimeUnit.SECONDS.toMillis(10));
           ChannelExec ce;
   
           try {
               ce = clientSession.createExecChannel("bc");
               OpenFuture openFuture = ce.open().verify(30, TimeUnit.SECONDS);
               Stack<String> cmds = new Stack<>();
               cmds.add("`");
               cmds.add("723+323");
               cmds.add("734334*23343434");
               // Create a new thread to handle input
               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);
                       }
                       stdin.write("quit\n".getBytes()); // Exit the bc session 
when done
                       stdin.flush();
                   } catch (IOException e) {
                       throw new RuntimeException(e);
                   }
               });
               t1.start();
               // Capture and print the output from bc
               InputStream outputStream = ce.getInvertedOut();
               InputStream errorStream = ce.getInvertedErr();
               BufferedReader reader = new BufferedReader(new 
InputStreamReader(outputStream));
               String line;
               if (outputStream != null) {
                   // Read output from bc
   
                   while ((line = reader.readLine()) != null) {
                       System.out.println("bc output: " + line);
                   }
               }
               if (errorStream != null) {
                   BufferedReader errorReader = new BufferedReader(new 
InputStreamReader(errorStream));
                   // Read error from bc
                   while ((line = errorReader.readLine()) != null) {
                       System.out.println("bc error: " + line);
                   }
               }
               Set<ClientChannelEvent> events =
                       ce.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), 
TimeUnit.SECONDS.toMillis(2));
               t1.join();
           } 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: 734334*23343434
   Sent: 723+323
   Sent: `
   bc output: 17141877262956
   bc output: 1046
   bc error: (standard_in) 3: illegal character: `
   
   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

Reply via email to