Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Dectect MAC address


  • Please log in to reply
8 replies to this topic
Pasukun
  • Members
  • 91 posts
  • Last active: Jun 27 2014 02:14 PM
  • Joined: 16 Dec 2004
Is there a way to detect MAC address of the local machine's network card?

I know I can find the Node name and IP address by using "A_ComputerName" and "A_IPAddress1".

It would be helpful to farther secure the script to only run on certain computer with the use of MAC address detection. This way I can create more risky scripts without worrying too much.

I know MAC address can be spoofed as well, but it is harder to do so and it will be better than nothing. :)

Jon
  • Members
  • 349 posts
  • Last active: Aug 30 2011 08:35 PM
  • Joined: 28 Apr 2004
This is probably the easiest way although you can probably do it with an API call as well-


runwait, %comspec% /c ipconfig /all > c:\mac.txt, ,hide

Loop, Read, c:\mac.txt
{
ifinstring, A_LoopReadLine, Physical Address
{
MAC=%A_LoopReadLine%
break
}
}

StringRight, MAC, MAC, 18
msgbox, %MAC%

filedelete, c:\mac.txt



niwi
  • Members
  • 140 posts
  • Last active: Feb 04 2013 02:51 PM
  • Joined: 27 Feb 2005
Hi Jon,

the 14th line isn't always correct.

Pasukun, you should search the output of the "ipconfig /all" command for the line containig "physical address" (is this the correct translation?).

NiWi.

Jon
  • Members
  • 349 posts
  • Last active: Aug 30 2011 08:35 PM
  • Joined: 28 Apr 2004

Hi Jon,

the 14th line isn't always correct.

Pasukun, you should search the output of the "ipconfig /all" command for the line containig "physical address" (is this the correct translation?).

NiWi.


Thanks, I've edited my script above

Jon
  • Members
  • 349 posts
  • Last active: Aug 30 2011 08:35 PM
  • Joined: 28 Apr 2004
Another way without a text file using corrupts CMDret DLL


; CMDret DLL version 3.1.1 (or greater) required

ret1 := CMDret(COMSPEC " /C ipconfig/all")

Loop, Parse, ret1, `n
{
ifinstring, A_LoopField, Physical Address
{
MAC=%A_LoopField%
break
}
}

StringRight, MAC, MAC, 19
msgbox, %MAC%


CMDret(CMD)
{
  VarSetCapacity(StrOut, 10000)
  RetVal := DllCall("cmdret.dll\RunReturn", "str", CMD, "str", StrOut)
  Return, %StrOut%
}


Pasukun
  • Members
  • 91 posts
  • Last active: Jun 27 2014 02:14 PM
  • Joined: 16 Dec 2004
Thanks for the replies.

Yeah I was afraid of that..
Looks like the only way to get the MAC address value is to run "IPCONFIG /ALL" and use the "IfInString" command.

niwi
  • Members
  • 140 posts
  • Last active: Feb 04 2013 02:51 PM
  • Joined: 27 Feb 2005
Hi Pasukun,

the MAC address is hard coded on the NICs. It is possible to set a new MAC address and overriding the original by setting a registry key. In this case you could read the address from the registry, but this is not really recommended. Changing the MAC address requires a good know how about networking, because this may make trouble in a network if you not really know what the MAC address is used for.

I think the best solution is to use ipconfig.

NiWi.

niwi
  • Members
  • 140 posts
  • Last active: Feb 04 2013 02:51 PM
  • Joined: 27 Feb 2005
Hi,

maybe you also can try to use a DLLCall ("IPHLPAPI.DLL\GetAdaptersInfo"...). But I haven't used this function yet so I have no experience what exactly is to do (parameters etc.). My first try call's Dr. Watson :-(

NiWi.

normel
  • Members
  • 5 posts
  • Last active: Jun 03 2005 02:36 AM
  • Joined: 19 May 2005
Here's a little code you can use. It creates a vbscript, then executes it. This assumes you have W2K or later that supports WMI.

ifExist,c:\wutemp\temp.vbs
   filedelete,c:\wutemp\temp.vbs
fileappend,On Error Resume Next`n,c:\wutemp\temp.vbs
fileappend,Dim NumAdap(5)`n,c:\wutemp\temp.vbs
fileappend,set objFSO = CreateObject("Scripting.FileSystemObject")`n,c:\wutemp\temp.vbs
fileappend,Set f1 = objFSO.CreateTextFile("c:\wutemp\temp.txt"`, True)`n,c:\wutemp\temp.vbs
fileappend,Set objWMIService = GetObject("winmgmts:\\127.0.0.1\root\CIMV2")`n,c:\wutemp\temp.vbs
fileappend,Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter")`n,c:\wutemp\temp.vbs
fileappend,i = 0`n,c:\wutemp\temp.vbs
fileappend,For Each objItem In colItems`n,c:\wutemp\temp.vbs
fileappend,if objItem.MACAddress <> "" then`n,c:\wutemp\temp.vbs
fileappend,i = i + 1`n,c:\wutemp\temp.vbs
fileappend,NumAdap(i) = objItem.MACAddress`n,c:\wutemp\temp.vbs
fileappend,end if`n,c:\wutemp\temp.vbs
fileappend,Next`n,c:\wutemp\temp.vbs
fileappend,For j = 1 to i`n,c:\wutemp\temp.vbs
fileappend,f1.writeline(NumAdap(j))`n,c:\wutemp\temp.vbs
fileappend,Next`n,c:\wutemp\temp.vbs
fileappend,f1.Close,c:\wutemp\temp.vbs
run,c:\wutemp\temp.vbs
Loop, Read, c:\wutemp\temp.txt
{
   MAC = %A_LoopReadLine%
   msgbox,MAC = %MAC%
}
sleep 5000
ifExist,c:\wutemp\temp.vbs
   filedelete,c:\wutemp\temp.vbs
ifExist,c:\wutemp\temp.txt
   filedelete,c:\wutemp\temp.txt

You'll have to play with the last few lines, if you want to get rid of the temporary files the script creates. If you use runwait instead of run, it will work every time, and you won't need the sleep 5000 line, but it also runs a lot slower before the msgbox popped up.

The script will work with multiple NICs that have MAC addresses.