package net.jweb.common.util;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.util.Map;public class UsingProcessBuilder { public static String getNodeStatuss() { System.out.println("UsingProcessBuilder.getNodeStatuss()"); String command = " telnet 192.168.4.200 3306 "; command = "cmd /d dir";// ok command = "cmd";// ok command = "calc";// ok command = "telnet";// 不行 Process process; StringBuffer sb = new StringBuffer(); try { // Process proc=Runtime.getRuntime().exec("notepad"); process = Runtime.getRuntime().exec(command);// InputStream inputStream = process.getInputStream();// // int read;// byte[] b = new byte[1024*1024];// while ((read=inputStream.read(b))!=-1) {// String str = new String(b);// sb.append(str);// }// inputStream.close(); final InputStream is1 = process.getInputStream(); new Thread() { public void run() { BufferedReader br = new BufferedReader(new InputStreamReader(is1)); try { String lineB = null; while ((lineB = br.readLine()) != null) { if (lineB != null) System.out.println(lineB); } } catch (IOException e) { e.printStackTrace(); } } }.start(); } catch (IOException e) { e.printStackTrace(); } return sb.toString(); } /** * 获取Windows系统下的网卡的MAC地址 * * @return */ public static String getPhysicalAddress() { Process p = null; try { // 执行ipconfig /all命令 // "cmd ", " 192.168.4.200", " 3306" // "ipconfig", "/all" // p = new ProcessBuilder("ipconfig", "/all").start(); // p = new ProcessBuilder("telnet", "host 192.168.4.200").start(); p = Runtime.getRuntime().exec(" telnet 192.168.4.200 3306" );// p = new ProcessBuilder("cmd", " dir").start(); } catch (IOException e) { e.printStackTrace(); return "eeeeeeeee"; } byte[] b = new byte[1024]; int readbytes = -1; StringBuffer sb = new StringBuffer(); // 读取进程输出值 InputStream in = p.getInputStream(); try { while ((readbytes = in.read(b)) != -1) { sb.append(new String(b, 0, readbytes)); } } catch (IOException e1) { } finally { try { in.close(); } catch (IOException e2) { } } return sb.toString(); } public static void executeMyCommand2() { ProcessBuilder pb = null; String sysatt = null; try { // 创建一个进程示例 pb = new ProcessBuilder("cmd.exe"); // 获取系统参数并打印显示 Mapenv = pb.environment();// Iterator it = env.keySet().iterator();// while (it.hasNext()) {// sysatt = (String) it.next();// System.out.println("System Attribute:" + sysatt + "="// + env.get(sysatt));// } // 设置工作目录 pb.directory(new File("C:\\Windows\\winsxs\\wow64_microsoft-windows-commandprompt_31bf3856ad364e35_6.1.7601.17514_none_f387767e655cd5ab")); Process p = pb.start(); // 将要执行的Windows命令写入 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( p.getOutputStream())); // '/r/n'是必须写入的// bw.write("test.bat /r/n"); bw.write("ping -t www.baidu.com /r/n"); // flush()方法是必须调用的 bw.flush(); // 将执行结果打印显示 InputStream is = p.getInputStream(); InputStreamReader isr = new InputStreamReader(is, "GBK"); BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (Exception e) { e.printStackTrace(); } } /** * 执行自定义的一个命令,该命令放在C:/temp下,并且需要2个环境变量的支持。 */ public static boolean executeMyCommand() { // 创建系统进程创建器 ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2"); // 获得进程的环境 Map env = pb.environment(); // 设置和去除环境变量 env.put("VAR1", "myValue"); env.remove("VAR0"); env.put("VAR2", env.get("VAR1") + ";"); // 切换工作目录 pb.directory(new File("C:/temp")); try { // 得到进程实例 Process p = pb.start(); // 等待该进程执行完毕 if (p.waitFor() != 0) { // 如果进程运行结果不为0,表示进程是错误退出的 // 获得进程实例的错误输出 InputStream error = p.getErrorStream(); // do something } // 获得进程实例的标准输出 InputStream sdin = p.getInputStream(); } catch (IOException e) { } catch (InterruptedException e) { } return true; } public static void main(String[] args) { String address = UsingProcessBuilder.getNodeStatuss();// executeMyCommand2(); System.out.println(address); }}
如上的代码, 我想判断服务器远程端口是否开启, 却一直没有得到正确的结果。我使用的是telnet , 其他的好像也不行。
执行 telnet 的时候, waitFor()的返回值始终是-1, 读取p.getInputStream 始终没有输出。 Runtime/ ProcessBuilder 其实都是一样的。
后面突然领悟到, telnet其实是打开了子进程进行通话, 其本身没有任何内容输出,直接返回了-1。而java的Process 好像没有办法获取其是否成功生成了子进程, 以及子进程的内容。
读了 http://blog.csdn.net/Code_cj/article/details/6411682 后明白了。 要实现这样的功能恐怕只有借助第三方的工具:commons-net ---- Jsch
posted on 2016-06-07 16:24 阅读( ...) 评论( ...)