2012-06-09 18 views
7

मैं एक समस्या हो रही थी अनुवर्ती कोड का उपयोग कर this question में एक मशीन है, जो हल किया गया था के मैक पते पाने के लिए जावा में मैक पते प्राप्त करें:का उपयोग कर getHardwareAddress गैर नियतात्मक

Process p = Runtime.getRuntime().exec("getmac /fo csv /nh"); 
java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream())); 
String line; 
line = in.readLine();   
String[] result = line.split(","); 

System.out.println(result[0].replace('"', ' ').trim()); 

हालांकि, मैं चाहते हैं पता है कि यह कोड क्यों काम नहीं कर रहा है। हर बार जब यह मैक पता पढ़ता है तो यह एक अलग मूल्य देता है। सबसे पहले मैंने सोचा कि ऐसा इसलिए था क्योंकि GetHash, शायद एक टाइमस्टैम्प का उपयोग करके मुझे पता नहीं है ... लेकिन इसे भी परिणाम परिवर्तन को हटा रहा है।

कोड

public static byte[] getMacAddress() { 
     try { 
      Enumeration<NetworkInterface> nwInterface = NetworkInterface.getNetworkInterfaces(); 
      while (nwInterface.hasMoreElements()) { 
       NetworkInterface nis = nwInterface.nextElement(); 
       if (nis != null) { 
        byte[] mac = nis.getHardwareAddress(); 
        if (mac != null) { 
         /* 
         * Extract each array of mac address and generate a 
         * hashCode for it 
         */ 
         return mac;//.hashCode(); 
        } else { 
         Logger.getLogger(Utils.class.getName()).log(Level.WARNING, "Address doesn't exist or is not accessible"); 
        } 
       } else { 
        Logger.getLogger(Utils.class.getName()).log(Level.WARNING, "Network Interface for the specified address is not found."); 
       } 
       return null; 
      } 
     } catch (SocketException ex) { 
      Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     return null; 
    } 
} 

आउटपुट उदाहरण (मैं बाइट सरणी से सीधे मुद्रण कर रहा हूँ, लेकिन इसके लिए पर्याप्त है कि विभिन्न मुझे लगता है कि देखने के लिए) पहले से

[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 

धन्यवाद

+1

आप डिफ़ॉल्ट 'एक बाइट सरणी के toString' मुद्रण कर रहे हैं। –

उत्तर

9

[email protected] वास्तव में परिणाम toString()byte[] सरणी का तरीका है।

मैं सुझाव दूंगा कि आप इसके बजाय new String(mac) का उपयोग करके मूल्य प्रिंट करें।

public String toString() { 
    return getClass().getName() + "@" + Integer.toHexString(hashCode()); 
} 

डिफ़ॉल्ट Object.hashCode() के बाद से स्मृति में पते के रूप में कार्यान्वित किया जाता है, इस प्रकार यह संगत नहीं है के रूप में आप नए Object हर बार पैदा कर रहे: के रूप में

byte[].toString() कार्यान्वित किया जाता है।

संपादित करें:

के बाद से लौटे बाइट हेक्स में है, इसलिए आप इसे दशमलव स्ट्रिंग में तब्दील करना चाहिए। कोड देख सकते हैं here

+0

@Wee: अजीब, यह नए स्ट्रिंग (मैक) के साथ कुछ भी प्रिंट नहीं करता है। अभी डबबगिंग ... –

+0

@PedroDusso मेरा अद्यतन उत्तर देखें। –

5

यहाँ कैसे जावा में मैक पता पाने के लिए पर Mkyong.com वेब साइट से एक उदाहरण है से:

package com.mkyong; 

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

public class app{ 

    public static void main(String[] args){ 

    InetAddress ip; 
    try { 

     ip = InetAddress.getLocalHost(); 
     System.out.println("Current IP address : " + ip.getHostAddress()); 

     NetworkInterface network = NetworkInterface.getByInetAddress(ip); 

     byte[] mac = network.getHardwareAddress(); 

     System.out.print("Current MAC address : "); 

     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(); 

    } 

    } 

} 
4

स्पेनी से जवाब अगर मशीन जुड़ा हुआ नहीं है काम नहीं करता है और यह विभिन्न मूल्यों को उस नेटवर्क पर निर्भर करेगा जो आपसे जुड़े हुए हैं।

यह एक किसी भी आईपी पते के आधार पर नहीं किया गया है:

public class MacAdress { 
    public static void main(String[] args) { 
     try { 
      InetAddress ip = InetAddress.getLocalHost(); 
      System.out.println("Current IP address : " + ip.getHostAddress()); 

      Enumeration<NetworkInterface> networks = 
          NetworkInterface.getNetworkInterfaces(); 
      while(networks.hasMoreElements()) { 
       NetworkInterface network = networks.nextElement(); 
       byte[] mac = network.getHardwareAddress(); 

       if (mac != null) { 
        System.out.print("Current MAC address : "); 

        StringBuilder sb = new StringBuilder(); 
        for (int i = 0; i < mac.length; i++) { 
         sb.append(String.format("%02X%s", mac[i], 
            (i < mac.length - 1) ? "-" : "")); 
        } 
       } 
      } 
     } catch (UnknownHostException e) { 
      e.printStackTrace(); 
     } catch (SocketException e){ 
      e.printStackTrace(); 
     } 
    } 
} 
2
public static String getHardwareAddress() throws Exception { 
    InetAddress ip = InetAddress.getLocalHost(); 
    NetworkInterface ni = NetworkInterface.getByInetAddress(ip); 
    if (!ni.isVirtual() && !ni.isLoopback() && !ni.isPointToPoint() && ni.isUp()) { 
     final byte[] bb = ni.getHardwareAddress(); 
     return IntStream.generate(ByteBuffer.wrap(bb)::get).limit(bb.length) 
       .mapToObj(b -> String.format("%02X", (byte)b)) 
       .collect(Collectors.joining("-")); 
    } 
    return null; 
}