AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Dectect MAC address

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Pasukun



Joined: 16 Dec 2004
Posts: 84

PostPosted: Mon May 23, 2005 6:26 pm    Post subject: Dectect MAC address Reply with quote

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. Smile
_________________

"the things we touch have no permanence. my master would say: there is nothing we can hold onto in this world.. only by letting go can we truly possess what is real..."
Back to top
View user's profile Send private message
Jon



Joined: 28 Apr 2004
Posts: 373

PostPosted: Mon May 23, 2005 7:11 pm    Post subject: Reply with quote

This is probably the easiest way although you can probably do it with an API call as well-

Code:


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



Last edited by Jon on Tue May 24, 2005 12:58 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail
niwi



Joined: 27 Feb 2005
Posts: 128
Location: Heidelberg, Germany

PostPosted: Tue May 24, 2005 10:58 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Jon



Joined: 28 Apr 2004
Posts: 373

PostPosted: Tue May 24, 2005 12:58 pm    Post subject: Reply with quote

niwi wrote:
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
Back to top
View user's profile Send private message Send e-mail
Jon



Joined: 28 Apr 2004
Posts: 373

PostPosted: Tue May 24, 2005 1:32 pm    Post subject: Reply with quote

Another way without a text file using corrupts CMDret DLL


Code:
; 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%
}
Back to top
View user's profile Send private message Send e-mail
Pasukun



Joined: 16 Dec 2004
Posts: 84

PostPosted: Tue May 24, 2005 6:34 pm    Post subject: Reply with quote

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.
_________________

"the things we touch have no permanence. my master would say: there is nothing we can hold onto in this world.. only by letting go can we truly possess what is real..."
Back to top
View user's profile Send private message
niwi



Joined: 27 Feb 2005
Posts: 128
Location: Heidelberg, Germany

PostPosted: Wed May 25, 2005 11:04 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
niwi



Joined: 27 Feb 2005
Posts: 128
Location: Heidelberg, Germany

PostPosted: Thu May 26, 2005 11:57 am    Post subject: Reply with quote

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 Sad

NiWi.
Back to top
View user's profile Send private message
normel



Joined: 20 May 2005
Posts: 5
Location: NY, NY

PostPosted: Fri May 27, 2005 8:05 pm    Post subject: Reply with quote

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.

Code:
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.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group