How to get network interfaces in Java

  • 05 March 2018
  • ADM

 

How to get network interfaces in Java - images/logos/java.jpg

 

Here is a snippet code how to get the local network interfaces list in Java.

Code

package com.adm;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

public class NetworkAddress {

    public static void getInterfaces() {
	try {
	    Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();

	    while (e.hasMoreElements()) {
		NetworkInterface ni = e.nextElement();
		System.out.println("Net interface: " + ni.getName() + " - " + ni.getDisplayName());

		Enumeration<InetAddress> e2 = ni.getInetAddresses();

		while (e2.hasMoreElements()) {
		    InetAddress ip = e2.nextElement();
		    System.out.println("IP address: " + ip.toString());
		}
		System.out.println();
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    public static void main(String[] args) {
	System.out.println("List all network interfaces example");
	System.out.println();
	getInterfaces();
    }
}

Output

List all network interfaces example

Net interface: lo - Software Loopback Interface 1
IP address: /127.0.0.1
IP address: /0:0:0:0:0:0:0:1

Net interface: net0 - WAN Miniport (SSTP)

Net interface: net1 - WAN Miniport (L2TP)

Net interface: net2 - WAN Miniport (PPTP)

Net interface: ppp0 - WAN Miniport (PPPOE)

Net interface: eth0 - WAN Miniport (IPv6)

Net interface: eth1 - WAN Miniport (Network Monitor)

Net interface: eth2 - WAN Miniport (IP)

Net interface: ppp1 - RAS Async Adapter

Net interface: net3 - WAN Miniport (IKEv2)

Net interface: eth3 - Intel(R) 82579LM Gigabit Network Connection
IP address: /100.100.60.61

Net interface: net4 - Microsoft ISATAP Adapter
IP address: /fe80:0:0:0:0:5efe:c0a8:7f01%net4

Net interface: eth4 - VMware Virtual Ethernet Adapter for VMnet1
IP address: /192.168.127.1
IP address: /fe80:0:0:0:21f3:d9e3:1624:a8ee%eth4

Net interface: net5 - Microsoft ISATAP Adapter #2
IP address: /fe80:0:0:0:0:5efe:c0a8:cc01%net5

Net interface: eth5 - VMware Virtual Ethernet Adapter for VMnet8
IP address: /192.168.204.1
IP address: /fe80:0:0:0:c9e9:e60e:ec1d:e071%eth5

Net interface: net6 - Microsoft ISATAP Adapter #3
IP address: /fe80:0:0:0:0:5efe:a0a:3c3d%net6

Net interface: net7 - Microsoft ISATAP Adapter #4
IP address: /fe80:0:0:0:0:5efe:c0a8:3801%net7

Net interface: eth6 - VirtualBox Host-Only Ethernet Adapter
IP address: /192.168.56.1
IP address: /fe80:0:0:0:9dfa:a392:b069:eb6f%eth6

Net interface: net8 - Microsoft ISATAP Adapter #5

Net interface: eth7 - Intel(R) 82579LM Gigabit Network Connection-VirtualBox NDIS Light-Weight Filter-0000

Net interface: eth8 - Intel(R) 82579LM Gigabit Network Connection-QoS Packet Scheduler-0000

Net interface: eth9 - Intel(R) 82579LM Gigabit Network Connection-WFP LightWeight Filter-0000

Net interface: eth10 - WAN Miniport (Network Monitor)-QoS Packet Scheduler-0000

Net interface: eth11 - WAN Miniport (IP)-QoS Packet Scheduler-0000

Net interface: eth12 - WAN Miniport (IPv6)-QoS Packet Scheduler-0000

Net interface: eth13 - VirtualBox Host-Only Ethernet Adapter-QoS Packet Scheduler-0000

Net interface: eth14 - VirtualBox Host-Only Ethernet Adapter-WFP LightWeight Filter-0000

As you can see the list is huge and only a few has an associated IP. My computer has Virtual Box and VMware installed.

You can identify the important interfaces:

  • 127.0.0.1 - lo - Software Loopback Interface 1
  • 100.100.60.61 - eth3 - Intel(R) 82579LM Gigabit Network Connection (this is connected to the internet)
  • 192.168.127.1 - eth4 - VMware Virtual Ethernet Adapter for VMnet1 IP address
  • 192.168.204.1 - eth5 - VMware Virtual Ethernet Adapter for VMnet8 IP address
  • 192.168.56.1 - eth6 - VirtualBox Host-Only Ethernet Adapter IP address

 

References