package com.haiyisoft.cAssistant.adapter.rest;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.haiyisoft.cAssistant.common.StateEnum;
import com.haiyisoft.cAssistant.utils.JsonReturn;
import com.haiyisoft.cAssistant.utils.ShellUtil;
import com.haiyisoft.cAssistant.vo.ReturnValueVo;
//http://172.20.188.157:8011/cAssistant/getshell/exec.do?scriptPath=/data/app/cassistant/startbatch.sh&para=start,cAssistantrightweb

public class ShellController {

public static String execute(String scriptPath,String para){

try {
String[] params= para.split(",");
if(params.length>0){
for(int i=0;i<params.length;i++){
scriptPath+=" "+params[i];
}
}
String[] cmd = new String[]{"/bin/sh","-c",scriptPath};
//解决脚本没有执行权限
ProcessBuilder builder = new ProcessBuilder("/bin/chmod", "777",scriptPath);
Process process = builder.start();
process.waitFor();

Process ps = Runtime.getRuntime().exec(cmd);
ps.waitFor();

BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
//执行结果
String result = sb.toString();
System.out.println(result);
return result;
} catch (Exception e) {
e.printStackTrace();
return " ";

}

}
//http://172.20.188.157:8011/cAssistant/getshell/mkdir.do?bakpath=/data/app/zzq&filepath=/data/app/filepath
@RequestMapping("/mkdir")
@ResponseBody
public ReturnValueVo mkdir(@RequestParam Map<String, String> map)
throws Exception {
String scriptPath=map.get("bakpath");
String filepath=map.get("filepath");
File file = new File(scriptPath);
if (!file.exists()) {//如果文件不存在
file.mkdir();
}
else{
String command3 = "rm -rf "+scriptPath;
String message3 = ShellUtil.runShell(command3);
file.mkdir();
System.out.println(message3);
}
ReturnValueVo result;
try {
/* String command1 = "chown zgc "+scriptPath;
String message1 = ShellUtil.runShell(command1);
System.out.println(message1);
String command2 = "chmod -R 777 "+scriptPath;
String message2 = ShellUtil.runShell(command2);
System.out.println(message2);
*/
String command3 = " cp -pr "+filepath +" "+scriptPath;
String message3 = ShellUtil.runShell(command3);
System.out.println(message3);
} catch (Exception e) {
// TODO: handle exception
result = JsonReturn.assemblyBean(null, StateEnum.FAIL.getStatus(),StateEnum.FAIL.getMsg());
return result;
}
result = JsonReturn.assemblyBean(null, StateEnum.SUCCESS.getStatus(),StateEnum.SUCCESS.getMsg());
return result;

}
}

最新文章

  1. Ruby之基本数据类型(三)
  2. CPU host-passthrough技术与应用
  3. ArcGIS API for Silverlight 之ElementLayer使用及TextSymbol的模板使用
  4. Android Non-UI to UI Thread Communications(Part 2 of 5)
  5. js基础例子dom+原型+oop基础知识记录01
  6. java Process在windows的使用汇总(转)
  7. 2015 QQ最新登录算法
  8. 基础数据结构-串-KMP算法
  9. 从Android源码的角度分析Binder机制
  10. 【JavaScript】设计模式-module模式及其改进
  11. c++(线性结构的处理)
  12. VS2013 重装 无法打开项目
  13. Python知识目录
  14. PhoneGap和Cordova应该用哪一个?
  15. 删除单链表节点,时间复杂度为O(1)
  16. [JLOI2014]聪明的燕姿(搜索)
  17. 使用cross-env解决跨平台设置NODE_ENV的问题
  18. Spring Boot集成JPA的Column注解命名字段无效的问题
  19. Linux网桥配置
  20. 免费json API

热门文章

  1. Java高并发程序设计学习笔记(六):JDK并发包(线程池的基本使用、ForkJoin)
  2. php.ini配置文件参数中文说明文档
  3. 性能测试分析工具nmon文件分析时报错解决办法
  4. 针对西门子PLC蠕虫的实现 
  5. 09-【el表达式和jstl标签库】
  6. 《Linux就该这么学》day3
  7. kotlin面向对象实战~
  8. 阿里云端安装mysql
  9. C# class 浅拷贝 与 深拷贝
  10. SSM项目无法解析JSP页面