|
@ -1,6 +1,9 @@
|
|
|
package com.yihu.hos.remoteManage.controller;
|
|
|
|
|
|
import com.yihu.hos.common.constants.ContextAttributes;
|
|
|
import com.yihu.hos.interceptor.LocalContext;
|
|
|
import com.yihu.hos.remoteManage.service.RemoteShellService;
|
|
|
import com.yihu.hos.web.framework.model.Result;
|
|
|
import com.yihu.hos.web.framework.util.controller.BaseController;
|
|
|
import io.swagger.annotations.ApiParam;
|
|
|
import org.springframework.stereotype.Controller;
|
|
@ -35,63 +38,57 @@ public class RemoteShellController extends BaseController {
|
|
|
*/
|
|
|
@RequestMapping("/initial")
|
|
|
public String appInitial(Model model) {
|
|
|
model.addAttribute("contentPage", "shell/shell");
|
|
|
model.addAttribute("contentPage", "/monitor/shell/shell");
|
|
|
return "partView";
|
|
|
}
|
|
|
|
|
|
@RequestMapping(value = "/shell", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
|
|
|
@RequestMapping(value = "/sendShell", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
|
|
|
@ResponseBody
|
|
|
public String shellTest(
|
|
|
public Result sendShell(
|
|
|
@ApiParam(name = "command", value = "shell命令")
|
|
|
@RequestParam(value = "command", required = false) String command,
|
|
|
// @ApiParam(name = "tenant", value = "租户名称")
|
|
|
// @RequestParam(value = "tenant", required = false) String tenant,
|
|
|
@ApiParam(name = "disCon", value = "是否断开会话连接")
|
|
|
@RequestParam(value = "disCon", required = true) boolean disCon) {
|
|
|
String result = "";
|
|
|
try {
|
|
|
//TODO 发送shell命令 消息
|
|
|
System.out.println("发送shell请求==================");
|
|
|
System.out.println("发送shell请求:" + command);
|
|
|
remoteShellService.sendShell(command, disCon);
|
|
|
System.out.println("结果1:" + result);
|
|
|
//TODO 接收shell命令执行结果 消息
|
|
|
remoteShellService.start();
|
|
|
result=remoteShellService.backResult;
|
|
|
System.out.println("接口返回结果:"+result);
|
|
|
|
|
|
return Result.success("shell请求发送成功!");
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return result;
|
|
|
return Result.error("shell请求发送失败!");
|
|
|
}
|
|
|
|
|
|
//
|
|
|
// //连接操作shell测试
|
|
|
// @RequestMapping(value = "/shell", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
|
|
|
// @ResponseBody
|
|
|
// public String shellTest(String cmd, String disCon) {
|
|
|
// String result = "";
|
|
|
// try {
|
|
|
// if (session==null){
|
|
|
// session = sshLinuxTool.getsessionConn();
|
|
|
// }
|
|
|
// if ("false".equals(disCon)) {
|
|
|
// //保持通道连接
|
|
|
// System.out.println("循环开始1111111==================");
|
|
|
// result = sshLinuxTool.sshShell(session, cmd, false);
|
|
|
// System.out.println("结果:"+result);
|
|
|
// } else {
|
|
|
// //断开通道连接,会话
|
|
|
// System.out.println("循环开始2222222222==================");
|
|
|
// result = sshLinuxTool.sshShell(session, cmd, true);
|
|
|
// session = null;
|
|
|
// System.out.println("结果:"+result);
|
|
|
// }
|
|
|
// } catch (Exception e) {
|
|
|
// e.printStackTrace();
|
|
|
// }
|
|
|
// return result;
|
|
|
// }
|
|
|
|
|
|
@RequestMapping(value = "/getShellBack", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
|
|
|
@ResponseBody
|
|
|
public String getShellBack() {
|
|
|
//TODO 切换用户的时候记得清除该值
|
|
|
String result = "";
|
|
|
try {
|
|
|
//TODO 如何去除等待时间,目前添加sleep是因为需要等待zbus回调方法的返回结果;
|
|
|
Thread.sleep(2000);
|
|
|
String attachment = LocalContext.getContext().getAttachment(ContextAttributes.TENANT_NAME);
|
|
|
result = LocalContext.getContext().getAttachment(ContextAttributes.SHELL_RESPONSE + attachment);
|
|
|
int count = 0;
|
|
|
while (result == null) {
|
|
|
count++;
|
|
|
Thread.sleep(2000);
|
|
|
result = LocalContext.getContext().getAttachment(ContextAttributes.SHELL_RESPONSE + attachment);
|
|
|
//获取失败时,尝试再一次获取结果
|
|
|
if (count > 2) {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
System.out.println("接口返回结果:" + result);
|
|
|
} catch (InterruptedException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|