2016-03-01 61 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); 
     } 

    } 
} 
相關問題