標籤:運行 puts val stream 管理員 example ppi ica vat
jar包:
http://www.ganymed.ethz.ch/ssh2/
此源碼的好處就是沒有依賴很多其他的包,拷貝過來乾乾淨淨。具體代碼實現可以看下文,或參考官方文檔,在下載的壓縮包裡ganymed-ssh2-build210\examples目錄下有樣本。
package com.system.action; import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import com.system.service.MonitorService;import ch.ethz.ssh2.Connection;import ch.ethz.ssh2.Session;import ch.ethz.ssh2.StreamGobbler;import util.dataSource.SwitchDataSourceUtil;import util.page.BaseAction; /** * 伺服器監控 * @author wangxiangyu * */@Controller@RequestMapping("/monitor")@SuppressWarnings("all")public class MonitorController extends BaseAction { private static List<String> commands = null;private static final String JNDI = "pn";//愛營運管理員權限資料來源 @AutowiredMonitorService monitorService;/** * 進入監控頁面,擷取伺服器配置(ip,登入名稱,密碼) * @param request * @param model * @return */@RequestMapping(value = "/index.do")public String index(HttpServletRequest request, Model model){String hostName = request.getParameter("hostName"); List<Map<String, String>> serverConfigList = monitorService.getServerConfig(hostName);model.addAttribute("serverConfigList", serverConfigList);return "system/monitor/monitor";} /** * 初始化命令集合 */private void initCommands() { commands = new ArrayList<String>();commands.add("top");commands.add("free");commands.add("df -hl");commands.add("ps -ef|grep tomcat");commands.add("ps aux|head -1;ps aux|grep -v PID|sort -rn -k +4|head");} /** * 擷取伺服器運行狀態 * @param request * @param response * @throws Exception */@RequestMapping("/findStatus.do")public void findStatus(HttpServletRequest request, HttpServletResponse response) throws Exception {//擷取伺服器配置資訊String hostName = request.getParameter("hostName");List<Map<String, String>> serverConfigList = monitorService.getServerConfig(hostName);String userName = serverConfigList.get(0).get("USER_NAME").toString();String password = serverConfigList.get(0).get("PASSWORD").toString();//預定義結果集Map<String, Object> result = new HashMap<String, Object>();//預定義命令結果集List<Map<String, String>> commandResults = new ArrayList<Map<String, String>>();try {//串連伺服器Connection conn = new Connection(hostName);conn.connect();boolean isAuthenticated = conn.authenticateWithPassword(userName, password);if (isAuthenticated == false) {throw new IOException("Authentication failed.");}//初始化命令參數initCommands();for(String command : commands) {Session sess = conn.openSession();sess.execCommand(command);InputStream stdout = new StreamGobbler(sess.getStdout());BufferedReader br = new BufferedReader(new InputStreamReader(stdout));StringBuffer details = new StringBuffer("");while (true){String line = br.readLine();if (line == null) {break;}details.append(line).append("<br/>");System.out.println(details);}//封裝結果Map<String, String> commandResult = new HashMap<String, String>();commandResult.put("hostName", hostName);commandResult.put("command", command);commandResult.put("exitCode", null==sess.getExitStatus()?"無":sess.getExitStatus().toString());commandResult.put("details", (null==details)?"無返回結果":details.toString());commandResults.add(commandResult);//關閉流br.close();sess.close();}conn.close();result.put("rows", commandResults);}catch(IOException e) {e.printStackTrace(System.err);}write(response, result);} /** * 查看資料庫資料表空間狀態 * @param request * @param response * @throws IOException */@RequestMapping("/getDBStatus.do")public void getDBStatus(HttpServletRequest request, HttpServletResponse response) throws IOException {List<Map<String, String>> dbResult = new ArrayList<Map<String,String>>();try {//切換資料來源SwitchDataSourceUtil.setCurrentDataSource(JNDI);dbResult = monitorService.getDBStatus();SwitchDataSourceUtil.clearDataSource();}catch(Exception e) {e.printStackTrace();}finally {SwitchDataSourceUtil.clearDataSource();}Map<String, Object> result = new HashMap<String, Object>();result.put("rows", dbResult);write(response, result);}}
Java SSH遠程執行Shell命令、shell指令碼實現(Ganymed SSH)