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

0

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.