Jump to content

Get Mac Address Efficiently


zelles
 Share

Recommended Posts

I was trying to get the MAC address of the computer running the script and the most efficient way seams to be using this function. I found it somewhere, but it was broken and would not run at all, and after making some change I got it displaying the Mac address. Does anyone know a more efficient way? This seams pretty efficient. Also, even though it works without issue, did I do things right in the function?

Working function to get the MAC address:

$IP_Address = @IPAddress1

$MAC_Address = GET_MAC($IP_Address)

MsgBox(0, "MAC Address:", $MAC_Address)

Func GET_MAC($_MACsIP)
    Local $_MAC,$_MACSize
    Local $_MACi,$_MACs,$_MACr,$_MACiIP
    $_MAC = DllStructCreate("byte[6]")
    $_MACSize = DllStructCreate("int")
    DllStructSetData($_MACSize,1,6)
    $_MACr = DllCall ("Ws2_32.dll", "int", "inet_addr", "str", $_MACsIP)
    $_MACiIP = $_MACr[0]
    $_MACr = DllCall ("iphlpapi.dll", "int", "SendARP", "int", $_MACiIP, "int", 0, "ptr", DllStructGetPtr($_MAC), "ptr", DllStructGetPtr($_MACSize))
    $_MACs  = ""
    For $_MACi = 0 To 5
    If $_MACi Then $_MACs = $_MACs & ":"
        $_MACs = $_MACs & Hex(DllStructGetData($_MAC,1,$_MACi+1),2)
    Next
    DllClose($_MAC)
    DllClose($_MACSize)
    Return $_MACs
EndFunc
Edited by zelles
Link to comment
Share on other sites

I used this function in a small tool, to get the MAC from IP and the vendor name from MAC. Tool is called Ip2Mac&Vendor and can be found on german AutoIt forum: http://autoit.de/index.php/Thread/45340-Offline-IP-zu-MAC-und-Hersteller-Konverter/

Download: http://autoit.de/index.php/Attachment/24512-IP2Mac-Vendor-7z/

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

  • 1 year later...
  • 3 years later...

Easier way (just change "Ethernet Connection" with whatever info you are looking for mac addr.) : 

Local $pid = Run(@ComSpec & " /c " & 'getmac /fo csv /v | find "Ethernet Connection"','',@SW_HIDE,$STDOUT_CHILD);
ProcessWaitClose($pid)

Local $sOutput = StdoutRead($pid)

ConsoleWrite($sOutput)

Link to comment
Share on other sites

  • 2 years later...

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
 Share

  • Recently Browsing   0 members

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