java ssh 远程执行_Java SSH远程执行Shell命令、shell脚本实现(Ganymed SSH)

packagecom.system.action;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.web.bind.annotation.RequestMapping;importcom.system.service.MonitorService;importch.ethz.ssh2.Connection;importch.ethz.ssh2.Session;importch.ethz.ssh2.StreamGobbler;importutil.dataSource.SwitchDataSourceUtil;importutil.page.BaseAction;/*** 服务器监控

*@authorwangxiangyu

**/@Controller

@RequestMapping("/monitor")

@SuppressWarnings("all")public class MonitorController extendsBaseAction {private static List commands = null;private static final String JNDI = "pn";//爱运维管理员权限数据源

@Autowired

MonitorService monitorService;/*** 进入监控页面,获取服务器配置(ip,登录名,密码)

*@paramrequest

*@parammodel

*@return

*/@RequestMapping(value= "/index.do")publicString index(HttpServletRequest request, Model model){

String hostName= request.getParameter("hostName");

List> serverConfigList =monitorService.getServerConfig(hostName);

model.addAttribute("serverConfigList", serverConfigList);return "system/monitor/monitor";

}/*** 初始化命令集合*/

private voidinitCommands() {

commands= new ArrayList();

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");

}/*** 获取服务器运行状态

*@paramrequest

*@paramresponse

*@throwsException*/@RequestMapping("/findStatus.do")public void findStatus(HttpServletRequest request, HttpServletResponse response) throwsException {//获取服务器配置信息

String hostName = request.getParameter("hostName");

List> serverConfigList =monitorService.getServerConfig(hostName);

String userName= serverConfigList.get(0).get("USER_NAME").toString();

String password= serverConfigList.get(0).get("PASSWORD").toString();//预定义结果集

Map result = new HashMap();//预定义命令结果集

List> commandResults = new ArrayList>();try{//连接服务器

Connection conn = newConnection(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= newStreamGobbler(sess.getStdout());

BufferedReader br= new BufferedReader(newInputStreamReader(stdout));

StringBuffer details= new StringBuffer("");while (true){

String line=br.readLine();if (line == null) {break;

}

details.append(line).append("
");

System.out.println(details);

}//封装结果

Map commandResult = new HashMap();

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);

}/*** 查看数据库表空间状态

*@paramrequest

*@paramresponse

*@throwsIOException*/@RequestMapping("/getDBStatus.do")public void getDBStatus(HttpServletRequest request, HttpServletResponse response) throwsIOException {

List> dbResult = new ArrayList>();try{//切换数据源

SwitchDataSourceUtil.setCurrentDataSource(JNDI);

dbResult=monitorService.getDBStatus();

SwitchDataSourceUtil.clearDataSource();

}catch(Exception e) {

e.printStackTrace();

}finally{

SwitchDataSourceUtil.clearDataSource();

}

Map result = new HashMap();

result.put("rows", dbResult);

write(response, result);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值