Java IP Address Get getInterfaceIPs()

Here you can find the source of getInterfaceIPs()

Description

Gets every IPv4 Address on each Interface except the loopback The Address format is ip/subnet

License

Open Source License

Return

The collected IPv4 Addresses

Declaration

public static TreeSet<String> getInterfaceIPs() 

Method Source Code

//package com.java2s;
/**//from   w w w.  ja v  a2 s.  c  om
 * Copyright (c) 2014-2016 openHAB UG (haftungsbeschraenkt) and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;

import java.util.Enumeration;
import java.util.Iterator;

import java.util.TreeSet;

public class Main {
    /**
     * Gets every IPv4 Address on each Interface except the loopback
     * The Address format is ip/subnet
     *
     * @return The collected IPv4 Addresses
     */
    public static TreeSet<String> getInterfaceIPs() {
        TreeSet<String> interfaceIPs = new TreeSet<String>();

        try {
            // For each interface ...
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                    .hasMoreElements();) {
                NetworkInterface networkInterface = en.nextElement();
                if (!networkInterface.isLoopback()) {

                    // .. and for each address ...
                    for (Iterator<InterfaceAddress> it = networkInterface.getInterfaceAddresses().iterator(); it
                            .hasNext();) {

                        // ... get IP and Subnet
                        InterfaceAddress interfaceAddress = it.next();
                        interfaceIPs.add(interfaceAddress.getAddress().getHostAddress() + "/"
                                + interfaceAddress.getNetworkPrefixLength());
                    }
                }
            }
        } catch (SocketException e) {
        }

        return interfaceIPs;
    }
}

Related

  1. getHostIPByHostName(String host)
  2. getInetIps()
  3. getIniFileInputStreamEclipse()
  4. getInputStreamFromZipFile( String zipFilePath, String resourcePath)
  5. getInterfaceAddress(final String ifaceName, final boolean ipV4)
  6. getInternalIp()
  7. getInternalIp()
  8. getInternetIpAddress()
  9. getIntfFromLabelAdva(int n, Inet4Address ip)