Home » Java programs » Most popular & searches Java programs

Java program to get system IP Address in Windows or Linux Machine

Get IP Address of Linux/Windows based Machine using Java program: This program will get and print the IP Address of Linux or Windows operating system based machines.

package com.includehelp;

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

/**
 * Program to get system IP Address in Windows or Linux Machine
 * @author includehelp
 */
public class SystemIPAddress {
    /**
     * Method for Get System IP Address
     * @return IP Address
     */
    public static String getSystemIP(){
        try{
            String sysIP="";
            String OSName=  System.getProperty("os.name");
	    if(OSName.contains("Windows")){
                sysIP   =InetAddress.getLocalHost().getHostAddress();
	    }
	    else{
	    	sysIP=getSystemIP4Linux("eth0");
	    	if(sysIP==null){
                    sysIP=getSystemIP4Linux("eth1");
		    if(sysIP==null){
		  	sysIP=getSystemIP4Linux("eth2");
                        if(sysIP==null){
                            sysIP=getSystemIP4Linux("usb0");
                        }
                    }
	   	}
	    }
	    return sysIP;
	}
	catch(Exception E){
            System.err.println("System IP Exp : "+E.getMessage());
            return null;
	}
    }
    
    /**
     * method for get IP of linux System
     * @param name
     * @return 
     */
    private static String getSystemIP4Linux(String name){
        try{
            String ip="";
            NetworkInterface networkInterface = NetworkInterface.getByName(name);
            Enumeration<InetAddress> inetAddress = networkInterface.getInetAddresses();
            InetAddress currentAddress = inetAddress.nextElement();
            while(inetAddress.hasMoreElements()){
                currentAddress = inetAddress.nextElement();
                if(currentAddress instanceof Inet4Address && !currentAddress.isLoopbackAddress()){
                    ip = currentAddress.toString();
                    break;
                }
            }
            if(ip.startsWith("/")){
                ip=ip.substring(1);
            }
            return ip;
        } 
        catch (Exception E) {
            System.err.println("System Linux IP Exp : "+E.getMessage());
            return null;
        }
    }
    
    public static void main(String[] args) {
        String ipAddress = getSystemIP();
        System.out.println("System IP : "+ipAddress);
    }
}

Output

System IP : 145.168.10.77





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.