Search in sources :

Example 11 with NetworkInterface

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);
}
Also used : NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 12 with NetworkInterface

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);
    }
}
Also used : InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface)

Example 13 with NetworkInterface

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()));
        }
    }
}
Also used : Inet4Address(java.net.Inet4Address) ArrayList(java.util.ArrayList) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress) Test(org.testng.annotations.Test)

Example 14 with NetworkInterface

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);
}
Also used : InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address)

Example 15 with NetworkInterface

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);
}
Also used : SocketException(java.net.SocketException) NetworkInterface(java.net.NetworkInterface)

Aggregations

NetworkInterface (java.net.NetworkInterface)225 InetAddress (java.net.InetAddress)178 SocketException (java.net.SocketException)112 ArrayList (java.util.ArrayList)42 Inet4Address (java.net.Inet4Address)39 UnknownHostException (java.net.UnknownHostException)36 InterfaceAddress (java.net.InterfaceAddress)32 Inet6Address (java.net.Inet6Address)29 IOException (java.io.IOException)25 Enumeration (java.util.Enumeration)9 InetSocketAddress (java.net.InetSocketAddress)7 HashSet (java.util.HashSet)7 LinkedHashSet (java.util.LinkedHashSet)7 Command (com.android.server.NativeDaemonConnector.Command)6 Test (org.junit.Test)6 DatagramSocket (java.net.DatagramSocket)5 File (java.io.File)4 MulticastSocket (java.net.MulticastSocket)4 List (java.util.List)4 SharedPreferences (android.content.SharedPreferences)3