Example usage for java.net NetworkInterface getByInetAddress

List of usage examples for java.net NetworkInterface getByInetAddress

Introduction

In this page you can find the example usage for java.net NetworkInterface getByInetAddress.

Prototype

public static NetworkInterface getByInetAddress(InetAddress addr) throws SocketException 

Source Link

Document

Convenience method to search for a network interface that has the specified Internet Protocol (IP) address bound to it.

Usage

From source file:Main.java

static public void main(String args[]) throws Exception {
    InetAddress ia = InetAddress.getByName(args[0]);
    NetworkInterface ni = NetworkInterface.getByInetAddress(ia);
    System.out.println(ni);//  w ww.j a  v a2 s.  co  m
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InetAddress address = InetAddress.getLocalHost();

    NetworkInterface ni = NetworkInterface.getByInetAddress(address);
    byte[] mac = ni.getHardwareAddress();

    for (int i = 0; i < mac.length; i++) {
        System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
    }//from  ww w. ja v a2s. c  o m
}

From source file:ReportByAddress.java

static public void main(String args[]) throws Exception {
    InetAddress ia = InetAddress.getByName("www.google.ca");
    NetworkInterface ni = NetworkInterface.getByInetAddress(ia);
    System.out.println(ni);//from   ww  w  .ja  va 2  s . com
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InetAddress 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) ? "-" : ""));
    }//from  w w w . j a v a 2 s .  c o m
    System.out.println(sb.toString());
}

From source file:Main.java

public static String getMACAdress() {
    InetAddress ip;//from   w  w w.  j av  a  2  s  . c  om
    try {
        ip = InetAddress.getLocalHost();
        NetworkInterface network = NetworkInterface.getByInetAddress(ip);
        byte[] mac = network.getHardwareAddress();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mac.length; i++) {
            sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
        }
        return sb.toString();

    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static String getWifiMac(Context context) {
    String macAddr;//  w w w. j a v a2 s  .com
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    macAddr = wifiInfo.getMacAddress();
    if (TextUtils.isEmpty(macAddr)) {
        NetworkInterface networkInterface = null;
        try {
            networkInterface = NetworkInterface.getByInetAddress(InetAddress.getByName(getIPAddr()));
            byte[] hardwareAddress = networkInterface.getHardwareAddress();
            macAddr = byte2hex(hardwareAddress);
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
    return macAddr;
}

From source file:license.mac.MacTest.java

private static String getMACAddress(InetAddress ia) throws Exception {
    //???mac?mac?byte
    byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();

    //??mac??String
    StringBuffer sb = new StringBuffer();

    for (int i = 0; i < mac.length; i++) {
        if (i != 0) {
            sb.append("-");
        }/*w  w  w .ja v a  2  s. c  om*/
        //mac[i] & 0xFF byte
        String s = Integer.toHexString(mac[i] & 0xFF);
        sb.append(s.length() == 1 ? 0 + s : s);
    }

    //???mac?
    return sb.toString().toUpperCase();
}

From source file:experts.net.nic.MACAddr.java

/**
 * Obtains the hardware address (usually MAC) of an interface which is from an IP address or a host name (e.g. "10.0.0.1" or "example").
 *
 * @param address//from w  w w  .j  a  va  2 s  . c o m
 *            the String of the IP address or the host name
 * @return a byte array contains the address
 * @throws java.net.SocketException
 * @throws java.net.UnknownHostException
 */
public static byte[] getMACAddress(String address) throws SocketException, UnknownHostException {
    // Stored in a NIC network interface corresponding to the IP address or the host name
    NetworkInterface nic = NetworkInterface.getByInetAddress(InetAddress.getByName(address));

    // Returns byte[] the hardware address
    return nic.getHardwareAddress();
}

From source file:org.nebulaframework.grid.ID.java

/**
 * Attempts to read the MAC Address of the local Network
 * Interface.//w ww  .  ja  va2 s  . c om
 * 
 * @return MAC Address as a HEX-String
 * 
 * @throws IOException if occurred during process
 */
private static String getMACAddress() throws IOException {
    StringBuilder mac = new StringBuilder();

    // Obtain NetworkInterface Reference
    NetworkInterface networkInterface = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());

    // Read MAC Address as Bytes
    byte[] macBytes = networkInterface.getHardwareAddress();

    // Check if MAC info is not available
    if (macBytes == null)
        return null;

    // Convert MAC Address to String
    for (int i = 0; i < macBytes.length; i++) {
        mac.append(String.format("%02X%s", macBytes[i], (i < macBytes.length - 1) ? "-" : ""));
    }

    return mac.deleteCharAt(mac.length() - 1).toString();
}

From source file:com.baidu.rigel.biplatform.cache.util.MacAddressUtil.java

/** 
 * ??mac???mac?//from  w ww . ja v  a 2 s .c o  m
 * @param ia
 * @throws SocketException
 * @throws UnknownHostException 
 */
public static String getMacAddress(InetAddress ia) throws SocketException, UnknownHostException {
    if (ia == null) {
        ia = InetAddress.getLocalHost();
    }
    //????
    byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
    StringBuffer sb = new StringBuffer("");
    for (int i = 0; i < mac.length; i++) {
        if (i != 0) {
            sb.append("-");
        }
        //?
        int temp = mac[i] & 0xff;
        String str = Integer.toHexString(temp).toUpperCase();
        if (str.length() == 1) {
            sb.append("0" + str);
        } else {
            sb.append(str);
        }
    }
    return sb.toString();
}