Get all the IP Addresses to Which a Device is Exposed

Get all the IP Addresses to Which a Device is Exposed

At times, your device can be exposed using multiple interface cards. The following code sample will help identify various interfaces and the related IP configuration available on them.

import*;import java.util.*;public class NetworkInterfaceIPs {   public static void main(String args[])   {      NetworkInterfaceIPs networkInterfaceIPs = new NetworkInterfaceIPs();      networkInterfaceIPs.proceed();   }   private void proceed()    {      try      {         Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces();         InetAddress inetAddress = null;         System.out.println("Available IPs in NetworkInterface.");         while (networkInterfaces.hasMoreElements()) {                NetworkInterface networkInterface = (NetworkInterface) networkInterfaces.nextElement();                Enumeration inetAddresses = networkInterface.getInetAddresses();                            while (inetAddresses.hasMoreElements()) {                    inetAddress = inetAddresses.nextElement();                    if (inetAddress instanceof Inet4Address) { //Working on only IPv4 IPs                  System.out.println("IP: " + inetAddress.getHostAddress());                    }                               }            }               }catch(SocketException se)      {         System.out.println("SocketException: " + se);      }   }}/*

Expected output:

[root@mypc]# java NetworkInterfaceIPsAvailable IPs in NetworkInterface.IP:*/ 

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist