Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.
Keywords: Mac (Media Access Control) Address Physical address NIC
There are many methods by which to retrieve the MAC or NIC address. Here are a few.
strComputer = "." objWMIService = GetObject(StrCat("winmgmts:!\\" , strComputer , "\root\cimv2")) colAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True AND DHCPEnabled = True") ForEach objAdapter in colAdapters Message( "Physical address: " , objAdapter.MACAddress ) Next
#DefineFunction GetMacAddr(server) ;This function requires Windows NT 3.1 or later ;PARAMETER ; server ; specifying the name of the remote server on which the function is to execute. ; The string must begin with \\. If this parameter is NULL, the local computer is used. ;Check platform if WinVersion(4) < 4 Message("","This function requires Windows NT 3.1 or later") return 0 endif NERR_Success = 0 ERROR_MORE_DATA = 234 MAX_PREFERRED_LENGTH = -1 level = 0 bufptr = BinaryAlloc(4) entriesreadbuf = BinaryAlloc(4) BinaryEODSet(entriesreadbuf,4) totalentriesbuf = BinaryAlloc(4) BinaryEODSet(totalentriesbuf,4) resumehandlebuf = BinaryAlloc(4) BinaryEODSet(resumehandlebuf,4) handle = DllLoad(StrCat(DirWindows(1),"NETAPI32.DLL")) ;You can specify the computername instead of Lpnull / lpstr:\\server if server == "" retcode = DllCall (handle,long:"NetWkstaTransportEnum", lpnull, long:level, lpBinary:bufptr , long:MAX_PREFERRED_LENGTH, lpBinary:entriesreadbuf, lpBinary:totalentriesbuf,lpBinary:resumehandlebuf) else serverbuf = BinaryAlloc(1000) BinaryPokeStr(serverbuf,0,server) BinaryConvert(serverbuf,0,3,0,0) retcode = DllCall (handle,long:"NetWkstaTransportEnum", lpbinary:serverbuf, long:level, lpBinary:bufptr , long:MAX_PREFERRED_LENGTH, lpBinary:entriesreadbuf, lpBinary:totalentriesbuf,lpBinary:resumehandlebuf) BinaryFree(serverbuf) endif if retcode != NERR_Success Message("Call to NetWkstaTransportEnum was unsuccessful", retcode) DllCall (handle,long:"NetApiBufferFree",lpbinary:bufptr) BinaryFree(bufptr) Exit Endif totalentries = BinaryPeek4(totalentriesbuf,0) entriesread = BinaryPeek4(entriesreadbuf,0) resumehandle = BinaryPeek4(resumehandlebuf,0) ;Get wkti0_Transport_Address member addr = BinaryPeek4(bufptr,0) memberaddr = IntControl (32, addr+12, "LONG", 0, 0) MacAddresBuf = BinaryAlloc(1000) For i = 0 to 20 macword = IntControl(32,memberaddr,"WORD",0,0) if macword == 0 then break memberaddr = memberaddr + 2 BinaryPoke2(MacAddresBuf,i*2,macword) Next BinaryConvert(MacAddresBuf,3,0,0,0) macAddr = BinaryPeekStr(MacAddresBuf,0, 100) ;message("Mac Address is:",macAddr) DllCall (handle,long:"NetApiBufferFree",long:addr) BinaryFree(resumehandlebuf) BinaryFree(totalentriesbuf) BinaryFree(entriesreadbuf) BinaryFree(MacAddresBuf) BinaryFree(bufptr) DllFree(handle) Return macAddr #EndFunction #DefineFunction GetAllTranportAddr(server) ; Retrieves information about transport protocols that are currently managed by the redirector ; returns the MAC address of each network protocol loaded ; ; This function requires Windows NT 3.1 or later ; ;PARAMETER ; server ; specifying the name of the remote server on which the function is to execute. ; The string must begin with \\. If this parameter is NULL, the local computer is used. ;RETURNS ; a tab delimited list of 'Transport address| Transport name' ; ; Table 4-1: Abbreviations of NetBIOS Protocols ; Abbreviation Network Protocol ; Nbf NetBEUI ; NwlnkNB->NwlnkIpx NetBIOS over IPX ; NetBT NetBIOS over TCP/IP ; ;Check platform if WinVersion(4) < 4 Message("","This function requires Windows NT 3.1 or later") return 0 endif NERR_Success = 0 ERROR_MORE_DATA = 234 MAX_PREFERRED_LENGTH = -1 level = 0 bufptr = BinaryAlloc(4) entriesreadbuf = BinaryAlloc(4) BinaryEODSet(entriesreadbuf,4) totalentriesbuf = BinaryAlloc(4) BinaryEODSet(totalentriesbuf,4) resumehandlebuf = BinaryAlloc(4) BinaryEODSet(resumehandlebuf,4) handle = DllLoad(StrCat(DirWindows(1),"NETAPI32.DLL")) ;You can specify the computername instead of Lpnull / lpstr:\\server serverbuf = BinaryAlloc(1000) BinaryPokeStr(serverbuf,0,server) BinaryConvert(serverbuf,0,3,0,0) if server == "" retcode = DllCall (handle,long:"NetWkstaTransportEnum", lpnull, long:level, lpBinary:bufptr , long:MAX_PREFERRED_LENGTH, lpBinary:entriesreadbuf, lpBinary:totalentriesbuf,lpBinary:resumehandlebuf) else BinaryFree(serverbuf) serverbuf = BinaryAlloc(1000) BinaryPokeStr(serverbuf,0,server) BinaryConvert(serverbuf,0,3,0,0) retcode = DllCall (handle,long:"NetWkstaTransportEnum", lpbinary:serverbuf, long:level, lpBinary:bufptr , long:MAX_PREFERRED_LENGTH, lpBinary:entriesreadbuf, lpBinary:totalentriesbuf,lpBinary:resumehandlebuf) BinaryFree(serverbuf) endif if retcode != NERR_Success Message("Call to NetWkstaTransportEnum was unsuccessful", retcode) DllCall (handle,long:"NetApiBufferFree",lpbinary:bufptr) BinaryFree(resumehandlebuf) BinaryFree(totalentriesbuf) BinaryFree(entriesreadbuf) BinaryFree(MacAddresBuf) BinaryFree(bufptr) Exit Endif totalentries = BinaryPeek4(totalentriesbuf,0) entriesread = BinaryPeek4(entriesreadbuf,0) resumehandle = BinaryPeek4(resumehandlebuf,0) list = "" ;Get wkti0_Transport_Address member addr = BinaryPeek4(bufptr,0) For x = 1 to EntriesRead membername = IntControl (32, addr+8, "LONG", 0, 0) memberaddr = IntControl (32, addr+12, "LONG", 0, 0) MacAddresBuf = BinaryAlloc(1000) ;Get addresses For i = 0 to 20 macword = IntControl(32,memberaddr,"WORD",0,0) if macword == 0 then break memberaddr = memberaddr + 2 BinaryPoke2(MacAddresBuf,i*2,macword) Next BinaryConvert(MacAddresBuf,3,0,0,0) macAddr = BinaryPeekStr(MacAddresBuf,0, 100) ;Get names NameBuf = BinaryAlloc(1000) For i = 0 to 256 nameword = IntControl(32,membername,"WORD",0,0) if nameword == 0 then break membername = membername + 2 BinaryPoke2(NameBuf,i*2,nameword) Next BinaryConvert(NameBuf,3,0,0,0) AddrName = BinaryPeekStr(NameBuf,0, 100) BinaryFree(NameBuf) list = StrCat(list,@tab,macAddr,"|",AddrName) Addr = Addr + 20 BinaryFree(MacAddresBuf) Next DllCall (handle,long:"NetApiBufferFree",long:addr) BinaryFree(resumehandlebuf) BinaryFree(totalentriesbuf) BinaryFree(entriesreadbuf) BinaryFree(bufptr) list = StrTrim(list) DllFree(handle) Return list #EndFunction macaddr = GetMacAddr("\\computername") Message("Mac address",macaddr) protocols = GetAllTranportAddr("\\computername") AskItemList("Transport Protocols",protocols,@tab,@unsorted,@single) exit
Try this quick and dirty method which works, if you have TCP/IP installed?
;run WINIPCFG or IPConfig and pipe the output to a file ;You can then parse the MAC layer address from the file. #DefineFunction GetTCPIPInfo_UDF(value) origdir = DirGet() OutputFile = StrCat (origdir, "IPCFG_TEMP.TXT") If WinVersion(4) >= 4;WINNT RunHideWait(Environment('COMSPEC'), '/c IPCONFIG /ALL >"%OutputFile%"') Else RunHideWait('WINIPCFG', '/ALL /BATCH > "%OutputFile%"') Endif If !FileExist(OutputFile) MessageTxt = StrCat ("TCP/IP parameters can't be determined.", @CRLF) MessageTxt = StrCat (MessageTxt, @CRLF, "No data will be captured.", @CRLF) Message (Caption, MessageTxt) exit EndIf fs = Filesize (OutputFile) buf = BinaryAlloc(fs) BinaryRead(buf, OutputFile) ; ; Grab value from file lacptr = BinaryIndexEx(buf, 0, "Description", @FWDSCAN, 0) ptr = BinaryIndexEx(buf, lacptr, value, @FWDSCAN, 0) colonptr = BinaryIndexEx(buf, ptr, ":", @FWDSCAN, 0) CRLFptr = BinaryIndexEx(buf, ptr, @CRLF, @FWDSCAN, 0) Address = BinaryPeekStr(buf, colonptr+2, CRLFptr-colonptr-2) Address = StrTrim(Address) BinaryFree (buf) FileDelete(OutputFile) return Address #EndFunction value = "Default Gateway" addr = GetTCPIPInfo_UDF(value) Message(StrCat("Local Area Connection - ",value),addr) value = "Physical Address" addr = GetTCPIPInfo_UDF(value) Message(StrCat("Local Area Connection - ",value),addr) value = "IP Address" addr = GetTCPIPInfo_UDF(value) Message(StrCat("Local Area Connection - ",value),addr)
Additionally, here's some code that does a reasonable job of obtaining it:
tempname="c:\temp\trash.txt" tempbat="c:\temp\trash.bat" bb=BinaryAlloc(2000) ptr=0 ptr=ptr+BinaryPokeStr(bb,ptr,strcat("net config >",tempname,@crlf)) ptr=ptr+BinaryPokeStr(bb,ptr,strcat("nbtstat -N >>",tempname,@crlf)) BinaryWrite(bb,tempbat) BinaryEODSet(bb,0) RunWait("command.com",strcat("/c ",tempbat)) BinaryRead(bb,tempname) ;Locate first EOF ptr=BinaryIndex(bb,0,@crlf,@fwdscan) ; get previous space ptrx=BinaryIndex(bb,ptr," ",@backscan) computername=BinaryPeekStr(bb,ptrx+1,ptr-ptrx-1) ;locate IpAddress ptr=BinaryIndexNC(bb,0,"Node IpAddress:",@fwdscan) ;Find both square brackets around IP address ptrx=BinaryIndex(bb,ptr,"[",@fwdscan) ptr=BinaryIndex(bb,ptrx,"]",@fwdscan) ipaddress=BinaryPeekStr(bb,ptrx+1,ptr-ptrx-1) BinaryEODSet(bb,0) BinaryPokeStr(bb,0,strcat("nbtstat -a ",strsub(computername,3,-1)," >",tempname,@crlf)) BinaryWrite(bb,tempbat) BinaryEODSet(bb,0) RunWait("command.com",strcat("/c ",tempbat)) BinaryRead(bb,tempname) ;locate MacAddress ptrx=BinaryIndexNC(bb,0,"MAC Address = ",@fwdscan)+strlen("MAC Address = ")-1 ;Find both square brackets around IP address ptr=BinaryIndex(bb,ptrx,@crlf,@fwdscan) macaddress=BinaryPeekStr(bb,ptrx+1,ptr-ptrx-1) Message("Computername",computername) Message("IpAddress",ipaddress) Message("MAC Address",macaddress) BinaryFree(bb) exit
If you have NT's Resource Kit, it includes getmac.exe which will query NT boxes for this information. If you enter getmac without parameters, it looks at the local nics. For example:
C:\>getmac Transport Address Transport Name ----------------- -------------- 00-50-DA-BD-0F-BC \Device\NwlnkNb 00-50-DA-BD-0F-BC \Device\Nbf_El90x1 00-50-DA-BD-0F-BC \Device\NetBT_El90x1 Displays Network Transports and Address Information GETMAC [\\computername] or [computername.domain.com ;In WinBatch, for a remote machine RunWait('GETMAC.EXE','\\Computername >macaddr.txt') ;for the local machine RunWait('GETMAC.EXE','>macaddr.txt')You would then have to parse the output.
Novell Net Address:
Restrictions: If you are using Novell Client32 v2.20 or newer on Win95, or one of the Novell Clients for NT, the following basic code works OK:
AddExtender("WWN4X34I.DLL") TempNetAddr = n4GetNetAddr("",0) TempMACAddr = ItemExtract(2,TempNetAddr,":")
Article ID: W13483
Filename: Get MAC Address (2nd Method).txt
File Created: 2005:12:14:13:42:20
Last Updated: 2005:12:14:13:42:20