Java Utililty Methods Network Interface Get

List of utility methods to do Network Interface Get

Description

The list of methods to do Network Interface Get are organized into topic(s).

Method

StringgetCustomMACFormat(NetworkInterface inte)
This method returns the MAC address of Network Interface.
if (inte == null)
    throw new IllegalArgumentException("Network Interface can not be null.");
byte[] mac = inte.getHardwareAddress();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++)
    sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""));
return sb.toString();
NetworkInterfacegetDefaultInterface()
Returns the system's default interface.
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
    NetworkInterface inter = e.nextElement();
    if (inter.isUp() && !inter.isLoopback())
        return inter;
return null;
NetworkInterfacegetDefaultNetworkInterface()
Selects the default network interface
try {
    Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
    while (networks.hasMoreElements()) {
        NetworkInterface ni = networks.nextElement();
        Enumeration<InetAddress> addresses = ni.getInetAddresses();
        boolean hasAddress = false;
        while (addresses.hasMoreElements()) {
            InetAddress ia = addresses.nextElement();
...
byte[]getFirstMACAdress()
Returns the first MAC adress that is found in the system.
final Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
    final NetworkInterface network = networkInterfaces.nextElement();
    final byte[] bmac = network.getHardwareAddress();
    if (bmac != null) {
        return bmac;
throw new SocketException("No network interfaces with a MAC adress found.");
StringgetFreeTunnelInterface()
Return the first free tunnel interface.
try {
    Boolean free = false;
    int i;
    for (i = 0;; i++) {
        NetworkInterface interfaz;
        free = true;
        for (Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); interfaces
                .hasMoreElements();) {
...
InetAddress[]getGlobalI()
get Global I
final ArrayList<InetAddress> inetAddrList = new ArrayList();
Enumeration<NetworkInterface> eNetIf = NetworkInterface.getNetworkInterfaces();
while (eNetIf.hasMoreElements()) {
    Enumeration<InetAddress> eInetAddr = eNetIf.nextElement().getInetAddresses();
    while (eInetAddr.hasMoreElements()) {
        InetAddress inetAddr = eInetAddr.nextElement();
        if (!inetAddr.isAnyLocalAddress() && !inetAddr.isLinkLocalAddress() && !inetAddr.isLoopbackAddress()
                && !inetAddr.isMulticastAddress()) {
...
InetAddress[]getGlobalInterfaces()
Returns all global scope addresses for interfaces that are up.
List<InetAddress> list = new ArrayList<InetAddress>();
for (NetworkInterface intf : getInterfaces()) {
    if (intf.isUp()) {
        for (InetAddress address : Collections.list(intf.getInetAddresses())) {
            if (address.isLoopbackAddress() == false && address.isSiteLocalAddress() == false
                    && address.isLinkLocalAddress() == false) {
                list.add(address);
return list.toArray(new InetAddress[list.size()]);
ListgetGoodNetworkInterfaces()
get Good Network Interfaces
ImmutableList.Builder<NetworkInterface> builder = ImmutableList.builder();
try {
    for (NetworkInterface networkInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
        try {
            if (!networkInterface.isLoopback() && networkInterface.isUp()) {
                builder.add(networkInterface);
        } catch (Exception ignored) {
...
StringgetHostnameFromNetworkInterface()
Returns the host name determined from the network interfaces.
String result;
List<String> list;
Enumeration<NetworkInterface> enmI;
NetworkInterface intf;
Enumeration<InetAddress> enmA;
InetAddress addr;
boolean found;
result = null;
...
NetworkInterfacegetInterface(String ifaceName)
get Interface
if (ifaceName == null) {
    NetworkInterface iface = NetworkInterface.getByIndex(1);
    if (iface == null) {
        throw new IllegalArgumentException(
                "Supplied ANY address for endpoint: %s with no networkInterface defined, cannot find network interface 1 ");
    return iface;
} else {
...