用JAVA的check.ethz.ssh2包写了个连接Linux的程序,
我是用getSession来执行命令,可是一段时间后再调用getSession就会报错,the connection is being shutdown,
怎样保持Connection一直连接的,或者判断是否还连接着的,判断超时方法,这些有吗?
JAVA ethz ssh2 Connection连接超时
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
歇歇 2022-06-08 01:49关注将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(); }解决评论 打赏 举报无用 3