Java IP Address Get getIpAddressesFromNic()

Here you can find the source of getIpAddressesFromNic()

Description

Tries to retrieve the IP address from the NIC.

License

Apache License

Return

null if not found

Declaration

public static InetAddress getIpAddressesFromNic() 

Method Source Code

//package com.java2s;
/*//w  w w . j  a  v a 2s .c  o m
 * Copyright 2015 dorkbox, llc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.net.*;
import java.util.Enumeration;

public class Main {
    /**
     * Tries to retrieve the IP address from the NIC. Order is ETHx -> EMx -> WLANx
     *
     * @return null if not found
     */
    public static InetAddress getIpAddressesFromNic() {
        try {
            Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
            while (nets.hasMoreElements()) {
                NetworkInterface nextElement = nets.nextElement();
                String name = nextElement.getName();

                // we only want to use ethX addresses!
                if (name.startsWith("eth")) {
                    Enumeration<InetAddress> inetAddresses = nextElement.getInetAddresses();

                    while (inetAddresses.hasMoreElements()) {
                        InetAddress address = inetAddresses.nextElement();
                        // we only support IPV4 addresses.
                        if (!address.isLoopbackAddress() && address instanceof Inet4Address) {
                            return address;
                        }
                    }
                }
            }

            // it didn't work with ethX, so try emX!
            while (nets.hasMoreElements()) {
                nets = NetworkInterface.getNetworkInterfaces();
                NetworkInterface nextElement = nets.nextElement();
                String name = nextElement.getName();

                // we only want to use emX addresses!
                if (name.startsWith("em")) {
                    Enumeration<InetAddress> inetAddresses = nextElement.getInetAddresses();

                    while (inetAddresses.hasMoreElements()) {
                        InetAddress address = inetAddresses.nextElement();
                        // we only support IPV4 addresses.
                        if (!address.isLoopbackAddress() && address instanceof Inet4Address) {
                            return address;
                        }
                    }
                }
            }

            // it didn't work with emX, so try enX!
            while (nets.hasMoreElements()) {
                nets = NetworkInterface.getNetworkInterfaces();
                NetworkInterface nextElement = nets.nextElement();
                String name = nextElement.getName();

                // we only want to use enX addresses!
                if (name.startsWith("en")) {
                    Enumeration<InetAddress> inetAddresses = nextElement.getInetAddresses();

                    while (inetAddresses.hasMoreElements()) {
                        InetAddress address = inetAddresses.nextElement();
                        // we only support IPV4 addresses.
                        if (!address.isLoopbackAddress() && address instanceof Inet4Address) {
                            return address;
                        }
                    }
                }
            }

            // it didn't work with ethX, so try wifi!
            while (nets.hasMoreElements()) {
                nets = NetworkInterface.getNetworkInterfaces();
                NetworkInterface nextElement = nets.nextElement();
                String name = nextElement.getName();

                // we only want to use wlanX addresses!
                if (name.startsWith("wlan")) {
                    Enumeration<InetAddress> inetAddresses = nextElement.getInetAddresses();

                    while (inetAddresses.hasMoreElements()) {
                        InetAddress address = inetAddresses.nextElement();
                        // we only support IPV4 addresses.
                        if (!address.isLoopbackAddress() && address instanceof Inet4Address) {
                            return address;
                        }
                    }
                }
            }
        } catch (SocketException ignored) {
        }

        return null;
    }
}

Related

  1. getIpAddress(String server)
  2. getIpAddresses()
  3. getIPAddresses()
  4. getIPAddresses()
  5. getIpAddresses()
  6. getIpAddressExternal()
  7. getIPAddressFromBytes(byte[] bytes)
  8. getIPAddressFromNetworkInterface(String ifaceName)
  9. getIPAddressFromNetworkInterfaces()