use of java.net.NetworkInterface in project cw-omnibus by commonsguy.
the class IPClipper method getLocalIPAddress.
public String getLocalIPAddress() throws SocketException {
Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
while (nics.hasMoreElements()) {
NetworkInterface intf = nics.nextElement();
Enumeration<InetAddress> addrs = intf.getInetAddresses();
while (addrs.hasMoreElements()) {
InetAddress addr = addrs.nextElement();
if (!addr.isLoopbackAddress()) {
return (addr.getHostAddress().toString());
}
}
}
return (null);
}
use of java.net.NetworkInterface in project deeplearning4j by deeplearning4j.
the class ExtraCounter method buildNetworkSnapshot.
public void buildNetworkSnapshot() {
try {
NetworkInformation netInfo = new NetworkInformation();
netInfo.setTotalMemory(Runtime.getRuntime().maxMemory());
netInfo.setAvailableMemory(Runtime.getRuntime().freeMemory());
String sparkIp = System.getenv("SPARK_PUBLIC_DNS");
if (sparkIp != null) {
// if spark ip is defined, we just use it, and don't bother with other interfaces
netInfo.addIpAddress(sparkIp);
} else {
// sparkIp wasn't defined, so we'll go for heuristics here
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface networkInterface : interfaces) {
if (networkInterface.isLoopback() || !networkInterface.isUp())
continue;
for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
String addr = address.getAddress().getHostAddress();
if (addr == null || addr.isEmpty() || addr.contains(":"))
continue;
netInfo.getIpAddresses().add(addr);
}
}
}
networkInformation.add(netInfo);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of java.net.NetworkInterface in project che by eclipse.
the class DefaultNetworkFinderHelperTest method checkFoundIpForABridge.
/**
* Check that we can find ipv4 address if we have some bridge
*
* @throws SocketException
*/
@Test
public void checkFoundIpForABridge() throws SocketException {
DefaultNetworkFinder networkFinder = new DefaultNetworkFinder();
Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface.getNetworkInterfaces();
while (enumNetworkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = enumNetworkInterfaces.nextElement();
Optional<InetAddress> foundIpAddress = networkFinder.getIPAddress(networkInterface.getName());
Enumeration<InetAddress> enumAddresses = networkInterface.getInetAddresses();
List<InetAddress> list = new ArrayList<>();
while (enumAddresses.hasMoreElements()) {
InetAddress inetAddress = enumAddresses.nextElement();
if (inetAddress instanceof Inet4Address) {
list.add(inetAddress);
}
}
if (list.size() > 0) {
assertTrue(foundIpAddress.isPresent());
assertTrue(list.contains(foundIpAddress.get()));
}
}
}
use of java.net.NetworkInterface in project elasticsearch by elastic.
the class IfConfig method doLogging.
/** perform actual logging: might throw exception if things go wrong */
private static void doLogging() throws IOException {
StringBuilder msg = new StringBuilder();
for (NetworkInterface nic : NetworkUtils.getInterfaces()) {
msg.append(System.lineSeparator());
// ordinary name
msg.append(nic.getName());
msg.append(System.lineSeparator());
// display name (e.g. on windows)
if (!nic.getName().equals(nic.getDisplayName())) {
msg.append(INDENT);
msg.append(nic.getDisplayName());
msg.append(System.lineSeparator());
}
// addresses: v4 first, then v6
List<InterfaceAddress> addresses = nic.getInterfaceAddresses();
for (InterfaceAddress address : addresses) {
if (address.getAddress() instanceof Inet6Address == false) {
msg.append(INDENT);
msg.append(formatAddress(address));
msg.append(System.lineSeparator());
}
}
for (InterfaceAddress address : addresses) {
if (address.getAddress() instanceof Inet6Address) {
msg.append(INDENT);
msg.append(formatAddress(address));
msg.append(System.lineSeparator());
}
}
// hardware address
byte[] hardware = nic.getHardwareAddress();
if (hardware != null) {
msg.append(INDENT);
msg.append("hardware ");
for (int i = 0; i < hardware.length; i++) {
if (i > 0) {
msg.append(":");
}
msg.append(String.format(Locale.ROOT, "%02X", hardware[i]));
}
msg.append(System.lineSeparator());
}
// attributes
msg.append(INDENT);
msg.append(formatFlags(nic));
msg.append(System.lineSeparator());
}
logger.debug("configuration:{}{}", System.lineSeparator(), msg);
}
use of java.net.NetworkInterface in project GT by Tencent.
the class GTCaptureActivity method startTcpDump.
// 最后调整实际的文件名和参数
private void startTcpDump(String filePath) {
String realParam = param;
// 如果是wifi,则适配为wlan0,对应小米等手机,其实大部分机型可用网卡名都是wlan0
if (NetUtils.isWifiActive()) {
try {
NetworkInterface network = NetworkInterface.getByName("wlan0");
if (network != null && !param.contains("wlan0")) {
realParam = "-i wlan0 " + param;
}
} catch (SocketException e) {
// nothing should do
}
}
GTCaptureEngine.getInstance().doCapture(filePath + "_" + String.valueOf(count) + ".pcap", realParam);
}
Aggregations