Jump to content

Get MAC Address

Recommended Posts

Hi. I am trying to determine the MAC address of the active network connection and write to text file.

I have found the examples in the help & forum for accessing the registry of the system, how to determine what the active connection is (LAN, Dial-up, etc) but nothing on MAC address. I have searched registry, but all I see is the "NetCfgInstanceId" and not the MAC.

I am still pretty new to AutoIt...I have only written some small, very simple programs. This though has me stumped. And how to handle several NIC's MAC? I only want the 'active' one. The target machines should only have one, but might have two (wireless and 10/100), but only one would be active.

Thanks. :)

Link to comment
Share on other sites

Something like:

runWait(@comSpec & ' /c ipconfig /all > "' & @tempDir & '\ipconfig.txt"')

will get the MAC address into a text file, which you could then parse. (Edit: missing double quote.)

Edited by LxP
Link to comment
Share on other sites

Thanks LxP! :evil: I can definitely work with that.

Question though: Is there no way then to get the value from the registry? Or discern the value from the NetCfgInstanceId? If that is indeed the MAC? Just curious. :)

Again, thanks.

Link to comment
Share on other sites

I don't know how to do it with native commands or DLL calls or objects or anything fancy like that, but I did once write a BAT file to get this info from IPCONFIG /ALL. The "Physical Address" is the MAC address, and disabled NICs are not displayed.

Link to comment
Share on other sites

Parsing a program's output isn't the most elegant solution but it's definitely the easiest. I did a quick Google for ideas but a lot of registry locations didn't work on my XP system.

Here's some (presumably) VBS I came across:

Set NetAdapterSet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_NetworkAdapter")
for each NetAdapter in NetAdapterSet
   wscript.echo NetAdapter.MACAddress

if anyone can translate that.

Link to comment
Share on other sites

Awesome. I appreciate the effort. :)

I will try parsing the text file to return the mac address back to AutoIt. This is really what I am trying to do. I need the MAC as a string.

I am trying to write a net client (no, not .NET) that will uniquely identify the PC based on MAC. I do not want to use the actual MAC, but a hashed value (say md5($MAC)). I think though this regkey,

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards

will provide me with enough information to get/generate a unique id of some sort.

One more thing I am wondering: can the VISA functions be used to obtain the MAC from the actual device? Just curious if anyone knows. Seems like overkill for my project but am curious.

Again thanks for all the advice/info.

Link to comment
Share on other sites

Another snippet: if you know IP of adapter (you most likely is) you can use this function to translate addresses (this is Ejoc's func if I'm recall right):

$MAC = _GetMACFromIP ("")
MsgBox (0, "MAC Value", $MAC)

Func _GetMACFromIP ($sIP)
    Local $MAC,$MACSize
    Local $i,$s,$r,$iIP
;Create the struct
;   char    data[6];
    $MAC        = DllStructCreate("byte[6]")

;Create a pointer to an int
;   int *MACSize;
    $MACSize    = DllStructCreate("int")

;*MACSize = 6;

;call inet_addr($sIP)
    $r = DllCall ("Ws2_32.dll", "int", "inet_addr",_
                    "str", $sIP)
    $iIP = $r[0]

;Make the DllCall
    $r = DllCall ("iphlpapi.dll", "int", "SendARP",_
                    "int", $iIP,_
                    "int", 0,_
                    "ptr", DllStructGetPtr($MAC),_
                    "ptr", DllStructGetPtr($MACSize))

;Format the MAC address into user readble format: 00:00:00:00:00:00
    $s  = ""
    For $i = 0 To 5
        If $i Then $s = $s & ":"
        $s = $s & Hex(DllStructGetData($MAC,1,$i+1),2)

;Must free the memory after it is used

;Return the user readble MAC address
    Return $s

Of course, this is beta stuff.

Link to comment
Share on other sites

  • 2 years later...

I stumbled accross this article while searching for a way to get the MAC adresses from a computer... so I translated the VBS code into AutoIt

$objWMIService  = ObjGet("winmgmts:{impersonationLevel=impersonate}")
$netAdapterSet = $objWMIService.ExecQuery("select * from Win32_NetworkAdapter")
For $netAdapter in $netAdapterSet
    MsgBox(0, "", $netAdapter.MACAddress)

$objNetwork = ""
$netAdapterSet = ""

EDIT: as you may notice while using this code it gives you a lot of different MAC Adresses, this is because it will output the MAC adress of the WAN/L2TP/Bluetooth/Firewire too... i recommand using

MsgBox(0, "", "MAC" & $netAdapter.MACAddress & @CRLF & "Type:" & $netAdapter.AdapterType & @CRLF & "Device name:" & $netAdapter.Caption)

to understand what it's all about...

Edited by JavaCupiX
Link to comment
Share on other sites

If you're using XP, how about something as simple as the DOS command getmac in an AutoIt runWait command like that shown above. It (getmac) can output several different formats so have a play around with it to see which suits you (type getmac /? in a cmd window for help).

Link to comment
Share on other sites

Sorry but I hate using RunWait stuff when Microsoft is so kind to provide us with a nice WMI interface. I can never be sure what happens with a RunWait... if one day the IT Staff decides to deactivate that command for the users it could take days before I think about "the program which uses a RunWait("ipconfig /all") or RunWait('getmac')"...

But thanks for the tip, didn't know that "getmac" thing... can be usefull

Cleaner is better!

Edited by JavaCupiX
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...