faqts : Computers : Programming : Languages : Python : Platforms : Windows

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

17 of 23 people (74%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

How do I find the MAC addresses of all network adapters on my computer?

Aug 19th, 2000 17:37
Seamus Venasse, Microsoft KB Q175472


from netbios import *

"""initialise the Netbios Control Block"""
ncb = NCB()

"""For machines with multiple network adapters you need to
enumerate the LANA numbers and perform the NCBASTAT
command on each. Even when you have a single network
adapter, it is a good idea to enumerate valid LANA numbers
first and perform the NCBASTAT on one of the valid LANA
numbers. It is considered bad programming to hardcode the
LANA number to 0."""

ncb.Reset()
ncb.Command = NCBENUM
netcards = LANA_ENUM()
ncb.Buffer = netcards
result = Netbios( ncb )
if ( result != 0 ):
	raise RuntimeError, "Error encountered: %d" % ( result )

for netcard in range( netcards.length ):

	"""The IBM NetBIOS 3.0 specifications defines four basic
	   NetBIOS environments under the NCBRESET command. Win32
	   follows the OS/2 Dynamic Link Routine (DLR) environment.
	   This means that the first NCB issued by an application
	   must be a NCBRESET, with the exception of NCBENUM.
	   The Windows NT implementation differs from the IBM
	   NetBIOS 3.0 specifications in the NCB_CALLNAME field"""

	ncb.Reset()
	ncb.Command = NCBRESET
	ncb.Lana_num = ord( netcards.lana[ netcard ] )
	result = Netbios(ncb)
	if ( result != 0 ):
		raise RuntimeError, "Error encountered: %d" % ( result )

	"""To get the Media Access Control (MAC) address for an
	   ethernet adapter programmatically, use the Netbios()
	   NCBASTAT command and provide a "*" as the name in the
	   NCB.ncb_CallName field (in a 16-chr string)."""

	ncb.Reset()
	ncb.Command = NCBASTAT
	ncb.Lana_num = ord( netcards.lana[ netcard ] )
	ncb.Callname = '*              '
	adapter = ADAPTER_STATUS()
	ncb.Buffer = adapter
	result = Netbios(ncb)
	if ( result != 0 ):
		raise RuntimeError, "Error encountered: %d" % ( result )

	MACAddr = ''
	for ch in adapter.adapter_address:
		MACAddr = '%s%02X' % ( MACAddr, ord( ch ) )

	print "MAC Address: %s" % ( MACAddr )