2013-11-21 27 views
1

我可以使用下面的代碼如何查找遠程系統的MAC地址

package com.eiw.server; 

import java.net.InetAddress; 
import java.net.NetworkInterface; 
import java.net.SocketException; 
import java.net.UnknownHostException; 

class FindMACAddress { 
    public static void main(String[] args) { 
     InetAddress ip; 
     try { 
      ip = InetAddress.getLocalHost(); 

      System.out.println("The mac Address of this machine is :" 
        + ip.getHostAddress()); 

      NetworkInterface network = NetworkInterface.getByInetAddress(ip); 

      byte[] mac = network.getHardwareAddress(); 

      System.out.print("The mac address is : "); 

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

      System.out.println(sb.toString()); 

     } catch (UnknownHostException e) { 
      e.printStackTrace(); 
     } catch (SocketException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

得到本地MAC地址,但我需要找到遠程系統的MAC地址... 是否有可能?我已經通過了一些帖子......但不清楚....

+0

可能重複[?如何排除從Log4j的記錄器/追加程序的單一類] (http://stackoverflow.com/questions/5270052/how-to-exclude-a-single-class-from-a-log4j-logger-appender) – Raedwald

+0

我不認爲這是重複 – DThought

+2

可能重複[查詢ARP緩存獲取MAC ID](http:// stackoverflow .com/questions/1238963/query-arp-cache-to-get-mac-id) – reto

回答

0

這取決於。如果可以連接到遠程系統,則可以執行ifconfig/ipconfig命令,並從輸出中查看該機器的mac地址。但是如果你不能在遠程機器上連接和執行命令,我不認爲有任何方法可以知道該機器的MAC地址。

+0

感謝aryann for ur response ...你能告訴我如何在遠程系統中執行ipconfig命令嗎 – user3016821

+0

是的,使用JSSH庫並使用它連接到系統並執行命令ifconfig就可以了。輸出是這個命令的響應。網上有幾個例子可以做到這一點。它的幾乎4-5行代碼。試着告訴我你是否需要幫助。 – aryann

0

你可以得到通過標準網絡的其他系統的MAC地址是指當兩個系統在同一個網段(同一個局域網,之間沒有IP的路由器)

Query ARP cache to get MAC ID似乎回答你的問題

0

arp -a將向您顯示活動連接。例如:

接口:10.0.0.9 --- 0x19
|互聯網地址|物理地址|類型|

| 10.0.0.1 | c4-3d-c7-68-82-87 |動態|

我在這臺機器上有awk,所以下面會輸出MAC地址給我。我也在尋找一種在代碼中實現這一點的方式(以獨立於系統的方式)。

這可能會解決你正在尋找(在Java中的東西,如Process p = Runtime.getRuntime().exec("Enter command here")把它包):

arp -a | awk "/10.0.0.1/"' { gsub(/-/, "", $2); print toupper($2)} 

輸出:

C43DC7688287

+0

這似乎是一個問題。如果這是真的,你應該問另一個問題。如果這是一個答案,請改進您答案的格式。 –

+1

感謝您的幫助。 –

0
private static String getMacAdressByUseArp(String ip) throws IOException { 
    String cmd = "arp -a " + ip; 
    Scanner s = new Scanner(Runtime.getRuntime().exec(cmd).getInputStream()); 
    String str = null; 
    Pattern pattern = Pattern.compile("(([0-9A-Fa-f]{2}[-:]){5}[0-9A-Fa-f]{2})|(([0-9A-Fa-f]{4}\\.){2}[0-9A-Fa-f]{4})"); 
    try { 
     while (s.hasNext()) { 
      str = s.next(); 
      Matcher matcher = pattern.matcher(str); 
      if (matcher.matches()){ 
       break; 
      } 
      else{ 
       str = null; 
      } 
     } 
    } 
    finally { 
     s.close(); 
    } 
    return (str != null) ? str.toUpperCase(): null; 
} 
0

你可以得到遠程主機調用函數得到的mac地址MacAddrHost(「192.168.1.xx」)。這可能不是最好的解決方案,但它的效果很好。注意這隻適用於LAN內部。

public static String getMacAddrHost(String host) throws IOException, InterruptedException { 
     // 
     boolean ok = ping3(host); 
     // 
     if (ok) { 
      InetAddress address = InetAddress.getByName(host); 
      String ip = address.getHostAddress(); 
      return run_program_with_catching_output("arp -a " + ip); 
     } 
     // 
     return null; 
     // 
    } 


public static boolean ping3(String host) throws IOException, InterruptedException { 
     boolean isWindows = System.getProperty("os.name").toLowerCase().contains("win"); 

     ProcessBuilder processBuilder = new ProcessBuilder("ping", isWindows ? "-n" : "-c", "1", host); 
     Process proc = processBuilder.start(); 

     int returnVal = proc.waitFor(); 
     return returnVal == 0; 
    } 

    public static String run_program_with_catching_output(String param) throws IOException { 
     Process p = Runtime.getRuntime().exec(param); 
     BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); 
     String line; 
     while ((line = input.readLine()) != null) { 
      if (!line.trim().equals("")) { 
       // keep only the process name 
       line = line.substring(1); 
       String mac = extractMacAddr(line); 
       if (mac.isEmpty() == false) { 
        return mac; 
       } 
      } 

     } 
     return null; 
    } 

    public static String extractMacAddr(String str) { 
     String arr[] = str.split(" "); 
     for (String string : arr) { 
      if (string.trim().length() == 17) { 
       return string.trim().toUpperCase(); 
      } 
     } 
     return ""; 
    } 
-1

你可以得到客戶端IP地址,並使用MAC地址HttpServletRequest

參考鏈接:Link

public void clientIpAndMacAddress(HttpServletRequest request) 
{ 
    HttpServletRequest httpServletRequest = (HttpServletRequest) request; 
    String userIpAddress = httpServletRequest.getHeader("X-Forwarded-For"); 
    if (userIpAddress == null) { 
     userIpAddress = request.getRemoteAddr(); 
    } 
    System.out.println("Ip address : " + userIpAddress); 

    String str = ""; 
    String macAddress = ""; 
    try { 
     Process p = Runtime.getRuntime() 
       .exec("nbtstat -A " + userIpAddress); 
     InputStreamReader ir = new InputStreamReader(p.getInputStream()); 
     LineNumberReader input = new LineNumberReader(ir); 
     for (int i = 1; i < 100; i++) { 
      str = input.readLine(); 
      if (str != null) { 
       if (str.indexOf("MAC Address") > 1) { 
        macAddress = str.substring(
          str.indexOf("MAC Address") + 14, str.length()); 
        break; 
       } 
      } 
     } 
    } catch (IOException e) { 
     e.printStackTrace(System.out); 
    } 
    System.out.println("Mac address : " + macAddress); 
}