java.net.NetworkInterface#getByIndex ( )源码实例Demo

下面列出了java.net.NetworkInterface#getByIndex ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: RISE-V2G   文件: MiscUtils.java
public static byte[] getMacAddress() {
	String networkInterfaceConfig = getPropertyValue("network.interface").toString();
	NetworkInterface nif = null;
	byte[] macAddress = null;
	
	try {
		if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) {
			nif = NetworkInterface.getByIndex(Integer.parseInt(networkInterfaceConfig));
		} else {
			nif = NetworkInterface.getByName(networkInterfaceConfig);
		}
		macAddress = nif.getHardwareAddress();
	} catch (SocketException e) {
		getLogger().error("Failed to retrieve local mac address (SocketException)", e);
	}
	
	return macAddress;
}
 
源代码2 项目: phoebus   文件: MQTT_PVConn.java
private void generateClientID()
{
    try
    {
        NetworkInterface nwi = NetworkInterface.getByIndex(0);
        byte mac[] = nwi.getHardwareAddress();
        clientID = String.valueOf(mac) + String.valueOf(randInt);
    }
    catch(Exception e)
    {
        try {
            InetAddress address = InetAddress.getLocalHost();
            clientID = address.getCanonicalHostName() + String.valueOf(randInt);
        }
        catch(Exception e2)
        {
            clientID = String.valueOf(randInt);
        }
    }

    //System MAC address (or hostname) + random integer + object hash... hopefully unique?
    clientID += "-" + System.identityHashCode(this);
}
 
源代码3 项目: onos   文件: OspfConfigUtil.java
/**
 * Returns interface IP by index.
 *
 * @param interfaceIndex interface index
 * @return interface IP by index
 */
private static Ip4Address getInterfaceIp(int interfaceIndex) {
    Ip4Address ipAddress = null;
    try {
        NetworkInterface networkInterface = NetworkInterface.getByIndex(interfaceIndex);
        Enumeration ipAddresses = networkInterface.getInetAddresses();
        while (ipAddresses.hasMoreElements()) {
            InetAddress address = (InetAddress) ipAddresses.nextElement();
            if (!address.isLinkLocalAddress()) {
                ipAddress = Ip4Address.valueOf(address.getAddress());
                break;
            }
        }
    } catch (Exception e) {
        log.debug("Error while getting Interface IP by index");
        return OspfUtil.DEFAULTIP;
    }
    return ipAddress;
}
 
源代码4 项目: onos   文件: Controller.java
/**
 * Returns interface IP by index.
 *
 * @param interfaceIndex interface index
 * @return interface IP by index
 */
private Ip4Address getInterfaceIp(int interfaceIndex) {
    Ip4Address ipAddress = null;
    try {
        NetworkInterface networkInterface = NetworkInterface.getByIndex(interfaceIndex);
        Enumeration ipAddresses = networkInterface.getInetAddresses();
        while (ipAddresses.hasMoreElements()) {
            InetAddress address = (InetAddress) ipAddresses.nextElement();
            if (!address.isLinkLocalAddress()) {
                ipAddress = Ip4Address.valueOf(address.getAddress());
                break;
            }
        }
    } catch (Exception e) {
        log.debug("Error while getting Interface IP by index");
        return OspfUtil.DEFAULTIP;
    }

    return ipAddress;
}
 
源代码5 项目: onos   文件: Controller.java
/**
 * Returns interface IP by index.
 *
 * @param interfaceIndex interface index
 * @return interface IP by index
 */
private Ip4Address getInterfaceIp(int interfaceIndex) {
    Ip4Address ipAddress = null;
    try {
        NetworkInterface networkInterface = NetworkInterface.getByIndex(interfaceIndex);
        Enumeration ipAddresses = networkInterface.getInetAddresses();
        while (ipAddresses.hasMoreElements()) {
            InetAddress address = (InetAddress) ipAddresses.nextElement();
            if (!address.isLinkLocalAddress()) {
                ipAddress = Ip4Address.valueOf(address.getAddress());
                break;
            }
        }
    } catch (Exception e) {
        log.debug("Error while getting Interface IP by index");
        return IsisConstants.DEFAULTIP;
    }
    return ipAddress;
}
 
源代码6 项目: RISE-V2G   文件: MiscUtils.java
/**
 * Determines the link-local IPv6 address which is configured on the network interface provided
 * in the properties file.
 * @return The link-local address given as a String
 */
public static Inet6Address getLinkLocalAddress() {
	String networkInterfaceConfig = getPropertyValue("network.interface").toString();
	
	NetworkInterface nif = null;
	
	try {
		if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) {
			nif = NetworkInterface.getByIndex(Integer.parseInt(networkInterfaceConfig));
		} else {
			nif = NetworkInterface.getByName(networkInterfaceConfig);
		}
		Enumeration<InetAddress> inetAddresses = nif.getInetAddresses();
		
		while (inetAddresses.hasMoreElements()) {
			InetAddress inetAddress = inetAddresses.nextElement();
			
			if (inetAddress.getClass() == Inet6Address.class && inetAddress.isLinkLocalAddress()) {
				return (Inet6Address) inetAddress;
			}
		}
		
		getLogger().fatal("No IPv6 link-local address found on the network interface '" +
				nif.getDisplayName() + "' configured in the properties file");
	} catch (SocketException e) {
		getLogger().fatal("SocketException while trying to get network interface for configured name " +
						  networkInterfaceConfig + "'", e); 
	} catch (NullPointerException | NumberFormatException e2) {
		getLogger().fatal("No network interface for configured network interface index '" + 
						  networkInterfaceConfig + "' found");
	}
	
	return null;
}
 
源代码7 项目: onos   文件: Controller.java
/**
 * Returns interface MAC by index.
 *
 * @param interfaceIndex interface index
 * @return interface IP by index
 */
private MacAddress getInterfaceMac(int interfaceIndex) {
    MacAddress macAddress = null;
    try {
        NetworkInterface networkInterface = NetworkInterface.getByIndex(interfaceIndex);
        macAddress = MacAddress.valueOf(networkInterface.getHardwareAddress());
    } catch (Exception e) {
        log.debug("Error while getting Interface IP by index");
        return macAddress;
    }

    return macAddress;
}
 
private NetworkInterface findIPv6Interface(Inet6Address address) throws IOException {
    if(blacklisted.isEmpty()) {
        if(log.isDebugEnabled()) {
            log.debug("Ignore IP6 default network interface setup with empty blacklist");
        }
        return null;
    }
    if(address.getScopeId() != 0) {
        if(log.isDebugEnabled()) {
            log.debug(String.format("Ignore IP6 default network interface setup for address with scope identifier %d", address.getScopeId()));
        }
        return null;
    }
    // If we find an interface name en0 that supports IPv6 make it the default.
    // We must use the index of the network interface. Referencing the interface by name will still
    // set the scope id to '0' referencing the awdl0 interface that is first in the list of enumerated
    // network interfaces instead of its correct index in <code>java.net.Inet6Address</code>
    // Use private API to defer the numeric format for the address
    List<Integer> indexes = new ArrayList<Integer>();
    final Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
    while(enumeration.hasMoreElements()) {
        indexes.add(enumeration.nextElement().getIndex());
    }
    for(Integer index : indexes) {
        final NetworkInterface n = NetworkInterface.getByIndex(index);
        if(log.isDebugEnabled()) {
            log.debug(String.format("Evaluate interface with %s index %d", n, index));
        }
        if(!n.isUp()) {
            if(log.isDebugEnabled()) {
                log.debug(String.format("Ignore interface %s not up", n));
            }
            continue;
        }
        if(blacklisted.contains(n.getName())) {
            log.warn(String.format("Ignore network interface %s disabled with blacklist", n));
            continue;
        }
        for(InterfaceAddress i : n.getInterfaceAddresses()) {
            if(i.getAddress() instanceof Inet6Address) {
                if(log.isInfoEnabled()) {
                    log.info(String.format("Selected network interface %s", n));
                }
                return n;
            }
        }
        log.warn(String.format("No IPv6 for interface %s", n));
    }
    log.warn("No network interface found for IPv6");
    return null;
}