本文整理汇总了Java中java.net.NetworkInterface.getByIndex方法的典型用法代码示例。如果您正苦于以下问题:Java NetworkInterface.getByIndex方法的具体用法?Java NetworkInterface.getByIndex怎么用?Java NetworkInterface.getByIndex使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.NetworkInterface
的用法示例。
在下文中一共展示了NetworkInterface.getByIndex方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getMacAddress
import java.net.NetworkInterface; //导入方法依赖的package包/类
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: getInterfaceIp
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* 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;
}
示例3: getInterfaceIp
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* 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;
}
示例4: getInterfaceIp
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* 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;
}
示例5: getLinkLocalAddress
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* 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;
}
示例6: getInterfaceMac
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* 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;
}
示例7: getHWSeq
import java.net.NetworkInterface; //导入方法依赖的package包/类
static private long getHWSeq()
{
if (hwseq != 0)
return hwseq;
for (int ii = 0; ii < 50; ii++)
{
try
{
NetworkInterface ni = NetworkInterface.getByIndex(ii);
if (ni != null) // real interface
{
byte[] hwaddr = ni.getHardwareAddress();
if (hwaddr == null) continue;
String dname = ni.getDisplayName();
if (dname.startsWith("Microsoft")) continue;
if (dname.startsWith("VMware")) continue;
if (dname.startsWith("VirtualBox")) continue;
if (dname.contains("Tunneling")) continue;
// HW will be somewhat unique, we use the above to skip things that generally are not
hwseq = (VARIANT1 & VARIANT_MASK)| (new Random().nextLong() & CLKSEQ_MASK) | (new BigInteger(1, hwaddr).longValue() & NODE_MASK);
break;
}
}
catch (SocketException se) {}
}
return hwseq;
}
示例8: findIPv6Interface
import java.net.NetworkInterface; //导入方法依赖的package包/类
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;
}