List of usage examples for java.net InetAddress getHostAddress
public String getHostAddress()
From source file:com.redhat.rhn.domain.monitoring.satcluster.SatClusterFactory.java
/** * Create a new SatCluster (scout)// w ww. j ava 2s .c om * @param user who creates the Scout * @return new instance */ public static SatCluster createSatCluster(User user) { SatCluster retval = new SatCluster(); CommandTarget ct = new CommandTarget(); ct.setOrg(user.getOrg()); ct.setTargetType("cluster"); retval.setCommandTarget(ct); retval.setOrg(user.getOrg()); retval.setPhysicalLocation(PHYSICAL_LOCATION); retval.setTargetType(ct.getTargetType()); retval.setDeployed("1"); retval.setLastUpdateUser(user.getLogin()); retval.setLastUpdateDate(new Date()); try { InetAddress ip = InetAddress.getLocalHost(); boolean haveIpv4 = ip instanceof Inet4Address; if (haveIpv4) { retval.setVip(ip.getHostAddress()); } else { retval.setVip6(ip.getHostAddress()); } NetworkInterface ni = NetworkInterface.getByInetAddress(ip); for (InterfaceAddress ifa : ni.getInterfaceAddresses()) { InetAddress ia = ifa.getAddress(); if ((ia instanceof Inet4Address) != haveIpv4) { if (haveIpv4) { retval.setVip6(ia.getHostAddress()); } else { retval.setVip(ia.getHostAddress()); } break; } } } catch (Exception e) { log.warn("Failed to find out IP host addresses. " + "Setting loopback IPs instead."); try { NetworkInterface ni = NetworkInterface.getByName("lo"); for (InterfaceAddress ifa : ni.getInterfaceAddresses()) { InetAddress ia = ifa.getAddress(); if (ia instanceof Inet4Address) { if (StringUtils.isEmpty(retval.getVip())) { retval.setVip(ia.getHostAddress()); } } else { //IPv6 if (StringUtils.isEmpty(retval.getVip6())) { retval.setVip6(ia.getHostAddress()); } } } } catch (SocketException se) { log.fatal("Failed to find out loopback IPs."); se.printStackTrace(); } } return retval; }
From source file:hsyndicate.utils.IPUtils.java
public static Collection<String> getIPAddress() { if (!cachedIPAddr.isEmpty()) { return Collections.unmodifiableCollection(cachedIPAddr); } else {/*from www . j ava2s. c o m*/ try { Enumeration e = NetworkInterface.getNetworkInterfaces(); while (e.hasMoreElements()) { NetworkInterface n = (NetworkInterface) e.nextElement(); Enumeration ee = n.getInetAddresses(); while (ee.hasMoreElements()) { InetAddress i = (InetAddress) ee.nextElement(); if (!i.isLoopbackAddress()) { String hostAddress = i.getHostAddress(); cachedIPAddr.add(hostAddress); } } } } catch (SocketException ex) { LOG.error("Exception occurred while scanning local interfaces", ex); } return Collections.unmodifiableCollection(cachedIPAddr); } }
From source file:eu.eubrazilcc.lvl.core.util.NetworkingUtils.java
/** * Gets the first public IP address of the host. If no public address are found, one of the private * IPs are randomly selected. Otherwise, it returns {@code localhost}. * @return the first public IP address of the host. *///from ww w . ja v a 2 s . c o m public static final String getInet4Address() { String inet4Address = null; final List<String> localAddresses = new ArrayList<String>(); try { final Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces(); if (networks != null) { final List<NetworkInterface> ifs = Collections.list(networks); for (int i = 0; i < ifs.size() && inet4Address == null; i++) { final Enumeration<InetAddress> inetAddresses = ifs.get(i).getInetAddresses(); if (inetAddresses != null) { final List<InetAddress> addresses = Collections.list(inetAddresses); for (int j = 0; j < addresses.size() && inet4Address == null; j++) { final InetAddress address = addresses.get(j); if (address instanceof Inet4Address && !address.isAnyLocalAddress() && !address.isLinkLocalAddress() && !address.isLoopbackAddress() && StringUtils.isNotBlank(address.getHostAddress())) { final String hostAddress = address.getHostAddress().trim(); if (!hostAddress.startsWith("10.") && !hostAddress.startsWith("172.16.") && !hostAddress.startsWith("192.168.")) { inet4Address = hostAddress; } else { localAddresses.add(hostAddress); } LOGGER.trace( "IP found - Name: " + address.getHostName() + ", Addr: " + hostAddress); } } } } } } catch (Exception e) { LOGGER.warn("Failed to discover public IP address for this host", e); } return (StringUtils.isNotBlank(inet4Address) ? inet4Address : (!localAddresses.isEmpty() ? localAddresses.get(new Random().nextInt(localAddresses.size())) : "localhost")).trim(); }
From source file:com.vaadin.tests.tb3.PrivateTB3Configuration.java
/** * Tries to automatically determine the IP address of the machine the test * is running on.//from w w w . j a v a 2 s.c o m * * @return An IP address of one of the network interfaces in the machine. * @throws RuntimeException * if there was an error or no IP was found */ private static String findAutoHostname() { try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface nwInterface = interfaces.nextElement(); if (!nwInterface.isUp() || nwInterface.isLoopback() || nwInterface.isVirtual()) { continue; } Enumeration<InetAddress> addresses = nwInterface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = addresses.nextElement(); if (address.isLoopbackAddress()) { continue; } if (address.isSiteLocalAddress()) { return address.getHostAddress(); } } } } catch (SocketException e) { throw new RuntimeException("Could not enumerate "); } throw new RuntimeException("No compatible (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16) ip address found."); }
From source file:totalcross.android.ConnectionManager4A.java
public static String getLocalIpAddress() { String ipv4 = null;//from w w w . j a va 2 s . c o m try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); // for getting IPV4 format if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4 = inetAddress.getHostAddress())) return ipv4; } } } catch (Exception ex) { AndroidUtils.debug("IP Address" + ex.toString()); } return null; }
From source file:de.vanita5.twittnuker.util.net.ssl.AbstractCheckSignatureVerifier.java
private static String normaliseIPv6Address(final String hostname) { if (hostname == null || !InetAddressUtilsHC4.isIPv6Address(hostname)) return hostname; try {/*from w w w. j a v a 2 s . c o m*/ final InetAddress inetAddress = InetAddress.getByName(hostname); return inetAddress.getHostAddress(); } catch (final UnknownHostException uhe) { // Should not happen, because // we check for IPv6 address // above Log.e(TAG, "Unexpected error converting " + hostname, uhe); return hostname; } }
From source file:cn.bc.web.util.WebUtils.java
/** * ?Server IP??/*w ww . j av a2 s. com*/ * * @return */ public static String getServerIP() { String ip; try { InetAddress localhost = InetAddress.getLocalHost(); ip = localhost.getHostAddress(); } catch (UnknownHostException e) { ip = "UnknownHost"; } return ip; }
From source file:com.noterik.bart.fs.fscommand.dynamic.presentation.playout.cache.java
public static void init() { readCacheConfig();//from ww w.java2 s . c om if (receiver == null) { started = true; receiver = new CacheMulticastReceiver("receiver"); receiver.start(); cachereader = new CacheTableReader("cachereader"); cachereader.start(); cachewriter = new CacheTableWriter("cachewriter", cachereader); cachewriter.start(); } try { InetAddress mip = InetAddress.getLocalHost(); String myip = "" + mip.getHostAddress(); String sends = myip + ":" + LazyHomer.getSmithersPort() + ":" + LazyHomer.getPort() + ":" + LazyHomer.getRole(); CacheMulticastSender.send(sends, "INFO", "ALIVE"); } catch (Exception e) { System.out.println("Exception =" + e.getMessage()); } }
From source file:com.beginner.core.utils.ProjectUtil.java
/** * ?IP(???)//from w ww .ja v a 2 s . c om */ public static String getIp() { InetAddress addr = null; String ip; try { addr = InetAddress.getLocalHost(); } catch (UnknownHostException e) { logger.error("??IP??", e); } ip = addr.getHostAddress().toString(); return ip; }
From source file:cn.bc.web.util.WebUtils.java
/** * ?Server?:[0]-IP?,[1]-??,[2]-url/* ww w . j a v a 2 s.c om*/ * * @return */ public static String[] getServer(HttpServletRequest request) { String[] server = new String[] { null, null, null }; try { InetAddress localhost = InetAddress.getLocalHost(); server[0] = localhost.getHostAddress(); server[1] = localhost.getHostName(); } catch (UnknownHostException e) { server[0] = "unknown"; server[1] = "unknown"; } server[2] = request != null ? request.getRequestURL().toString() : "unknown"; return server; }