2016-03-01 60 views
0

我要列出ip和mac地址局域网中的所有设备(如网络扫描器)获取局域网中的所有IP和Mac地址

我想使用java编程语言。

如果我使用我的IP地址,结果是true,但是如果我在lan中使用另一个ip地址,网络变量为空。

(例如,我的IP地址:192.168.1.7,另一个IP地址:192.168.1.8)

这里我的代码;

public static void checkHosts(String subnet) throws UnknownHostException, IOException{ 
     int timeout=3000; 
     for (int i=1;i<255;i++){ 
      String host=subnet + "." + i; 
      if (InetAddress.getByName(host).isReachable(timeout)){ 
       System.out.println(host + " is reachable" + InetAddress.getByName(host)); 


       NetworkInterface network = NetworkInterface.getByInetAddress(InetAddress.getByName(host)); 

       if(network!=null){ 
        System.out.println(network.isUp()); 
        byte[] mac = network.getHardwareAddress(); 
        System.out.println(network.getDisplayName()); 
        System.out.println(network.getName()); 
        System.out.println(InetAddress.getByName(host).getHostName()); 
        System.out.print("Current MAC address : "); 

        StringBuilder sb = new StringBuilder(); 
        for (int j = 0; j < mac.length; j++) { 
         sb.append(String.format("%02X%s", mac[j], (j < mac.length - 1) ? "-" : ""));   
        } 
        System.out.println(sb.toString()); 
       } 

      } 
     } 
    } 
+1

的可能的复制[如何获得IP的连接在同一网络(子网)列表,使用Java(http://stackoverflow.com/questions/3345857/how-to-get-a -list-的-IP连接的功能于同一网络的子网使用-java的) – jww

回答

1

我一直在做一个项目来做同样的事情。我认为最好的方法是在运行时执行另一个进程并阅读结果。如前所述,您可以读取系统ARP表并解析结果,但这取决于平台。命令提示符中的Windows命令是:arp -a。

我选择了对nmap进行远程调用并解析这些结果。它要求你的机器上安装NMAP,但解决方案“应该”,只要安装NMAP相应版本的跨平台:

可在这里:https://nmap.org/download.html

这里有一个简单的例子。您当然需要进行一些更改以动态选择网络来扫描和解析结果,而不是打印结果。

try { 
    Process proc = Runtime.getRuntime().exec("nmap -PR -sn 192.168.1.0/24"); 

    BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream())); 
    BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); 

    // read the output from the command 
    String s = null; 

    while ((s = stdInput.readLine()) != null) { 
     System.out.println(s); 

    // read any errors from the attempted command 
    while ((s = stdError.readLine()) != null) { 
     System.err.println(s); 
    } 
} catch (IOException ex) { 
    System.err.println(ex); 
} 
2

基本上,你要做的是在你的本地网络上执行ping扫描。您提供的代码可能会执行所需的ping扫描(取决于实现),但它只显示本地接口的MAC地址。要确定网络上的计算机的MAC地址,您必须查看您的计算机的ARP缓存,这不是平台独立的,因此在Java中不容易实现。

1

评论(和javadoc)中所述的NetworkInterface类用于本地接口。它不能用于识别远程网卡。

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

public class RealMacAddress { 

    public static void main(String args[]){ 
     try { 
      Process proc = Runtime.getRuntime().exec("arp -a "); 

      BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream())); 
      BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); 

      // read the output from the command 
      String s = null; 

      while ((s = stdInput.readLine()) != null) { 
       System.out.println(s); 

      // read any errors from the attempted command 
      while ((s = stdError.readLine()) != null) { 
       System.err.println(s); 
      } 
      }}catch (IOException ex) { 
      System.err.println(ex); 
     } 

    } 
} 
相关问题