how to read hardware info on Win NT

Bengt Richter bokr at oz.net
Wed Apr 10 16:12:14 EDT 2002


On Wed, 10 Apr 2002 15:50:20 +0100, Mark Gash <mgash at trisystems.co.uk> wrote:
[...]
>>Thanks for the __winreg hint. Looks like I have to get some infos
>>about the registry because I have no idea how to find the right key.
>>
>>If anyone knows, please tell me.
>>
>>I need the id of my ethernet card for a license documentation tool we
                               ^^^^
It is possible to have more than one, so I assume you want the one
associated with the current host ip? I made a hack building on Mark's advice
which does this. (Of course it could break if ipconfig changes its output format).
See below.

>>want to write in Python. As many commercial softwares are using this
>>id for generating the license keys etc. I thought there should be
>>module in Python.  Maybe there is a modul for commercial distribution
>>of Python software?
>
>Marcus,
>With further investigation, I cannot find where the MAC address of the card
>is in registry (I assume that you require the MAC address when you say that
>you want the id of your Ethernet card).
>
>I am also assuming that you are using a win32 machine.
>
>There are two other ways that I can think of in order to get your info:-
>
>1. Run the command 'ipconfig /all' and pipe the results into a text file
>that you can then search i.e.
>	'ipconfig /all > c:\ipdetails.txt'
Using popen you can avoid having to deal with a file, which I did below.

>
>2. The nbtstat command will report the MAC address of a card, so that for
>example, you could use the command 'nbtstat -a <machinename>' and pipe the
>results out to a text file, i.e.
>	'nbtstat -a mypc > c:\nbtdetails.txt' 
>
>Once the correct command has been tested, then from your python script wrap
>it up in an os.system call.
>
>So, we will end up with something along the lines of :-
>
>import os
>os.system("ipconfig /all > c:\ipdetails.txt")
>
>or 
>
>import os
>os.system("nbtstat -a mypc > c:\nbtdetails.txt")
>
>Then you will have to add some string handling/searching to find the MAC
>address and process it as required.
>
>Hope this helps.
>

You could try this. It seems to work on my NT4:
(You might want to add some error handling though).
It obviously also depends on exact spelling and the IP Address
line coming after the Physical Address line, etc.

===< getMac.py >===
def getMAC():
    import socket, os
    curr_ip = socket.gethostbyname(socket.gethostname())
    ipconfdata= os.popen('ipconfig/all').readlines()
    for line in ipconfdata:
        if line.find('Physical Address') >=0:
            pa = line.split(':')[-1].strip()
        if line.find('IP Address') >=0:
            ip = line.split(':')[-1].strip()
            if ip == curr_ip: return pa, curr_ip

if __name__ == '__main__':
    print '%s is the physical address associated with %s' % getMAC()
===================

Regards,
Bengt Richter



More information about the Python-list mailing list