List of usage examples for java.net NetworkInterface getHardwareAddress
public byte[] getHardwareAddress() throws SocketException
From source file:org.ihsp.data.common.ObjectId.java
private static int createMachineIdentifier() { // build a 2-byte machine piece based on NICs info int machinePiece; try {/* w ww.java 2 s . co m*/ StringBuilder sb = new StringBuilder(); Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); while (e.hasMoreElements()) { NetworkInterface ni = e.nextElement(); sb.append(ni.toString()); byte[] mac = ni.getHardwareAddress(); if (mac != null) { ByteBuffer bb = ByteBuffer.wrap(mac); try { sb.append(bb.getChar()); sb.append(bb.getChar()); sb.append(bb.getChar()); } catch (BufferUnderflowException shortHardwareAddressException) { //NOPMD // mac with less than 6 bytes. continue } } } machinePiece = sb.toString().hashCode(); } catch (Throwable t) { // exception sometimes happens with IBM JVM, use random machinePiece = (new SecureRandom().nextInt()); log.info("Failed to get machine identifier from network interface, using random number instead", t); } machinePiece = machinePiece & LOW_ORDER_THREE_BYTES; return machinePiece; }
From source file:com.asquareb.kaaval.MachineKey.java
/** * Method to encrypt a string. Accepts the string to be encrypted and the * name of the file to store the key vale which can be used for decryption *///w ww.j av a2 s .co m public static String encrypt(String property, String app) throws IOException, KaavalException { InetAddress ip = null; String ipAddress = null; ObjectOutputStream os = null; NetworkInterface macAddress = null; byte[] macId = null; Cipher pbeCipher = null; Random rand = new Random(); rand.nextBytes(salt); try { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(password)); ip = InetAddress.getLocalHost(); ipAddress = ip.getHostAddress(); macAddress = NetworkInterface.getByInetAddress(ip); macId = macAddress.getHardwareAddress(); MachineKey mKey = new MachineKey(); mKey.api = ipAddress; mKey.macad = new String(macId); mKey.yek = key; mKey.tlas = salt; mKey.eti = rand.nextInt(1000); os = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(app))); os.writeObject(mKey); os.close(); pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(salt, mKey.eti)); return base64Encode(pbeCipher.doFinal(property.getBytes())); } catch (IOException e) { throw new KaavalException(1, "Error in key file during encryption", e); } catch (Exception e) { throw new KaavalException(2, "Errors during encryption", e); } finally { if (os != null) os.close(); } }
From source file:org.basdroid.common.NetworkUtils.java
/** * Returns MAC address of the given interface name. * @param interfaceName eth0, wlan0 or NULL=use first interface * @return mac address or empty string// w ww .j a v a2s .co m */ public static String getMACAddress(String interfaceName) { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { if (interfaceName != null) { Log.d(TAG, "intf.getName() = " + intf.getName()); if (!intf.getName().equalsIgnoreCase(interfaceName)) continue; } byte[] mac = intf.getHardwareAddress(); if (mac == null) return ""; StringBuilder buf = new StringBuilder(); for (int idx = 0; idx < mac.length; idx++) { buf.append(String.format("%02X:", mac[idx])); } if (buf.length() > 0) buf.deleteCharAt(buf.length() - 1); return buf.toString(); } } catch (Exception ex) { } // for now eat exceptions return ""; }
From source file:net.ftb.util.OSUtils.java
/** * Grabs the mac address of computer and makes it 10 times longer * @return a byte array containing mac address *///w w w .j a va2 s . c om public static byte[] getMacAddress() { if (cachedMacAddress != null && cachedMacAddress.length >= 10) { return cachedMacAddress; } try { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface network = networkInterfaces.nextElement(); byte[] mac = network.getHardwareAddress(); if (mac != null && mac.length > 0 && !network.isLoopback() && !network.isVirtual() && !network.isPointToPoint() && network.getName().substring(0, 3) != "ham") { Logger.logDebug("Interface: " + network.getDisplayName() + " : " + network.getName()); cachedMacAddress = new byte[mac.length * 10]; for (int i = 0; i < cachedMacAddress.length; i++) { cachedMacAddress[i] = mac[i - (Math.round(i / mac.length) * mac.length)]; } return cachedMacAddress; } } } catch (SocketException e) { Logger.logWarn("Exception getting MAC address", e); } Logger.logWarn("Failed to get MAC address, using default logindata key"); return new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; }
From source file:com.adamkruger.myipaddressinfo.NetworkInfoFragment.java
@TargetApi(Build.VERSION_CODES.GINGERBREAD) private static List<NetworkInterfaceInfo> getNetworkInterfaceInfos() { List<NetworkInterfaceInfo> networkInterfaceInfos = new ArrayList<NetworkInterfaceInfo>(); try {//from www . j a va 2 s. com List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface networkInterface : interfaces) { NetworkInterfaceInfo networkInterfaceInfo = new NetworkInterfaceInfo(); networkInterfaceInfo.name = networkInterface.getDisplayName(); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) { byte[] MAC = networkInterface.getHardwareAddress(); if (MAC != null) { StringBuilder stringBuilder = new StringBuilder(18); for (byte b : MAC) { if (stringBuilder.length() > 0) { stringBuilder.append(':'); } stringBuilder.append(String.format("%02x", b)); } networkInterfaceInfo.MAC = stringBuilder.toString(); } networkInterfaceInfo.MTU = networkInterface.getMTU(); } List<InetAddress> addresses = Collections.list(networkInterface.getInetAddresses()); for (InetAddress address : addresses) { if (!address.isLoopbackAddress()) { networkInterfaceInfo.ipAddresses.add(InetAddressToString(address)); } } if (networkInterfaceInfo.ipAddresses.size() > 0) { networkInterfaceInfos.add(networkInterfaceInfo); } } } catch (SocketException e) { } return networkInterfaceInfos; }
From source file:Main.java
private static void displayInterfaceInformation(NetworkInterface netint) throws SocketException { System.out.printf("Display name: %s%n", netint.getDisplayName()); System.out.printf("Name: %s%n", netint.getName()); Enumeration<InetAddress> inetAddresses = netint.getInetAddresses(); for (InetAddress inetAddress : Collections.list(inetAddresses)) { System.out.printf("InetAddress: %s%n", inetAddress); }// w w w . j av a2 s . co m System.out.printf("Parent: %s%n", netint.getParent()); System.out.printf("Up? %s%n", netint.isUp()); System.out.printf("Loopback? %s%n", netint.isLoopback()); System.out.printf("PointToPoint? %s%n", netint.isPointToPoint()); System.out.printf("Supports multicast? %s%n", netint.isVirtual()); System.out.printf("Virtual? %s%n", netint.isVirtual()); System.out.printf("Hardware address: %s%n", Arrays.toString(netint.getHardwareAddress())); System.out.printf("MTU: %s%n", netint.getMTU()); List<InterfaceAddress> interfaceAddresses = netint.getInterfaceAddresses(); for (InterfaceAddress addr : interfaceAddresses) { System.out.printf("InterfaceAddress: %s%n", addr.getAddress()); } System.out.printf("%n"); Enumeration<NetworkInterface> subInterfaces = netint.getSubInterfaces(); for (NetworkInterface networkInterface : Collections.list(subInterfaces)) { System.out.printf("%nSubInterface%n"); displayInterfaceInformation(networkInterface); } System.out.printf("%n"); }
From source file:com.distrimind.madkit.kernel.MadkitProperties.java
private static long getMacAddress() { long result = 0; long result2 = 0; try {/*from ww w.ja va 2 s . c o m*/ final Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); while (e.hasMoreElements()) { final NetworkInterface ni = e.nextElement(); if (!ni.isLoopback()) { long val = getHardwareAddress(ni.getHardwareAddress()); if (val != 0 && val != 224) { if (ni.isPointToPoint()) { result2 = val; } else { result = val; break; } } } } } catch (Exception e) { e.printStackTrace(); } if (result == 0) result = result2; return result; }
From source file:org.tinymediamanager.core.License.java
/** * returns the MAC address of this instance * /*from www . jav a 2s .co m*/ * @return MAC or empty string */ public static String getMac() { try { InetAddress ip = InetAddress.getLocalHost(); if (ip != null) { // we are connected to Internet/router and have an IP NetworkInterface ni = NetworkInterface.getByInetAddress(ip); if (ni == null) { // search for other interfaces Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface network = networkInterfaces.nextElement(); if (!network.isLoopback()) { ni = network; break; } } } String macAddress = formatMac(ni.getHardwareAddress()); if (macAddress != null && !macAddress.isEmpty()) { return macAddress; } } } catch (Exception e) { e.printStackTrace(); LOGGER.warn("Error getting MAC from LocalHost IP - not connected to internet/router?"); } try { for (Enumeration<NetworkInterface> nif = NetworkInterface.getNetworkInterfaces(); nif .hasMoreElements();) { NetworkInterface ni = null; try { ni = nif.nextElement(); String macAddress = formatMac(ni.getHardwareAddress()); if (macAddress != null && !macAddress.isEmpty()) { // get first return macAddress; } } catch (Exception e2) { LOGGER.warn("Error getting MAC of " + ni); } } return UNKNOWN_MAC; } catch (Exception e) { LOGGER.warn("I/O Error on getting network interfaces"); return UNKNOWN_MAC; } }
From source file:org.wso2.carbon.apimgt.hybrid.gateway.configurator.Configurator.java
/** * Retrieve host name, mac address of the device * * @return details Map<String, String> */// w ww.ja v a 2 s . c om protected static Map<String, String> getDeviceDetails() { InetAddress ip; String hostName = ""; String macAddress = ConfigConstants.DEFAULT_MAC_ADDRESS; Map<String, String> details = new HashMap(); try { ip = InetAddress.getLocalHost(); hostName = ip.getHostName(); Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces(); while (networkInterfaceEnumeration.hasMoreElements()) { NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement(); Enumeration<InetAddress> enumeration = networkInterface.getInetAddresses(); for (; enumeration.hasMoreElements();) { InetAddress address = enumeration.nextElement(); if (!address.isLoopbackAddress() && !address.isLinkLocalAddress() && address.isSiteLocalAddress()) { byte[] mac = networkInterface.getHardwareAddress(); if (mac != null) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < mac.length; i++) { //Construct mac address sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ConfigConstants.DELIMITER : "")); } macAddress = sb.toString(); break; } } } } } catch (UnknownHostException | SocketException e) { log.error("Error while retrieving mac address", e); Runtime.getRuntime().exit(1); } details.put(ConfigConstants.HOST_NAME, hostName); details.put(ConfigConstants.MAC_ADDRESS, macAddress); return details; }
From source file:com.landenlabs.all_devtool.NetFragment.java
public static String getMacAddr() { try {//from ww w.jav a2 s . c om List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface nif : all) { if (!nif.getName().equalsIgnoreCase("wlan0")) continue; byte[] macBytes = nif.getHardwareAddress(); if (macBytes == null) { return ""; } StringBuilder res1 = new StringBuilder(); for (byte b : macBytes) { res1.append(Integer.toHexString(b & 0xFF)).append(":"); } if (res1.length() > 0) { res1.deleteCharAt(res1.length() - 1); } return res1.toString(); } } catch (Exception ex) { } return "02:00:00:00:00:00"; }