forked from Ishaan28malik/Hacktoberfest-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Ishaan28malik#611 from suraj7086/master
Finding MAC Address
- Loading branch information
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import java.net.UnknownHostException; | ||
import java.net.InetAddress; | ||
import java.net.NetworkInterface; | ||
|
||
public class Mac_Address { | ||
public static void main(String[] args) throws UnknownHostException { | ||
InetAddress ip; | ||
InetAddress addr = InetAddress.getLocalHost(); | ||
try { | ||
String hostname = addr.getHostName(); | ||
System.out.println("Host Name : " + hostname); | ||
|
||
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 (Exception e) { | ||
|
||
e.printStackTrace(); | ||
|
||
} | ||
|
||
} | ||
} | ||
|
||
|
||
|