1

I have a situation where a another pop like password is appearing, Means I need to enter another text after password and I also need to handle it programmatically.

below code is working for password

((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; echo demouser | pbrun democommand");

echo does work for me to enter the password. But just after it I need to enter text just like password and I am not able to do so. so I put an another echo with pipe, but it is not working.

Code I am using for same

((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; echo Automation |  echo demouser | pbrun democommand");

I also tried below refernce and wrote the command as below, still no luck

pipe password to sudo and other data to sudoed command

((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; echo Automation |  { echo demopass; } | pbrun democommand");

Reference screenshot:

enter image description here

enter image description here

Code I am using:

try {
            JSch jsch = new JSch();
            Session session = jsch.getSession(user, host, 22);
            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);;
            session.setPassword(password);
            System.out.println("user=="+user+"\n host=="+host);
            session.connect();
            System.out.println("connected to host ===="+host);
            String sudo_pass="demopassword";

        Channel channel=session.openChannel("exec");


        System.out.println("cd command");
        ((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; ( echo demopassword && echo Automation )  | pbrun democommand");
        ((ChannelExec) channel).setPty(true);

        InputStream in=channel.getInputStream();
        OutputStream out=channel.getOutputStream();
        ((ChannelExec)channel).setErrStream(System.err);

        channel.connect();

        out.write((sudo_pass+"\n").getBytes());

        out.flush();

        byte[] tmp=new byte[1024];
        while(true){
          while(in.available()>0){
            int i=in.read(tmp, 0, 1024);
            if(i<0)break;
            System.out.print(new String(tmp, 0, i));
          }
          if(channel.isClosed()){
            System.out.println("exit-status: "+channel.getExitStatus());
            break;
          }
          try{Thread.sleep(1000);}catch(Exception ee){}
        }

        channel.disconnect();
        session.disconnect();
      }
      catch(Exception e){
        System.out.println(e);
      }
}

Any workaround will be helpful

Shubham Jain
  • 14,804
  • 9
  • 73
  • 115

2 Answers2

1

Bash syntax for providing two lines of an input to a command is:

( echo input1 && echo input2 ) | command

See also Pipe multiple commands into a single command.


You can also provide the input in your Java code rather than using shell constructs:
Providing input/subcommands to command executed over SSH with JSch

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846
  • Thanks Martin for your reply. Your answer has put the password but the another pop is waiting for the parameter , means the program is not terminating and waiting for something. do i need to press enter programitacally – Shubham Jain May 29 '18 at 10:35
  • I have posted the code along with this Question. please review and let me know if I doing anything wrong or missing anything.. it will really help – Shubham Jain May 29 '18 at 10:36
  • Sorry, I do not understand what you mean by *"Your answer has but the password but the another pop is waiting for the paramenter"* - My answer show how to pass two lines (password and some other [parameter?]). – Martin Prikryl May 29 '18 at 10:37
  • I also do not understand how your `out.write((sudo_pass+"\n").getBytes());` goes with the rest. Is that yet another password? You cannot have two separate input streams. – Martin Prikryl May 29 '18 at 10:38
  • cd ~demouser/bin;ls; ( echo demopassword && echo Automation ) | pbrun democommand") command succesfully enter the password but on eclipse the next pop-up still waiting to enter the input2. either is not entering the input2 – Shubham Jain May 29 '18 at 10:40
  • Oh do I need to delete whole Inputstream till out.flush(). because I am entering password through terminal. i am new to this library so parden me if I said something wrong – Shubham Jain May 29 '18 at 10:42
  • You better first try everything in terminal. Only once you make everything working, try the same in Java. – Martin Prikryl May 29 '18 at 10:43
  • `demopassword` and `Automation` are inputs to `pbrun` or to `democommand`? – Martin Prikryl May 29 '18 at 10:44
  • terminal everthing works fine. Just making the java code is making issue. demopassword is a password of pbrun command .. pbrun asking another pop i.e (Enter reason for this privilege access:), which need some text – Shubham Jain May 29 '18 at 10:47
  • *"terminal everthing works fine"* - so in terminal `( echo demopassword && echo Automation ) | pbrun democommand` works? – Martin Prikryl May 29 '18 at 10:51
  • no .. means doing it by manual step is working.. ( echo demopassword && echo Automation ) | pbrun democommand is not entering even password in terminal. while password is entered when execute using code – Shubham Jain May 29 '18 at 10:57
  • command is just a username ... I have to run "pbrun username" which require a password and text to be enter first – Shubham Jain May 29 '18 at 11:02
  • Did you try removing the `setPty(true);`? Also try to remove spaces from the command: `(echo demopassword&&echo Automation) | pbrun democommand` – Martin Prikryl May 29 '18 at 11:32
  • Martin thanks for your valuable time... voting you up .. I have tried a code which works for me .. I still not get the flow that why it works now.. please have a look and let me know if you understand that why now it is working like that... it will help – Shubham Jain May 30 '18 at 08:14
  • yes you were right .. marking as accepted .. just now facing issue as jsch is showing permission denied for the commands. I am putting commands like ls in place of input 2 but , it is taking folder and file name as command – Shubham Jain May 31 '18 at 09:01
  • If you have a new problem, please post a separate question. – Martin Prikryl May 31 '18 at 09:04
  • issue resolved .. actually I was using commnad without echo .. echo 'commond' was required.. Thanks Martin – Shubham Jain May 31 '18 at 13:52
1

Thanks to Martin below approach works for me

( echo 'command1' && echo 'command2' ) | command

Below code works for me

try {
        JSch jsch = new JSch();
       Session session = jsch.getSession(user, host, 22);
       Properties config = new Properties();
       config.put("StrictHostKeyChecking", "no");
       session.setConfig(config);;
       session.setPassword(password);
       System.out.println("user=="+user+"\n host=="+host);
       session.connect();
       System.out.println("connected to host ===="+host);
       String sudo_pass="demo123";

   Channel channel=session.openChannel("exec");


   System.out.println("cd command");

   ((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; ( echo 'echo Automation' && echo 'command' )  | pbrun democommand");
   ((ChannelExec) channel).setPty(true);

   InputStream in=channel.getInputStream();
   OutputStream out=channel.getOutputStream();
   ((ChannelExec)channel).setErrStream(System.err);

   channel.connect();

   out.write((sudo_pass+"\n").getBytes());
  // out.write(("\n").getBytes());

   out.flush();

   byte[] tmp=new byte[102400];
   while(true){
     while(in.available()>0){
       int i=in.read(tmp, 0, 102400);
       if(i<0)break;
       System.out.print(new String(tmp, 0, i));
     }
     if(channel.isClosed()){
       System.out.println("exit-status: "+channel.getExitStatus());
       break;
     }
     try{Thread.sleep(1000);}catch(Exception ee){
         ee.printStackTrace();
         System.out.println(ee.getMessage());
     }
   }
   channel.disconnect();
   session.disconnect();
 }
 catch(Exception e){
   System.out.println(e);
 }
}

Dependency Used:

    <dependency>
        <groupId>com.jcraft</groupId>
        <artifactId>jsch</artifactId>
        <version>0.1.50</version>
    </dependency>
Shubham Jain
  • 14,804
  • 9
  • 73
  • 115