我有一种情况,出现另一个弹出式密码,这意味着我需要在密码后输入另一个文本,并且我还需要以编程方式处理它。
下面的代码适用于密码
((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; echo demouser | pbrun democommand");
echo 确实可以让我输入密码。但就在它之后,我需要像密码一样输入文本,但我无法这样做。所以我用管道放了另一个回声,但它不起作用。
我用于相同的代码
((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; echo Automation | echo demouser | pbrun democommand");
我也尝试了下面的参考并编写了如下命令,仍然没有运气
((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; echo Automation | { echo demopass; } | pbrun democommand");
参考截图:
我正在使用的代码:
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);
}
}
任何解决方法都会有所帮助