Re: How to get MAC address for local machine?

P

Phil Hanna

U¿ytkownik Shripathi Kamath napisa³:

Oh... Sorry about that :)
This "not necessarily" seemed to be referring to "Sounds like you need some
JNI and platform specific code".
Operating system specific, of course, but this works for Windows NT:

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.regex.*;

public class GetMac
{
public static void main(String[] args)
throws IOException
{
String address = new GetMac().getMacAddress();
System.out.println(address);
}

public String getMacAddress() throws IOException
{
String macAddress = null;
String command = "ipconfig /all";
Process pid = Runtime.getRuntime().exec(command);
BufferedReader in =
new BufferedReader(
new InputStreamReader(pid.getInputStream()));
while (true) {
String line = in.readLine();
if (line == null)
break;
Pattern p = Pattern.compile(".*Physical Address.*: (.*)");
Matcher m = p.matcher(line);
if (m.matches()) {
macAddress = m.group(1);
break;
}
}
in.close();
return macAddress;
}
}
 
R

Roedy Green

Pattern p = Pattern.compile(".*Physical Address.*: (.*)");

I did a look for a Windows api call to find the MAC but came up blank.
If I could find one, it would be pretty easy to do a JNI which would
be faster that the exec method, and would not be subject to IPCONFIG
wording changes.
 
R

Rene

Roedy Green said:
I did a look for a Windows api call to find the MAC but came up blank.
If I could find one, it would be pretty easy to do a JNI which would
be faster that the exec method, and would not be subject to IPCONFIG
wording changes.

Bah me again.

Best solution seems to be here:
http://www.codeguru.com/network/GetMAC.html

(method number three) since apparently Microsoft stopped using the MAC
address starting with Windows 2000 in its UUIDs.

Btw, the pages were found using google search with: "win32 api call get mac
address"

HTH.

CU

Rene
 
R

Roedy Green


It would help, but that just defines many more types. I also need the
prototypes for the methods too. Perhaps all this could be gleaned
from docs and recreated. The proper way is to find a compiler with
these *.h files built in or a downloadable package with the all
included.

I was only prepared to dedicate a few hours to the project, not days,
since I have no need of the result myself and I don't think it has any
commercial potential.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top