Jump to content
Sign in to follow this  
GregThompson

MAC Address

Recommended Posts

I'm sure someone has made one before, but here is my take on it. You're left with $macaddress as the variable. You will of course need admin rights to run the net config portion, so you can always wrap that RunWait with a RunAs(). Enjoy!

RunWait(@ComSpec & " /c net config workstation > " & @TempDir & "\mac.txt", "", @SW_HIDE)

If FileExists(@TempDir & "\mac.txt") Then

$macfile = FileOpen(@TempDir & "\mac.txt", 0)

While 1
    $macline = FileReadLine($macfile)
    If @error = -1 Then ExitLoop
    $macresult = StringInStr($macline, "NetBT_Tcpip_")
    If $macresult <> "0" Then
        $macline = StringStripWS($macline, 4)
        $macarray = StringSplit($macline, "(")
        $macline = StringStripWS($macarray[2], 8)
        $macarray = StringSplit($macline, ")")
        $macaddress = $macarray[1]
        ExitLoop
    EndIf
Wend

FileClose($macfile)

EndIf
Edited by GregThompson

Share this post


Link to post
Share on other sites

My code...

RunWait (@ComSpec & ' /c ' & 'nbtstat -A '&@IPAddress1&' | find /I "MAC" >'&@TempDir&'\mac.tmp',@ScriptDir, @SW_HIDE)
MsgBox(0,"",StringRight(FileReadLine(@TempDir&'\mac.tmp'),17))

Pierre

Share this post


Link to post
Share on other sites

For what it's worth, the snippet that was posted at the beginning of this thread was probably the most useful and compact bit of code I have found in AutoIT for grabbing the MAC address, but here is what I did to it:

Dim $macaddress[4]

RunWait(@ComSpec & " /c ipconfig /all > C:\ipconfig.txt", "", @SW_HIDE)

If FileExists("C:\ipconfig.txt") Then

$nics = 1

$macfile = FileOpen("C:\ipconfig.txt", 0)

While $nics < 5
    $macline = FileReadLine($macfile)
    If @error = -1 Then ExitLoop
    $macresult = StringInStr($macline, "Physical Address")
    If $macresult <> "0" Then
    $macline = StringStripWS($macline, 8)
    $macline = StringTrimLeft($macline, 25)
                $macarray = StringSplit($macline, "-")
    $macaddress[$nics] = $macarray[1] & $macarray[2] & $macarray[3] & _
                            $macarray[4] & $macarray[5] & $macarray[6]
    MsgBox(0, "MAC Address", $macaddress[$nics])
   EndIf
Wend

FileClose($macfile)

EndIf

I found I needed the MAC addresses from ALL of the NIC's in a given box so I could use another command line configurator widget to create a NIC team on the fly (Broadcom). It's "hard coded" with the assumption that there are no more than four NIC's in the box. Someone could probably make it even more open ended.

-Chris

Edited by AramisResearch

Share this post


Link to post
Share on other sites

another one...

#include <array.au3>
#include <file.au3>

$filename = "C:\ipconfig.txt"
RunWait(@ComSpec & " /c ipconfig /all > " & $filename , "", @SW_HIDE)
Dim $array
_FileReadToArray($filename,$array)
$text = _ArrayToString($array,";")
$macs = StringRegExp($text,":\s((?:\A\A-){5}\A\A);",3)
_ArrayDisplay($macs,"MAC Addrs")
FileDelete($filename)

Cheers

Kurt


__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

For what it's worth, the snippet that was posted at the beginning of this thread was probably the most useful and compact bit of code I have found in AutoIT for grabbing the MAC address, but here is what I did to it:

Dim $macaddress[4]

RunWait(@ComSpec & " /c ipconfig /all > C:\ipconfig.txt", "", @SW_HIDE)

If FileExists("C:\ipconfig.txt") Then

$nics = 1

$macfile = FileOpen("C:\ipconfig.txt", 0)

While $nics < 5
    $macline = FileReadLine($macfile)
    If @error = -1 Then ExitLoop
    $macresult = StringInStr($macline, "Physical Address")
    If $macresult <> "0" Then
    $macline = StringStripWS($macline, 8)
    $macline = StringTrimLeft($macline, 25)
                $macarray = StringSplit($macline, "-")
    $macaddress[$nics] = $macarray[1] & $macarray[2] & $macarray[3] & _
                            $macarray[4] & $macarray[5] & $macarray[6]
    MsgBox(0, "MAC Address", $macaddress[$nics])
   EndIf
Wend

FileClose($macfile)

EndIf

I found I needed the MAC addresses from ALL of the NIC's in a given box so I could use another command line configurator widget to create a NIC team on the fly (Broadcom). It's "hard coded" with the assumption that there are no more than four NIC's in the box. Someone could probably make it even more open ended.

-Chris

Nice, I added these to display it in a mesage and copy to clipboard.

MsgBox(4096,"My Mac",$macaddress)

ClipPut($macaddress)

Share this post


Link to post
Share on other sites

You're new so, everyone needs time to adjust... but what on earth were you thinking? This thread was 5 YEARS dead, and you brought it back for a MsgBox and a ClipPut? Really?

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites

Anyway since this thread was resurrected , it can be done this way too , and i think is nicer cause is not using the hdd .

Func GetEnthPyshAddrs()
$Pid = Run(@ComSpec & " /c ipconfig /all", "", 0x0, 0x2)
Local $line = ""
While 1
    $line = StdoutRead($Pid)
    If StringLen($line) > 0 Then
        StdioClose($Pid)
        ExitLoop
    EndIf
WEnd
$macs = StringRegExp($line, "[0-9A-F]{2}\-[0-9A-F]{2}\-[0-9A-F]{2}\-[0-9A-F]{2}\-[0-9A-F]{2}", 3)
If @error Then
    Return @error
Else
   Return $macs
EndIf
EndFunc

Share this post


Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...