RemoteShellController.java 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package com.yihu.hos.remoteManage.controller;
  2. import com.yihu.hos.common.constants.ContextAttributes;
  3. import com.yihu.hos.interceptor.LocalContext;
  4. import com.yihu.hos.remoteManage.service.RemoteShellService;
  5. import com.yihu.hos.web.framework.model.Result;
  6. import com.yihu.hos.web.framework.util.controller.BaseController;
  7. import io.swagger.annotations.ApiParam;
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.ui.Model;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestMethod;
  12. import org.springframework.web.bind.annotation.RequestParam;
  13. import org.springframework.web.bind.annotation.ResponseBody;
  14. import javax.annotation.Resource;
  15. /**
  16. * 远程终端管理--shell操作
  17. *
  18. * @author HZY
  19. * @vsrsion 1.0
  20. * Created at 2017/1/5.
  21. */
  22. @RequestMapping("/shell")
  23. @Controller
  24. public class RemoteShellController extends BaseController {
  25. @Resource(name = RemoteShellService.BEAN_ID)
  26. private RemoteShellService remoteShellService;
  27. /**
  28. * 远程shell操作页面
  29. *
  30. * @param model
  31. * @return
  32. */
  33. @RequestMapping("/initial")
  34. public String appInitial(Model model) {
  35. model.addAttribute("contentPage", "/monitor/shell/shell");
  36. return "partView";
  37. }
  38. @RequestMapping(value = "/sendShell", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
  39. @ResponseBody
  40. public Result sendShell(
  41. @ApiParam(name = "command", value = "shell命令")
  42. @RequestParam(value = "command", required = false) String command,
  43. @ApiParam(name = "disCon", value = "是否断开会话连接")
  44. @RequestParam(value = "disCon", required = true) boolean disCon) {
  45. String result = "";
  46. try {
  47. //TODO 发送shell命令 消息
  48. System.out.println("发送shell请求:" + command);
  49. remoteShellService.sendShell(command, disCon);
  50. return Result.success("shell请求发送成功!");
  51. } catch (Exception e) {
  52. e.printStackTrace();
  53. }
  54. return Result.error("shell请求发送失败!");
  55. }
  56. @RequestMapping(value = "/getShellBack", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
  57. @ResponseBody
  58. public String getShellBack() {
  59. //TODO 切换用户的时候记得清除该值
  60. String result = "";
  61. try {
  62. //TODO 如何去除等待时间,目前添加sleep是因为需要等待zbus回调方法的返回结果;
  63. Thread.sleep(2000);
  64. String attachment = LocalContext.getContext().getAttachment(ContextAttributes.TENANT_NAME);
  65. result = LocalContext.getContext().getAttachment(ContextAttributes.SHELL_RESPONSE + attachment);
  66. int count = 0;
  67. while (result == null) {
  68. count++;
  69. Thread.sleep(2000);
  70. result = LocalContext.getContext().getAttachment(ContextAttributes.SHELL_RESPONSE + attachment);
  71. //获取失败时,尝试再一次获取结果
  72. if (count > 2) {
  73. break;
  74. }
  75. }
  76. System.out.println("接口返回结果:" + result);
  77. } catch (InterruptedException e) {
  78. e.printStackTrace();
  79. }
  80. return result;
  81. }
  82. }