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

   Thanks for helping me. stdin is not null this time, I was doing some 
mistakes and still doing some mistake. Here is the 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("2*2");
               cmds.add("2+2");
               OutputStream stdin = ce.getInvertedIn();
               Thread t1 = new Thread(() -> {
                   // Code to be executed in the new thread
                   while (!cmds.empty()) {
                           try {
                               Thread.sleep(1000);
                           } catch (InterruptedException e) {
                               throw new RuntimeException(e);
                           }
                       try {
                           stdin.write(cmds.pop().getBytes());
                           System.out.println("Out1 : " + 
ce.getOut().toString());
                           System.out.println("Out2 " + stdin.toString());
                           System.out.println("Out3 " + ce.getOut().toString());
                       } catch (IOException e) {
                           throw new RuntimeException(e);
                       }
                   }
               });
               t1.start();
               t1.join();
               Set<ClientChannelEvent> events =
                       ce.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), 
TimeUnit.SECONDS.toMillis(30));
   
               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: 
   Out1 : org.apache.sshd.common.channel.ChannelPipedOutputStream@26d394cf
   Out2 ChannelOutputStream[ChannelExec[id=0, 
recipient=0]-ClientSessionImpl[root@/<hostIp>:22]] SSH_MSG_CHANNEL_DATA
   Out3 org.apache.sshd.common.channel.ChannelPipedOutputStream@26d394cf
   Out1 : org.apache.sshd.common.channel.ChannelPipedOutputStream@26d394cf
   Out2 ChannelOutputStream[ChannelExec[id=0, 
recipient=0]-ClientSessionImpl[root@/<hostIp>:22]] SSH_MSG_CHANNEL_DATA
   Out3 org.apache.sshd.common.channel.ChannelPipedOutputStream@26d394cf
   Final Out: org.apache.sshd.common.channel.ChannelPipedOutputStream@26d394cf
   Disconnected from the target VM, address: '127.0.0.1:57678', 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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to