List of usage examples for java.net NetworkInterface getHardwareAddress
public byte[] getHardwareAddress() throws SocketException
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) ? "-" : ""); }// w w w . j a v a 2 s. co m }
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) ? "-" : "")); }// ww w . j a v a 2 s.c om System.out.println(sb.toString()); }
From source file:org.cosmo.common.util.Util.java
public static void main(String[] args) { try {// w ww.jav a2s. c o m //InetAddress address = InetAddress.getLocalHost(); InetAddress address = InetAddress.getByName("192.168.46.53"); /* * Get NetworkInterface for the current host and then read the * hardware address. */ NetworkInterface ni = NetworkInterface.getByInetAddress(address); if (ni != null) { byte[] mac = ni.getHardwareAddress(); if (mac != null) { /* * Extract each array of mac address and convert it to hexa with the * following format 08-00-27-DC-4A-9E. */ for (int i = 0; i < mac.length; i++) { System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""); } } else { System.out.println("Address doesn't exist or is not accessible."); } } else { System.out.println("Network Interface for the specified address is not found."); } } catch (UnknownHostException e) { e.printStackTrace(); } catch (SocketException e) { e.printStackTrace(); } }
From source file:Main.java
public static String getMACAdress() { InetAddress ip;/*from w ww. j ava 2s .c o m*/ 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
/** * @return The MAC hardware address of the first network interface of this host. *//* ww w. j a v a2 s . com*/ public static byte[] getFirstNetworkInterfaceHardwareAddress() { try { Enumeration<NetworkInterface> interfaceEnumeration = NetworkInterface.getNetworkInterfaces(); for (NetworkInterface iface : Collections.list(interfaceEnumeration)) { if (!iface.isLoopback() && iface.isUp() && iface.getHardwareAddress() != null) { return iface.getHardwareAddress(); } } } catch (Exception ex) { throw new RuntimeException("Could not discover first network interface hardware address"); } throw new RuntimeException("Could not discover first network interface hardware address"); }
From source file:org.eclipse.recommenders.utils.rcp.UUIDHelper.java
private static Optional<String> generateUUIDFromMacAddress() { try {/* w w w . j a va 2s.co m*/ final Enumeration<NetworkInterface> e = getNetworkInterfaces(); for (final NetworkInterface net : iterable(e)) { final byte[] mac = net.getHardwareAddress(); if (ArrayUtils.isEmpty(mac)) { continue; } final String uuid = UUID.nameUUIDFromBytes(mac).toString(); if (Strings.isNullOrEmpty(uuid)) { continue; } return fromNullable(uuid); } } catch (final Exception e) { // this is odd: e.printStackTrace(); } return absent(); }
From source file:com.jaeksoft.searchlib.util.NetworksUtils.java
public static final String getHardwareAddress(NetworkInterface networkInterface) throws SocketException { if (networkInterface == null) return null; if (networkInterface.getHardwareAddress() == null) return null; return Hex.encodeHexString(networkInterface.getHardwareAddress()); }
From source file:org.pentaho.platform.util.UUIDUtil.java
private static String getInterfaceInfo(NetworkInterface nif, String sep) throws Exception { byte[] addrBytes = nif.getHardwareAddress(); // get the MAC address StringBuffer buff = new StringBuffer(); for (int i = 0; i < addrBytes.length; i++) { buff.append(String.format("%02X%s", addrBytes[i], (i < addrBytes.length - 1) ? sep : "")); //$NON-NLS-1$ //$NON-NLS-2$ }/*from www. ja v a 2s . co m*/ return buff.toString(); }
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 v a2s . 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.sakuli.actions.environment.CipherUtil.java
/** * Determines a valid network interfaces. * * @return ethernet interface name// ww w. j a v a 2 s . co m * @throws Exception */ static String autodetectValidInterfaceName() throws Exception { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface anInterface = interfaces.nextElement(); if (anInterface.getHardwareAddress() != null && anInterface.getHardwareAddress().length == 6) { return anInterface.getName(); } } throw new Exception("No network interface with a MAC address is present, please check your os settings!"); }