问题1:我目前使用sshj通过SSH进行一些远程控制,它工作得很好,但我似乎无法让它正确处理提示(主机不提供真正的root,只提供sudo-i,所以我需要先登录)。
问题2:我下载了ExpectJ来处理提示,但是我一辈子都不知道如何在以root身份登录和验证之后维护会话。
当前的黑客解决方案要求我每次重新登录:
public class Expect {
Spawn shell;
ExpectJ exp;
String host;
int port;
String username;
String passwd;
boolean sudo = false;
public Expect(String host,int port,String username,String passwd) throws IOException, TimeoutException, ExpectJException{
exp = new ExpectJ(5);
this.host = host;
this.port = port;
this.username = username;
this.passwd = passwd;
shell = exp.spawn(host, port, username, passwd);
shell.send("sudo netstat -natvp | grep Xtightvnc\n");
System.out.println(shell.getCurrentStandardOutContents());
try{
shell.expect("[sudo] password for #######:");
shell.send(passwd+"\n");
}
catch (IOException ex){
String err = ex.toString();
if(!err.equals("java.io.IOException: End of stream reached, no match found")){
throw new IOException(ex);
}
}
}
问题1:sshj能否用于“预期”密码提示?我找不到任何暗示这种控制的文件。