Sunday, October 29, 2017
LAB 2 - IP AND MAC ADDRESS
package get.mac;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
public class GetMAC
{
public static void main(String[] args)
{
InetAddress ip;
try
{
ip = InetAddress.getLocalHost();
System.out.println("Current IP address : " + ip.getHostAddress());
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
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 | SocketException e)
{
}
}
}
OUTPUT
Current IP address : 10.18.44.51
Current MAC address : EC:F4:BB:9D:DC:D4
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.