用JAVA的check.ethz.ssh2包写了个连接Linux的程序,
我是用getSession来执行命令,可是一段时间后再调用getSession就会报错,the connection is being shutdown,
怎样保持Connection一直连接的,或者判断是否还连接着的,判断超时方法,这些有吗?
将root用户重新重命名回root或者代码判断
public String exec(String cmd,String strSplit) {
InputStream stdout = null;
StringBuffer sb = new StringBuffer();
Session sess = null;
try {
// System.out.println(this.login());
// if (this.login()) {
if(conn != null && conn.isAuthenticationComplete()){
sess = conn.openSession();
sess.execCommand(cmd);
stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while (true)
{
String line = br.readLine();
if (line == null)
break;
// System.out.println(line);
sb.append(line);
if(strSplit!=null&&!"".equals(strSplit))
sb.append(strSplit);
}
sess.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);
logger.warn(conn+"\t"+sess+"\tExitCode: " +sess.getExitStatus());
}
// }
} catch (Exception e) {
// e.printStackTrace();
logger.error(ipAddr+"\t"+cmd);
logger.error(ExceptionUtil.getExceptionMessage(e));
System.exit(2);
} finally{
if(null != sess){
sess.close();
sess = null;
logger.warn("session关闭");
}
}
return sb.toString();
}
session会过期。。应该有相关配置。多找找资料吧