Re: How to get MAC address for local machine?

Discussion in 'Java' started by Phil Hanna, Jul 1, 2003.

  1. Phil Hanna

    Phil Hanna Guest

    On Mon, 23 Jun 2003 00:53:21 +0200, Jezuch <> wrote:

    >U¿ytkownik Shripathi Kamath napisa³:
    >> Which is why I said "not necessarily". He might want to run it only on a
    >> variety of platforms on which the code *may* be the same.
    >>
    >> Besides, I addressed that in the rest of the post you snipped away.

    >
    >Oh... Sorry about that :)
    >This "not necessarily" seemed to be referring to "Sounds like you need some
    >JNI and platform specific code".
    >--
    >Ecce Jezuch
    >"If I was the king of all the rotten, stealing filth from those forgotten,
    >Would you be my queen?" - P. Keenan
    >

    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;
    }
    }
    --
    Phil Hanna
    Author of JSP 2.0: The Complete Reference
    http://www.philhanna.com
    http://www.philhanna.com
    Phil Hanna, Jul 1, 2003
    #1
    1. Advertising

  2. Phil Hanna

    Roedy Green Guest

    On Mon, 30 Jun 2003 23:24:48 GMT, (Phil Hanna) wrote
    or quoted :

    >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.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jul 1, 2003
    #2
    1. Advertising

  3. Phil Hanna

    Rene Guest

    Roedy Green <> wrote:
    > On Mon, 30 Jun 2003 23:24:48 GMT, (Phil Hanna) wrote
    > or quoted :
    >
    > >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.


    Maybe http://members.cox.net/preston.bannister/software/Win32_GetMAC.html
    helps you.

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service New Rate! $9.95/Month 50GB
    Rene, Jul 1, 2003
    #3
  4. Phil Hanna

    Rene Guest

    Roedy Green <> wrote:
    > On Mon, 30 Jun 2003 23:24:48 GMT, (Phil Hanna) wrote
    > or quoted :
    >
    > >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.


    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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service New Rate! $9.95/Month 50GB
    Rene, Jul 1, 2003
    #4
  5. Phil Hanna

    Rene Guest

    Roedy Green <> wrote:
    > On 01 Jul 2003 17:28:45 GMT, Rene <> wrote or quoted
    > :
    >
    > >Best solution seems to be here:
    > >http://www.codeguru.com/network/GetMAC.html

    >
    > This is the technique the recommends. The catch is when I went to
    > implement it, I discovered my Borland C++ v 5 does not have the
    > necessary include libraries to define IP_ADAPTER_INFO


    Does
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iphlp/iphl
    p/ip_adapter_info.asp help?

    CU

    Rene

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service New Rate! $9.95/Month 50GB
    Rene, Jul 3, 2003
    #5
  6. Phil Hanna

    Roedy Green Guest

    On 03 Jul 2003 19:30:11 GMT, Rene <> wrote or quoted
    :

    >
    >http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iphlp/iphl
    >p/ip_adapter_info.asp help?


    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.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jul 5, 2003
    #6
  7. Phil Hanna

    Roedy Green Guest

    Roedy Green, Jul 5, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. UJ
    Replies:
    4
    Views:
    705
    John Rivers
    Sep 2, 2005
  2. SJ

    Local machine's MAC address?

    SJ, Mar 8, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    2,728
    Eliyahu Goldin
    Mar 9, 2006
  3. JAVA Rookie
    Replies:
    4
    Views:
    492
    Tor Iver Wilhelmsen
    Dec 11, 2003
  4. Daniel Crespo

    How to get the local mac address?

    Daniel Crespo, Dec 14, 2005, in forum: Python
    Replies:
    24
    Views:
    1,689
    Daniel Crespo
    Dec 20, 2005
  5. SJ

    Local machine's MAC address

    SJ, Mar 8, 2006, in forum: ASP .Net Security
    Replies:
    1
    Views:
    149
    Dominick Baier [DevelopMentor]
    Mar 8, 2006
Loading...

Share This Page