List of usage examples for java.net NetworkInterface isLoopback
public boolean isLoopback() throws SocketException
From source file:uk.ac.horizon.ubihelper.j2se.Server.java
/** * @param args//from w w w. ja va2 s . c o m */ public static void main(String[] args) { InetAddress bestAddress = null; try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface ni = interfaces.nextElement(); if (!ni.isUp() || ni.isVirtual() || ni.isLoopback()) continue; logger.info("Has interface " + ni.getName() + ": " + ni.getDisplayName()); Enumeration<InetAddress> as = ni.getInetAddresses(); while (as.hasMoreElements()) { InetAddress a = as.nextElement(); if (a instanceof Inet4Address) { Inet4Address ip = (Inet4Address) a; logger.info("- IPv4 address " + ip.getHostAddress()); if (ip.isSiteLocalAddress()) { logger.info("-- site local!"); if (bestAddress == null) bestAddress = ip; } else bestAddress = ip; } } } } catch (Exception e) { logger.severe("Could not list NetworkInterfaces: " + e); System.exit(-1); } if (bestAddress == null) { logger.severe("Could not find an IP address to bind to - using localhost"); try { bestAddress = InetAddress.getLocalHost(); } catch (Exception e) { logger.severe("Could not get localhost address: " + e); System.exit(-2); } } int port = 0; if (args.length == 1) { try { port = Integer.parseInt(args[0]); } catch (NumberFormatException nfe) { System.err.println("Usage: <server-port>"); System.exit(-3); } } Server server = new Server(); server.init(bestAddress, port); }
From source file:Main.java
/** * @return The MAC hardware address of the first network interface of this host. *//*from w ww .jav a 2 s . c o m*/ 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.nebula.service.core.HostAddress.java
public static String getLocalHost() throws Exception { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface current = interfaces.nextElement(); if (!current.isUp() || current.isLoopback() || current.isVirtual()) { continue; }/*ww w .ja v a 2s . co m*/ Enumeration<InetAddress> addresses = current.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress current_addr = addresses.nextElement(); if (current_addr.isLoopbackAddress() || !InetAddressUtils.isIPv4Address(current_addr.getHostAddress())) { continue; } return current_addr.getHostName(); } } throw new Exception("Failed to get local hostname"); }
From source file:Main.java
public static InetAddress getHostAddress() { try {// w w w .j a va 2 s .com Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); // filters out 127.0.0.1 and inactive interfaces if (iface.isLoopback() || !iface.isUp()) continue; Enumeration<InetAddress> addresses = iface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (addr instanceof Inet4Address) return addr; } } } catch (SocketException e) { throw new RuntimeException(e); } return null; }
From source file:net.sf.jasperreports.phantomjs.InetUtil.java
public static Inet4Address getIPv4Loopback() { InetAddress loopbackAddress = InetAddress.getLoopbackAddress(); //the phantomjs web server module only works with IPv4 if (loopbackAddress instanceof Inet4Address) { return (Inet4Address) loopbackAddress; }/*from w ww .ja va2 s. com*/ try { InetAddress[] addresses = InetAddress.getAllByName(loopbackAddress.getHostName()); for (InetAddress inetAddress : addresses) { if (inetAddress instanceof Inet4Address) { return (Inet4Address) inetAddress; } } } catch (UnknownHostException e) { log.warn("Error while determining loopback addresses for " + loopbackAddress.getHostName(), e); } try { //keep looking for a IPv4 loopback address for (Enumeration<NetworkInterface> itfs = NetworkInterface.getNetworkInterfaces(); itfs .hasMoreElements();) { NetworkInterface itf = itfs.nextElement(); if (itf.isLoopback()) { for (Enumeration<InetAddress> addresses = itf.getInetAddresses(); addresses .hasMoreElements();) { InetAddress address = addresses.nextElement(); if (address instanceof Inet4Address) { return (Inet4Address) address; } } } } } catch (SocketException e) { log.warn("Error while listing network interfaces", e); } return null; }
From source file:codeOrchestra.lcs.license.ActivationReporter.java
private static String getFingerPrint() { StringBuilder resultSB = new StringBuilder(); try {/*from www. ja va 2s .co m*/ for (final Enumeration<NetworkInterface> interfaces = NetworkInterface .getNetworkInterfaces(); interfaces.hasMoreElements();) { final NetworkInterface networkInterface = (NetworkInterface) interfaces.nextElement(); if (networkInterface.isLoopback()) { continue; } byte[] mac = networkInterface.getHardwareAddress(); if (mac == null) { continue; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < mac.length; i++) { sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "")); } if ("00-00-00-00-00-00-00-E0".equals(sb.toString())) { continue; } resultSB.append(sb); if (interfaces.hasMoreElements()) { resultSB.append("|"); } } } catch (Exception e) { // ignore } String result = resultSB.toString(); if (result.endsWith("|")) { return result.substring(0, result.length() - 1); } return result; }
From source file:io.galeb.router.sync.HttpClient.java
private static String localIpsEncoded() { final List<String> ipList = new ArrayList<>(); try {//from w w w . j a v a 2 s . c om Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces(); while (ifs.hasMoreElements()) { NetworkInterface localInterface = ifs.nextElement(); if (!localInterface.isLoopback() && localInterface.isUp()) { Enumeration<InetAddress> ips = localInterface.getInetAddresses(); while (ips.hasMoreElements()) { InetAddress ipaddress = ips.nextElement(); if (ipaddress instanceof Inet4Address) { ipList.add(ipaddress.getHostAddress()); break; } } } } } catch (Exception e) { LOGGER.error(ExceptionUtils.getStackTrace(e)); } String ip = String.join("-", ipList); if (ip == null || "".equals(ip)) { ip = "undef-" + System.currentTimeMillis(); } return ip.replaceAll("[:%]", ""); }
From source file:org.openhab.binding.network.service.NetworkUtils.java
/** * Gets every IPv4 Address on each Interface except the loopback * The Address format is ip/subnet/*from w ww . j ava 2 s .co m*/ * * @return The collected IPv4 Addresses */ public static TreeSet<String> getInterfaceIPs() { TreeSet<String> interfaceIPs = new TreeSet<String>(); try { // For each interface ... for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface networkInterface = en.nextElement(); if (!networkInterface.isLoopback()) { // .. and for each address ... for (Iterator<InterfaceAddress> it = networkInterface.getInterfaceAddresses().iterator(); it .hasNext();) { // ... get IP and Subnet InterfaceAddress interfaceAddress = it.next(); interfaceIPs.add(interfaceAddress.getAddress().getHostAddress() + "/" + interfaceAddress.getNetworkPrefixLength()); } } } } catch (SocketException e) { } return interfaceIPs; }
From source file:org.apache.zeppelin.interpreter.remote.RemoteInterpreterUtils.java
public static String findAvailableHostAddress() throws UnknownHostException, SocketException { InetAddress address = InetAddress.getLocalHost(); if (address.isLoopbackAddress()) { for (NetworkInterface networkInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) { if (!networkInterface.isLoopback()) { for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) { InetAddress a = interfaceAddress.getAddress(); if (a instanceof Inet4Address) { return a.getHostAddress(); }//w w w . ja va 2s . com } } } } return address.getHostAddress(); }
From source file:com.vmware.identity.idm.CommonUtil.java
private static String findInetAddress(Predicate<InetAddress> match) throws SocketException { String ipAddress = null;// www . jav a 2s . c o m Enumeration<NetworkInterface> it = NetworkInterface.getNetworkInterfaces(); while ((ipAddress == null) && it.hasMoreElements()) { NetworkInterface iface = it.nextElement(); if ((!iface.isLoopback()) && (iface.isUp())) { Enumeration<InetAddress> it2 = iface.getInetAddresses(); while (it2.hasMoreElements()) { InetAddress addr = it2.nextElement(); if (match.matches(addr)) { ipAddress = addr.getHostAddress(); break; } } } } return ipAddress; }