Saturday, October 5, 2024
Google search engine
HomeLanguagesJavaJava Program to Get System IP Address in Windows and Linux Machine

Java Program to Get System IP Address in Windows and Linux Machine

IP Address: An Internet Protocol address is a numerical label assigned to each device connected to a computer network that uses the Internet Protocol for communication. 

Packages used:

  1. io (input-output): This package provides for system input and output through data streams, serialization, and the file system.
  2. net (network): This package provides the classes for implementing networking applications.
  3. util (utility): It contains the collection framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes

Methods Used:

1. getInetAddresses()

Syntax:

public Enumeration getInetAddresses()

Return Type: It returns an Enumeration of InetAddress.

2. getInterfaceAddresses()

Syntax:

public List getInterfaceAddresses()

Return Type: It returns a list of java.net.InterfaceAddress instances.

Below is the implementation of the problem statement:

Java




// Java Program to Get System IP Address
// in Windows and Linux Machine
import static java.lang.System.out;
 
import java.io.*;
import java.net.*;
import java.util.*;
 
public class gfg {
    public static void main(String args[]) // main method
        throws SocketException
    {
        // fetching network interface
        Enumeration<NetworkInterface> nets
            = NetworkInterface.getNetworkInterfaces();
 
        for (NetworkInterface netint :
             Collections.list(nets))
            displayInterfaceInformation(netint);
    }
 
    // Display Internet Information method
    static void
    displayInterfaceInformation(NetworkInterface netint)
        throws SocketException
    {
        out.printf("Display name: %s\n",
                   netint.getDisplayName());
        out.printf("Name: %s\n", netint.getName());
        Enumeration<InetAddress> inetAddresses
            = netint.getInetAddresses();
        // Output System IP
        for (InetAddress inetAddress :
             Collections.list(inetAddresses)) {
            out.printf("System IP: %s\n", inetAddress);
        }
        out.printf("\n");
    }
}


Output

Display name: eth0
Name: eth0

Display name: lo
Name: lo
System IP: /127.0.0.1

Output in Windows: 

Output in Linux: 

 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments