package com.sunfan.monitor.platform.linux; import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import com.sunfan.monitor.platform.IMonitorable; import com.trilead.ssh2.Connection; import com.trilead.ssh2.Session; import com.trilead.ssh2.StreamGobbler; /** * * @author sunfan * */ @Component public class LinuxSessionHandle implements IMonitorable,Closeable{ Logger logger = LoggerFactory.getLogger(LinuxSessionHandle.class); private Session session; /** * open session then execute commands on remote server and return the result of it * @param command * @return String * @throws IOException */ public synchronized String executeCommand(Connection conn,String command) throws IOException { String str=""; try { session = conn.openSession(); session.execCommand(command); str = this.read().toString(); } catch (Exception e) { session.ping(); throw new IOException("session exception",e); } finally{ close(); } return str; } /** * read the result of remote server execute commands * @return * @throws IOException */ private StringBuffer read() throws IOException{ InputStream stdout = new StreamGobbler(session.getStdout()); BufferedReader br = new BufferedReader(new InputStreamReader(stdout)); String tempString = null; // readLine()每次调用都默认会读一行 StringBuffer str = new StringBuffer(); while ((tempString = br.readLine()) != null) { str.append(tempString+"\r\n"); } br.close(); return str; } /** * close session */ @Override public void close() throws IOException { if (this.session != null) this.session.close(); } } 5 EntityBaseUtil package com.sunfan.monitor.entity.util; import java.util.ArrayList; import java.util.List; /** * * @author sunfan * */ public class EntityBaseUtil { /**change String result to List<String> result by split "\r\n" * remove the content above flag * to transfer List<String> ---> list<String[]> by .split("\\s{1,}") * @param result * @param flag * @return */ public List<String[]> transferListofStringArray(String result,String flag){ List<String> resList = this.transferList(result); List<String> contentList = this.removeResultHead(resList,flag); return this.transferArrayOfList(contentList); } /** * change String result to List<String> result by split "\r\n" * @param result * @return List<String> */ public List<String> transferList(String result){ String[] strs = result.split("\r\n"); List<String> list = new ArrayList<String>(); for(String s:strs){ list.add(s); } return list; } /**remove the content above flag * * @return List<String> */ public List<String> removeResultHead(List<String> resultList,String flag){ List<String> contentList = new ArrayList<String>(); contentList.addAll(resultList); for(String res:resultList){ if(res.contains(flag)){ break; } contentList.remove(res); } return contentList; } /**to transfer List<String> ---> list<String[]> by .split("\\s{1,}") * * @param contentList * @return List<String[]> */ public List<String[]> transferArrayOfList(List<String> contentList){ List<String[]> contentLists =new ArrayList<>(); for(String content:contentList){ contentLists.add(content.split("\\s{1,}")); } return contentLists; } /**get result of reference by title * * @param head data of reference title * @param title reference title * @param infos data of each reference * @return String result of reference by title ,if title is not matched ,return " " */ public String resolveValueByTagName(List<String> head,String[] infos,String title){ if(head.indexOf(title)>0){ return infos[head.indexOf(title)]; } return ""; } } |