use of java.net.InterfaceAddress in project jmeter by apache.
the class ParallelResourcesAndIpSource method implementationsAndIps.
public static Iterable<Arguments> implementationsAndIps() throws SocketException {
List<Arguments> res = new ArrayList<>();
// Caveat: 127.0.0.1
InetAddress local4 = null;
InetAddress local6 = null;
List<InetAddress> localIps = Collections.list(NetworkInterface.getNetworkInterfaces()).stream().filter(networkInterface -> {
try {
return networkInterface.isLoopback();
} catch (SocketException e) {
return false;
}
}).flatMap(ni -> ni.getInterfaceAddresses().stream()).map(InterfaceAddress::getAddress).collect(Collectors.toList());
// We don't want to have accidental O(N^2) in case there are lots of source IPs
// So we use just two for the source IP
// So we shuffle them to use different ones every time
Collections.shuffle(localIps);
for (InetAddress localIp : localIps) {
if (local4 == null && localIp instanceof Inet4Address) {
local4 = localIp;
} else if (local6 == null && localIp instanceof Inet6Address) {
local6 = localIp;
}
}
// This is to allow the variables for use in lambda below (the vars should be effectively final)
InetAddress finalLocal4 = local4;
InetAddress finalLocal6 = local6;
// This is to make test names pretty
localIps.sort(Comparator.comparing(InetAddress::toString));
for (String impl : HTTPSamplerFactory.getImplementations()) {
for (String targetHost : new String[] { "127.0.0.1", "[::1]", "localhost" }) {
if ("localhost".equals(targetHost)) {
// with the given source IP
continue;
}
localIps.forEach(localIp -> {
if (finalLocal4 != null) {
res.add(arguments(impl, targetHost, finalLocal4));
}
if (finalLocal6 != null) {
res.add(arguments(impl, targetHost, finalLocal6));
}
});
}
}
return res;
}
use of java.net.InterfaceAddress in project cxf by apache.
the class WSDiscoveryClientTest method findIpv4Interface.
static NetworkInterface findIpv4Interface() throws Exception {
Enumeration<NetworkInterface> ifcs = NetworkInterface.getNetworkInterfaces();
List<NetworkInterface> possibles = new ArrayList<>();
if (ifcs != null) {
while (ifcs.hasMoreElements()) {
NetworkInterface ni = ifcs.nextElement();
if (ni.supportsMulticast() && ni.isUp()) {
for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
if (ia.getAddress() instanceof java.net.Inet4Address && !ia.getAddress().isLoopbackAddress() && !ni.getDisplayName().startsWith("vnic")) {
possibles.add(ni);
}
}
}
}
}
return possibles.isEmpty() ? null : possibles.get(possibles.size() - 1);
}
use of java.net.InterfaceAddress in project uavstack by uavorg.
the class HostNewworkInfo method getNetCardInfo.
/**
* @return {wlan0={ips={"192.168.1.109":{"bcast":"192.168.1.255","mask":"255.255.255.0"}}, mac=28:C6:3F:C4:29:23}}
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public Map getNetCardInfo() {
Map netCardInfos = new HashMap<String, Map<String, String>>();
Enumeration<NetworkInterface> netInterfaces;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = netInterfaces.nextElement();
// if (ni.isVirtual()) {
// continue;
// }
Map ipInfos = new HashMap<String, Map<String, String>>();
List<InterfaceAddress> addresses = ni.getInterfaceAddresses();
for (InterfaceAddress ia : addresses) {
if (ia.getAddress().isLoopbackAddress() || ia.getAddress().getHostAddress().indexOf(":") != -1) {
continue;
}
Map ipInfo = new HashMap<String, String>();
InetAddress bcast = ia.getBroadcast();
ipInfo.put("bcast", bcast == null ? "UNKNOWN" : bcast.getHostAddress());
ipInfo.put("mask", NetmaskLengthToNetmask(ia.getNetworkPrefixLength()));
ipInfos.put(ia.getAddress().getHostAddress(), ipInfo);
}
if (!ipInfos.isEmpty()) {
Map netCardInfo = new HashMap<String, Map<String, String>>();
netCardInfo.put("ips", JSONHelper.toString(ipInfos));
netCardInfo.put("mac", getMacAddressAsString(ni));
netCardInfos.put(ni.getName(), netCardInfo);
}
}
} catch (SocketException e) {
// ignore
}
return netCardInfos;
}
use of java.net.InterfaceAddress in project kcanotify by antest1.
the class Util method getNetworkInfo.
public static String getNetworkInfo(Context context) {
StringBuilder sb = new StringBuilder();
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ani = cm.getActiveNetworkInfo();
List<NetworkInfo> listNI = new ArrayList<>();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
listNI.addAll(Arrays.asList(cm.getAllNetworkInfo()));
else
for (Network network : cm.getAllNetworks()) {
NetworkInfo ni = cm.getNetworkInfo(network);
if (ni != null)
listNI.add(ni);
}
for (NetworkInfo ni : listNI) {
sb.append(ni.getTypeName()).append('/').append(ni.getSubtypeName()).append(' ').append(ni.getDetailedState()).append(TextUtils.isEmpty(ni.getExtraInfo()) ? "" : " " + ni.getExtraInfo()).append(ni.getType() == ConnectivityManager.TYPE_MOBILE ? " " + Util.getNetworkGeneration(ni.getSubtype()) : "").append(ni.isRoaming() ? " R" : "").append(ani != null && ni.getType() == ani.getType() && ni.getSubtype() == ani.getSubtype() ? " *" : "").append("\r\n");
}
try {
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
if (nis != null)
while (nis.hasMoreElements()) {
NetworkInterface ni = nis.nextElement();
if (ni != null && !"lo".equals(ni.getName())) {
List<InterfaceAddress> ias = ni.getInterfaceAddresses();
if (ias != null)
for (InterfaceAddress ia : ias) sb.append(ni.getName()).append(' ').append(ia.getAddress().getHostAddress()).append('/').append(ia.getNetworkPrefixLength()).append(' ').append(ni.getMTU()).append("\r\n");
}
}
} catch (Throwable ex) {
sb.append(ex.toString()).append("\r\n");
}
if (sb.length() > 2)
sb.setLength(sb.length() - 2);
return sb.toString();
}
use of java.net.InterfaceAddress in project wechat by dllwh.
the class SystemHelper method getHostIP.
/**
* @方法描述: 获取主机IP
* @return
*/
public static String getHostIP() {
String localip = "";
InetAddress ip = null;
List<InterfaceAddress> addressList = null;
try {
// 获得本机的所有网络接口
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
while (nets.hasMoreElements()) {
NetworkInterface netint = (NetworkInterface) nets.nextElement();
addressList = netint.getInterfaceAddresses();
for (InterfaceAddress interfaceAddress : addressList) {
ip = interfaceAddress.getAddress();
if (ip instanceof Inet4Address) {
// 只关心 IPv4 地址
if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {
localip = ip.getHostAddress();
} else {
localip += ip;
}
}
}
}
localip = localip.replaceAll("null", "");
} catch (Exception e) {
System.out.println("获取服务器IP出错");
}
return localip;
}
Aggregations