package com.web.console.shell;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ShellConsole extends HttpServlet {
private static final long serialVersionUID = 1L;
// static Logger log = LogManager.getLogger(ShellConsole.class);
// sh子进程实例
public static Process processContext = null;
//进程返回执行过程
public static List<String> listIN = new ArrayList<String>();
//进程返回错误信息
public static List<String> listOUT = new ArrayList<String>();
Thread thIn = null;
Thread thER = null;
/**
* 初始化
*/
@Override
public void init() throws ServletException {
if (processContext == null) {
try {
Shell.getInstance();
} catch (Exception e) {
e.printStackTrace();
}
}
try {
// 进程中执行过程信息
InputStream stremIN = processContext.getInputStream();
BufferedReader readIN = new BufferedReader(new InputStreamReader(stremIN,"GBK"));
// 进行执行过程中错误信息
InputStream stremERR = processContext.getErrorStream();
BufferedReader readERR = new BufferedReader(new InputStreamReader(stremERR,"GBK"));
thIn = new printStdout(readIN);
thER = new printStdout(readERR);
thIn.start();
thER.start();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
/**
* 退出
*/
// @Override
// public void destroy() {
// if (processContext != null) {
// try {
// Thread.sleep(3000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// try {
// processContext.getOutputStream().write("\r\nexit \n".getBytes());
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取页面输入的cmd
String inputCmd = req.getParameter("inputCmd");
String met = req.getParameter("met");
StringBuffer mesbuf = new StringBuffer();
resp.setCharacterEncoding("UTF-8");
if (met == null) {
dowork(inputCmd);
} else {
retMess(resp, mesbuf);
}
}
/**
* 输出命令
*
* @param inputCmd
* @throws IOException
*/
public void dowork(String inputCmd) throws IOException {
// 输出执行命令信息
OutputStream stremOUT = processContext.getOutputStream();
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(stremOUT));
listIN.add(" CMD : "+inputCmd);
write.write(inputCmd + "\t \n");
write.flush();
}
public void retMess(HttpServletResponse resp, StringBuffer mesbuf) throws IOException {
boolean isin=true;
if (listIN != null && listIN.size() > 0) {
do{
listOUT = listIN;
listIN = new ArrayList<String>();
for (String mes : listOUT) {
mesbuf.append(mes);
mesbuf.append(";");
}
isin=false;
}while (isin);
}
PrintWriter write = resp.getWriter();
write.write(mesbuf.toString());
write.flush();
write.close();
}
}
class printStdout extends Thread{
public BufferedReader readIN;
public printStdout(BufferedReader readIN){
this.readIN=readIN;
}
@Override
public void run() {
String line = null;
try {
while ((line = readIN.readLine()) != null) {
ShellConsole.listIN.add(line);
System.out.println("MESSAGR:" + line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用runtime实现linux命令行或shell脚本多次调用
需积分: 50 49 浏览量
2015-08-30
21:47:27
上传
评论
收藏 2KB ZIP 举报
登山的蚂蚁
- 粉丝: 0
- 资源: 3
最新资源
- vidstream-3-0.0.1.tar.gz
- 基于FPGA深度学习的SPI 驱动 HC595 点亮数码管
- 2023-2024学年教材费结算明细(公示) -V2.xls
- 129197954513812WPS.apk
- 1_2.cpp
- logxianshidddd
- 细胞的奇迹:吃出来的免疫力(美亚畅销书!作者的TED演讲播放量超300万次!《谷物大脑》作者、《三联生活周刊》推荐!吃下对的食物,改善你的... (Z-Library).azw3
- 智能手机产品拆解,使用mindmaster打开
- 11111111111111
- 统信系统ARM64依赖openssl-1.0.2版本的libssl.so.10和libcrypto.so.10文件
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈