programa Java para obtener la dirección IP del sistema en Windows o Linux Máquina

Obtener dirección IP de la máquina basada en Linux / Windows utilizando el programa Java : Este programa obtendrá e imprimir la dirección IP de las máquinas basadas en el sistema operativo Linux o Windows.

package com.includehelp;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

public class SystemIPAddress {

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;
}
}

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);
}
}

salida

System IP : 145.168.10.77


Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *