use of java.net.NetworkInterface in project screenbird by adamhub.
the class MediaUtil method getMacAddress.
/**
* Returns the MAC address for the current computer.
* @return
*/
public static String getMacAddress() {
StringBuilder macAddress = new StringBuilder();
try {
InetAddress address = InetAddress.getLocalHost();
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
byte[] mac = ni.getHardwareAddress();
for (int i = 0; i < mac.length; i++) {
macAddress.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "" : ""));
}
} catch (SocketException e) {
log(e);
} catch (UnknownHostException e) {
log(e);
}
return macAddress.toString();
}
use of java.net.NetworkInterface in project Openfire by igniterealtime.
the class LocalIPResolver method getLocalIP.
public static String getLocalIP() {
if (overrideIp != null && overrideIp.length() >= 7) {
return overrideIp;
}
Enumeration ifaces;
try {
ifaces = NetworkInterface.getNetworkInterfaces();
while (ifaces.hasMoreElements()) {
NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
Enumeration iaddresses = iface.getInetAddresses();
while (iaddresses.hasMoreElements()) {
InetAddress iaddress = (InetAddress) iaddresses.nextElement();
if (!iaddress.isLoopbackAddress() && !iaddress.isLinkLocalAddress() && !iaddress.isSiteLocalAddress()) {
return iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName();
}
}
}
ifaces = NetworkInterface.getNetworkInterfaces();
while (ifaces.hasMoreElements()) {
NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
Enumeration iaddresses = iface.getInetAddresses();
while (iaddresses.hasMoreElements()) {
InetAddress iaddress = (InetAddress) iaddresses.nextElement();
if (!iaddress.isLoopbackAddress() && !iaddress.isLinkLocalAddress()) {
return iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName();
}
}
}
return InetAddress.getLocalHost().getHostAddress() != null ? InetAddress.getLocalHost().getHostAddress() : InetAddress.getLocalHost().getHostName();
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return "127.0.0.1";
}
use of java.net.NetworkInterface in project ats-framework by Axway.
the class HostUtils method getIpAddressesList.
/**
* Return a list with addresses.
* if called with true, it will try to return the first IPv4 address or if there isn't such - the last IPv6 address
* if called with false, it will return a list with all IP addresses
*
* @return
*/
private static List<InetAddress> getIpAddressesList(boolean exitOnFirstIPv4) {
//list containing all ip addresses
List<InetAddress> ipList = new ArrayList<InetAddress>();
try {
// cycle all net interfaces
Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) netInterfaces.nextElement();
if (!netInterface.isLoopback()) {
// for each net interface cycle all IP addresses
Enumeration<InetAddress> ipAddresses = netInterface.getInetAddresses();
InetAddress ipAddress = null;
while (ipAddresses.hasMoreElements()) {
ipAddress = (InetAddress) ipAddresses.nextElement();
if (ipAddress instanceof java.net.Inet4Address) {
Inet4Address ipv4 = (Inet4Address) ipAddress;
if (!ipv4.isLoopbackAddress() && !LOCAL_HOST_NAME.equals(ipv4.getCanonicalHostName()) && !LOCAL_HOST_IPv4.equals(ipv4.getCanonicalHostName())) {
// we found an appropriate IPv4 address
ipList.add(ipv4);
if (exitOnFirstIPv4) {
break;
}
}
} else //if( ip instanceof java.net.Inet6Address )
{
Inet6Address ipv6 = (Inet6Address) ipAddress;
// FIXME: currently we do not filter out the temporary IPv6 addresses
if (!ipv6.isLinkLocalAddress() && !LOCAL_HOST_IPv6.equals(ipv6.getCanonicalHostName())) {
// We found an appropriate IPv6 address. Add it to the list, but keep searching for an appropriate IPv4 address.
ipList.add(ipv6);
}
}
}
}
}
} catch (SocketException se) {
log.error("Error obtaining the local host address", se);
}
/*
* We will return either list with the IP addresses or empty list
*/
if (exitOnFirstIPv4 && !ipList.isEmpty()) {
int listSize = ipList.size();
return ipList.subList(listSize - 1, listSize);
}
return ipList;
}
use of java.net.NetworkInterface in project ats-framework by Axway.
the class HostUtils method isLocalHost.
/**
* Tells if host is the local host. Note it will return true if:
* <ul>
* <li>host is null,</li>
* <li>empty string,</li>
* <li>IP of the local host including 127.0.0.1,</li>
* <li>the name of the local host including "localhost".</li>
* </ul>
* Otherwise it will return false.
*
* @param host
* the host to check
* @return if host is the local host
*/
public static boolean isLocalHost(String host) {
if (StringUtils.isNullOrEmpty(host)) {
// we assume a local host
return true;
} else if (localHosts.contains(host)) {
// we already know this is a local host
return true;
} else if (nonlocalHosts.contains(host)) {
// we already know this is not a local host
return false;
} else {
// unknown host, check if it is local or not
Enumeration<NetworkInterface> netInterfaces = null;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
// we hope this will never happen
log.error("Error obtaining info about this system's network interfaces. We will assume '" + host + "' is a local host", e);
return true;
}
while (netInterfaces.hasMoreElements()) {
NetworkInterface netInterface = netInterfaces.nextElement();
Enumeration<InetAddress> inetAddresses = netInterface.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
InetAddress inetAddress = inetAddresses.nextElement();
String hostAddress = inetAddress.getHostAddress();
if (inetAddress instanceof Inet6Address) {
if (hostAddress != null && stripIPv6InterfaceId(compressIPv6Address(host)).equalsIgnoreCase(stripIPv6InterfaceId(compressIPv6Address(hostAddress)))) {
localHosts.add(host);
return true;
}
} else if (host.equalsIgnoreCase(hostAddress)) {
localHosts.add(host);
return true;
}
String hostName = inetAddress.getHostName();
if (host.equalsIgnoreCase(hostName)) {
localHosts.add(host);
return true;
}
}
}
nonlocalHosts.add(host);
return false;
}
}
use of java.net.NetworkInterface in project CloudStack-archive by CloudStack-extras.
the class NetUtils method getAllLocalInetAddresses.
public static InetAddress[] getAllLocalInetAddresses() {
List<InetAddress> addrList = new ArrayList<InetAddress>();
try {
for (NetworkInterface ifc : IteratorUtil.enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) {
if (ifc.isUp() && !ifc.isVirtual()) {
for (InetAddress addr : IteratorUtil.enumerationAsIterable(ifc.getInetAddresses())) {
addrList.add(addr);
}
}
}
} catch (SocketException e) {
s_logger.warn("SocketException in getAllLocalInetAddresses().", e);
}
InetAddress[] addrs = new InetAddress[addrList.size()];
if (addrList.size() > 0) {
System.arraycopy(addrList.toArray(), 0, addrs, 0, addrList.size());
}
return addrs;
}
Aggregations