This code will print your workstations name and IP address:
Sample output:public class NetInfo { public static void main(String[] args) { new NetInfo().say(); } public void say() { try { java.net.InetAddress i = java.net.InetAddress.getLocalHost(); System.out.println(i); // name and IP address System.out.println(i.getHostName()); // name System.out.println(i.getHostAddress()); // IP address only } catch(Exception e){e.printStackTrace();} } }
homepc/10.210.65.8 homepc 10.210.65.8
This code will list the interfaces available on a workstation:
Sample output:import java.net.*; import java.util.*; import java.io.*; import java.nio.*; public class IPAddress { public void getInterfaces (){ try { Enumeration e = NetworkInterface.getNetworkInterfaces(); while(e.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) e.nextElement(); System.out.println("Net interface: "+ni.getName()); Enumeration e2 = ni.getInetAddresses(); while (e2.hasMoreElements()){ InetAddress ip = (InetAddress) e2.nextElement(); System.out.println("IP address: "+ ip.toString()); } } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { IPAddress ip = new IPAddress(); ip.getInterfaces(); } }
Net interface: lo IP address: /127.0.0.1 Net interface: eth0 IP address: /10.210.62.4