package
action.fg;
import
java.io.BufferedReader;
import
java.io.InputStreamReader;
import
java.io.PrintWriter;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.apache.commons.logging.Log;
import
org.apache.commons.logging.LogFactory;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;
import
org.apache.struts.action.DynaActionForm;
import
org.apache.struts.actions.DispatchAction;
public
class
LinuxAction
extends
DispatchAction{
private
final
Log logger = LogFactory.getLog(
this
.getClass());
public
ActionForward ls_List(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res)
throws
Exception {
PrintWriter out = res.getWriter();
try
{
DynaActionForm letterForm = (DynaActionForm) form;
String str=letterForm.getString(
"str"
);
String listStr=callShell_ls(str);
out.print(listStr);
}
catch
(Exception e){
e.printStackTrace();
logger.info(
"ls_List error:"
+e.getMessage());
}
return
null
;
}
public
String callShell_ls(String lsStr){
StringBuffer sb =
new
StringBuffer();
try
{
String[] cmd =
new
String[]{
"/bin/sh"
,
"-c"
, lsStr};
logger.info(
"执行ING。。。。。"
+cmd.toString());
Process ps = Runtime.getRuntime().exec(cmd);
BufferedReader br =
new
BufferedReader(
new
InputStreamReader(ps.getInputStream()));
String line;
while
((line = br.readLine()) !=
null
) {
sb.append(line).append(
"<br/>"
);
}
logger.info(
"执行完成,字符串为:"
+sb.toString());
}
catch
(Exception e) {
e.printStackTrace();
logger.info(
"callShell_ls error:"
+e.getMessage());
}
return
sb.toString();
}
public
ActionForward start(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res)
throws
Exception {
PrintWriter out = res.getWriter();
try
{
DynaActionForm letterForm = (DynaActionForm) form;
callShell_ls(
"./start.sh"
);
String listStr=callShell_ls(
"tail -f ./logs/catalina.out"
);
out.print(listStr);
}
catch
(Exception e){
e.printStackTrace();
logger.info(
"start error:"
+e.getMessage());
}
return
null
;
}
public
ActionForward stop(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res)
throws
Exception {
PrintWriter out = res.getWriter();
try
{
DynaActionForm letterForm = (DynaActionForm) form;
callShell_ls(
"./stop.sh"
);
String listStr=callShell_ls(
"tail -f ./logs/catalina.out"
);
out.print(listStr);
}
catch
(Exception e){
e.printStackTrace();
logger.info(
"stop error:"
+e.getMessage());
}
return
null
;
}
}