java代码实现运行cmd命令

package test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CMDtest {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		//简单测试局域网中的电脑是否连通.这些电脑的ip范围从192.168.0.101到192.168.0.200.
		
		String cmd = "cmd.exe /c ping ";
		String ipprefix = "192.168.0.";
		int begin = 101;
		int end = 200;
		Process p = null;
		
		for(int i=begin;i<end;i++)
		{
			p = Runtime.getRuntime().exec(cmd + ipprefix + i);
			String line = null;
			BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
			while((line = reader.readLine()) != null)				
			{
				System.out.println(line);
			}
			reader.close();
			p.destroy();
		}
	}

}

猜你喜欢

转载自huahua09.iteye.com/blog/1694800