In JDK 1.6 a new method is added in the java.net.NetworkInterface class, this method is getHardwareAddress().
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
public class PrintMacAdress {
public static void main(String[] args){
try {
InetAddress address = InetAddress.getLocalHost();
//InetAddress address = InetAddress.getByName("www.google.co.in");
System.out.println("Current IP address : "+address.getHostAddress());
NetworkInterface network = NetworkInterface.getByInetAddress(address);
byte[] mac = network.getHardwareAddress();
System.out.print("Current MAC address : ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s",mac[i],(i<mac.length-1)?"-":""));
}
System.out.println(sb.toString());
} catch(UnknownHostException e) {
} catch(SocketException e){
}
}
}
Output:
Current IP address : 192.168.230.215
Current MAC address : D8-CB-8A-67-8B-3F
No comments:
Post a Comment