java 获取局域网内IP

java,如何获取局域网内所有的ip呢?

如何扫描局域网内的所有ip呢?

代码如下:

package com.jn.lan;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.StringTokenizer;
import java.util.Vector;

import com.common.util.SystemHWUtil;
import com.string.widget.util.ValueWidget;

public class LANIP {
	public static InetAddress[] getAllOnline() {
		// TODO Auto-generated method stub
		Vector v = new Vector(50);
		try {
			// Process process1 =
			// Runtime.getRuntime().exec("ping -w 2 -n 1 192.168.1.%i");
			// process1.destroy();
			Process process = Runtime.getRuntime().exec("arp -a");
			InputStreamReader inputStr = new InputStreamReader(
					process.getInputStream(), "GBK");
			BufferedReader br = new BufferedReader(inputStr);
			String temp = "";
			br.readLine();
			br.readLine();
			br.readLine();// 此后开始读取IP地址,之前为描述信息,忽略。
			while ((temp = br.readLine()) != null) {
				System.out.println(temp);
				if (!ValueWidget.isNullOrEmpty(temp)) {
					StringTokenizer tokens = new StringTokenizer(temp);
					String x;
					InetAddress add=null;
					try {
						add = InetAddress.getByName(x = tokens
								.nextToken());
					} catch (java.net.UnknownHostException e) {
						continue;
					}
					// System.out.println(x);
					v.add(add);
					// System.out.println(add);
				}
			}
			System.out.println(SystemHWUtil.DIVIDING_LINE);
			v.add(InetAddress.getLocalHost());
			process.destroy();
			br.close();
			inputStr.close();
		} catch (Exception e) {
			System.out.println("可能是网络不可用。");
			e.printStackTrace();
		}
		int cap = v.size();
		InetAddress[] addrs = new InetAddress[cap];
		for (int i = 0; i < cap; i++) {
			addrs[i] = (InetAddress) v.elementAt(i);
			 System.out.println(addrs[i]);
		}
		return addrs;

	}

	public static void main(String args[]) {
		InetAddress[] i = new LANIP().getAllOnline();
	}
}

 运行结果:

  169.254.255.255       ff-ff-ff-ff-ff-ff     静态        
  224.0.0.2             01-00-5e-00-00-02     静态        
  224.0.0.22            01-00-5e-00-00-16     静态        
  224.0.0.252           01-00-5e-00-00-fc     静态        
  226.81.9.8            01-00-5e-51-09-08     静态        
  239.255.255.250       01-00-5e-7f-ff-fa     静态        
  255.255.255.255       ff-ff-ff-ff-ff-ff     静态        

接口: 169.254.190.162 --- 0xd
  Internet 地址         物理地址              类型
  169.254.255.255       ff-ff-ff-ff-ff-ff     静态        
  224.0.0.2             01-00-5e-00-00-02     静态        
  224.0.0.22            01-00-5e-00-00-16     静态        
  224.0.0.252           01-00-5e-00-00-fc     静态        
  226.81.9.8            01-00-5e-51-09-08     静态        
  239.255.255.250       01-00-5e-7f-ff-fa     静态        
  255.255.255.255       ff-ff-ff-ff-ff-ff     静态        

接口: 192.168.1.105 --- 0x14
  Internet 地址         物理地址              类型
  192.168.1.1           00-21-5d-3c-79-16     动态        
  192.168.1.106         b8-76-3f-a6-a8-e9     动态        
  192.168.1.109         60-d8-19-14-6c-55     动态        
  192.168.1.110         00-21-5d-3c-79-16     动态        
  192.168.1.255         ff-ff-ff-ff-ff-ff     静态        
  224.0.0.2             01-00-5e-00-00-02     静态        
  224.0.0.22            01-00-5e-00-00-16     静态        
  224.0.0.252           01-00-5e-00-00-fc     静态        
  239.255.255.250       01-00-5e-7f-ff-fa     静态        
  255.255.255.255       ff-ff-ff-ff-ff-ff     静态        

接口: 192.168.137.1 --- 0x15
  Internet 地址         物理地址              类型
  192.168.137.182       84-7a-88-cf-87-35     动态        
  192.168.137.255       ff-ff-ff-ff-ff-ff     静态        
  224.0.0.2             01-00-5e-00-00-02     静态        
  224.0.0.22            01-00-5e-00-00-16     静态        
  224.0.0.252           01-00-5e-00-00-fc     静态        
  239.255.255.250       01-00-5e-7f-ff-fa     静态        
  255.255.255.255       ff-ff-ff-ff-ff-ff     静态        
---------------------------------------
/169.254.255.255
/224.0.0.2
/224.0.0.22
/224.0.0.252
/226.81.9.8
/239.255.255.250
/255.255.255.255
/169.254.255.255
/224.0.0.2
/224.0.0.22
/224.0.0.252
/226.81.9.8
/239.255.255.250
/255.255.255.255
/192.168.1.1
/192.168.1.106
/192.168.1.109
/192.168.1.110
/192.168.1.255
/224.0.0.2
/224.0.0.22
/224.0.0.252
/239.255.255.250
/255.255.255.255
/192.168.137.182
/192.168.137.255
/224.0.0.2
/224.0.0.22
/224.0.0.252
/239.255.255.250
/255.255.255.255
wh-THINK/169.254.86.27

 

你可能感兴趣的:(扫描,局域网,所有ip,LAN,搜索所有ip)