Getting Local MAC Address

Discussion in 'Python' started by Booter, Apr 2, 2010.

  1. Booter

    Booter Guest

    Hello all,

    I am new to python ans was wondering if there was a way to get the mac
    address from the local NIC?

    Thanks for your help.

    Gerad
    Booter, Apr 2, 2010
    #1
    1. Advertising

  2. Booter

    Guest

    On Apr 2, 2:14 pm, Booter <> wrote:
    > Hello all,
    >
    > I am new to python ans was wondering if there was a way to get the mac
    > address from the local NIC?
    >
    > Thanks for your help.
    >
    > Gerad


    for windows parse p.stdout.read():

    import subprocess

    p = subprocess.Popen('ipconfig', shell = True, stdout =
    subprocess.PIPE)

    p.wait()

    print p.stdout.read()
    , Apr 2, 2010
    #2
    1. Advertising

  3. Booter

    Guest

    On Apr 2, 2:52 pm, "" <>
    wrote:
    > On Apr 2, 2:14 pm, Booter <> wrote:
    >
    > > Hello all,

    >
    > > I am new to python ans was wondering if there was a way to get the mac
    > > address from the local NIC?

    >
    > > Thanks for your help.

    >
    > > Gerad

    >
    > for windows parse p.stdout.read():
    >
    > import subprocess
    >
    > p = subprocess.Popen('ipconfig', shell = True, stdout =
    > subprocess.PIPE)
    >
    > p.wait()
    >
    > print p.stdout.read()


    sorry, posted too soon. looks like this is for ip address only.
    , Apr 2, 2010
    #3
  4. Booter

    Guest

    On Apr 2, 2:52 pm, "" <>
    wrote:
    > On Apr 2, 2:14 pm, Booter <> wrote:
    >
    > > Hello all,

    >
    > > I am new to python ans was wondering if there was a way to get the mac
    > > address from the local NIC?

    >
    > > Thanks for your help.

    >
    > > Gerad

    >
    > for windows parse p.stdout.read():
    >
    > import subprocess
    >
    > p = subprocess.Popen('ipconfig', shell = True, stdout =
    > subprocess.PIPE)
    >
    > p.wait()
    >
    > print p.stdout.read()


    try this instead:

    import subprocess

    p = subprocess.Popen('ipconfig /all', shell = True, stdout =
    subprocess.PIPE)

    p.wait()

    print p.stdout.read()
    , Apr 2, 2010
    #4
  5. On 2-4-2010 22:55, wrote:
    > On Apr 2, 2:52 pm, ""<>
    > wrote:
    >> On Apr 2, 2:14 pm, Booter<> wrote:
    >>
    >>> Hello all,

    >>
    >>> I am new to python ans was wondering if there was a way to get the mac
    >>> address from the local NIC?

    >>
    >>> Thanks for your help.

    >>
    >>> Gerad

    >>
    >> for windows parse p.stdout.read():
    >>
    >> import subprocess
    >>
    >> p = subprocess.Popen('ipconfig', shell = True, stdout =
    >> subprocess.PIPE)
    >>
    >> p.wait()
    >>
    >> print p.stdout.read()

    >
    > sorry, posted too soon. looks like this is for ip address only.


    Actually you can get more info including the MAC address when you pass
    the /all switch.

    -irmen
    Irmen de Jong, Apr 2, 2010
    #5
  6. On 04/02/2010 03:30 PM, Dan McLeran wrote:
    > i'm running python 2.6 on win xp sp3 and i get:


    Your code isn't portable to non-Windows OS's. On my Mac and on my Linux
    workstations it simply doesn't work. Using '/usr/sbin/ifconfig' as the
    executable name in Popen does work, however.

    The OP didn't state his platform, so we shouldn't assume that a
    windows-only solution will work for him may.

    Since this list covers the use of many kinds of operating systems, it is
    foolish to make assumptions. This was my point.
    Michael Torrie, Apr 2, 2010
    #6
  7. On 04/02/2010 02:14 PM, Booter wrote:
    > I am new to python ans was wondering if there was a way to get the mac
    > address from the local NIC?


    As Dan has indicated, you have to Popen an external command to get this
    information. Every OS has different commands and syntaxes for this.
    You'll have to have a different Popen for each operating system. Also
    you must take into account that most computers have more than one
    ethernet interface these days (real and virtual). So you'll likely end
    up with between 2 and 5 different MAC addresses. And some of those are
    fake as well, like the MAC addresses used by VMware's virtual networking
    interfaces.

    What operating system are you targeting? Windows? Linux? Mac? To
    really answer your question you must supply more information.
    Michael Torrie, Apr 2, 2010
    #7
  8. Booter

    Steve Holden Guest

    Booter wrote:
    > Hello all,
    >
    > I am new to python ans was wondering if there was a way to get the mac
    > address from the local NIC?
    >
    > Thanks for your help.
    >
    >>> import uuid
    >>> uuid.getnode()

    246090452741227L
    >>>


    This is supposed to return the MAC address, but I am not sure it does.
    The documentation says:

    """
    getnode( )

    Get the hardware address as a 48-bit positive integer. The first time
    this runs, it may launch a separate program, which could be quite slow.
    If all attempts to obtain the hardware address fail, we choose a random
    48-bit number with its eighth bit set to 1 as recommended in RFC 4122.
    "Hardware address" means the MAC address of a network interface, and on
    a machine with multiple network interfaces the MAC address of any one of
    them may be returned.
    """

    So the return value isn't *guaranteed* to be an ethernet address, and
    I'm not sure whether that code gets any regular testing.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    See PyCon Talks from Atlanta 2010 http://pycon.blip.tv/
    Holden Web LLC http://www.holdenweb.com/
    UPCOMING EVENTS: http://holdenweb.eventbrite.com/
    Steve Holden, Apr 2, 2010
    #8
  9. On 04/02/2010 04:01 PM, Dan McLeran wrote:
    > which is why my OP stated the solution was for windows:
    >
    > "for windows parse
    > p.stdout.read():"


    Gotcha. Definitely missed that!
    Michael Torrie, Apr 2, 2010
    #9
  10. "Booter" <> wrote in message
    news:...
    > Hello all,
    >
    > I am new to python ans was wondering if there was a way to get the mac
    > address from the local NIC?
    >
    > Thanks for your help.
    >
    > Gerad


    This is what I use -

    ------------------------
    def get_mac_address():
    if sys.platform == 'win32':
    for line in os.popen("ipconfig /all"):
    if line.lstrip().startswith('Physical Address'):
    mac = line.split(':')[1].strip().replace('-',':')
    break
    else:
    # mac = os.popen("/sbin/ifconfig|grep Ether|awk {'print
    $5'}").read()[:-1]
    for line in os.popen("/sbin/ifconfig"):
    if 'Ether' in line:
    mac = line.split()[4]
    break
    return mac

    ------------------------

    I only target windows and linux. I don't know if it works for all platforms.

    I wrote this a long time ago. I think it would now be preferable to use
    subprocess() instead of os.popen().

    Note the commented-out line in the linux block. This is an alternative
    method I cribbed from somewhere. Not as readable, but probably faster.

    HTH

    Frank Millman
    Frank Millman, Apr 3, 2010
    #10
  11. Booter

    Booter Guest

    All,

    Thanks for all of the great solutions! Sorry I wasn't more specific
    in my post and will keep that in mind for future posts. Just FYI I
    was using a Windows machine and running Python 2.6.

    Once again thanks for all of your help!

    Gerad
    Booter, Apr 5, 2010
    #11
  12. In message
    <>, Booter
    wrote:

    > I am new to python ans was wondering if there was a way to get the mac
    > address from the local NIC?


    What if you have more than one?
    Lawrence D'Oliveiro, Apr 6, 2010
    #12
  13. Booter

    Rebelo Guest

    Lawrence D'Oliveiro wrote:
    > In message
    > <>, Booter
    > wrote:
    >
    >> I am new to python ans was wondering if there was a way to get the mac
    >> address from the local NIC?

    >
    > What if you have more than one?



    you can try with netifaces :
    http://pypi.python.org/pypi/netifaces/0.3
    I use them on both Windows and Linux
    Rebelo, Apr 7, 2010
    #13
    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. SJ

    MAC Address/Physical Address?

    SJ, Jan 7, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    504
  2. SJ

    Local machine's MAC address?

    SJ, Mar 8, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    2,724
    Eliyahu Goldin
    Mar 9, 2006
  3. Phil Hanna
    Replies:
    6
    Views:
    29,966
    Roedy Green
    Jul 5, 2003
  4. Daniel Crespo

    How to get the local mac address?

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

    Local machine's MAC address

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

Share This Page