Java 扫描局域网IP

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

public class IP {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			//Runtime.getRuntime().exec("ping 192.168.1.1").destroy();
			//Runtime.getRuntime().exec("for /l %i in (1,1,255) do " +
			//		"(ping -w 2 -n 1 192.168.1.%i)").destroy();
			
			String localIP=InetAddress.getLocalHost().getHostAddress();
			String[] net=localIP.split(".");
			
			int j=Integer.parseInt(net[2]);
			for(int i=1;i<256;i++){
				Runtime.getRuntime().exec("ping -w 2 -n 1 192.168."+j+"."+i).destroy();
			}
			
			Process p=Runtime.getRuntime().exec("arp -a");
			BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
			
			br.readLine();
			br.readLine();
			br.readLine();
			String temp=null;
			while(((temp=br.readLine())!=null&&!temp.isEmpty())){
				System.out.println(temp.trim().split(" ")[0]);
				
			}
			
			//String localIP=InetAddress.getLocalHost().getHostAddress();
			//String[] net=localIP.split(".");
			
			//System.out.println(net[2]);
			
			//System.out.println(InetAddress.getLocalHost().getHostAddress());
			p.destroy();
			br.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}


}



知识共享许可协议本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。

相关推荐

暂无评论