Skip to content

Instantly share code, notes, and snippets.

@Urunov
Created August 23, 2022 10:11
Show Gist options
  • Save Urunov/a762301358bd0a148e062696b29bd0ed to your computer and use it in GitHub Desktop.
Save Urunov/a762301358bd0a148e062696b29bd0ed to your computer and use it in GitHub Desktop.
GET MAC Address and IP address
package org.example;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
/**
* @project: ${MAC_IP}
* @Date: ${2022}
* @author: ${Urunov}
**/
public class MacAndIP {
public static void main(String[] args) throws SocketException, UnknownHostException {
// System.out.println("Hello world!");
Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
NetworkInterface inter;
System.out.println("MAC address: ");
while (networks.hasMoreElements()) {
inter = networks.nextElement();
byte[] mac = inter.getHardwareAddress();
if (mac != null) {
for (int i = 0; i < mac.length; i++) {
System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
}
System.out.println("");
}
}
System.out.println("IP address: ");
Enumeration e = NetworkInterface.getNetworkInterfaces();
while(e.hasMoreElements())
{
NetworkInterface n = (NetworkInterface) e.nextElement();
Enumeration ee = n.getInetAddresses();
while (ee.hasMoreElements())
{
InetAddress i = (InetAddress) ee.nextElement();
System.out.println(i.getHostAddress());
}
}
InetAddress IP=InetAddress.getLocalHost();
System.out.println("IP of my system is := "+IP.getHostAddress());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment