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 sudo
ed command
((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; echo Automation | { echo demopass; } | pbrun democommand");
Reference screenshot:
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