Created
August 23, 2022 10:11
-
-
Save Urunov/a762301358bd0a148e062696b29bd0ed to your computer and use it in GitHub Desktop.
GET MAC Address and IP address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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