Sign in to follow this  
Followers 0
RichE

Get_mac UDF

10 posts in this topic

#1 ·  Posted (edited)

Hi All,

Someone's most probably done ages ago, but someone may find it useful, i know i will :P , anyway it've tested it in a vista environment and the getmac command works fine, will test it via and Xp environment tomorrow at work.

CODE

#cs ----------------------------------------------------------------------------

AutoIt Version: 3.2.12.1

Author: Richard Easton

Script Function:

Get mac address.

#ce ----------------------------------------------------------------------------

#Include <File.au3>

#RequireAdmin

$PC = @ComputerName & ".tmp"

if fileexists($pc) then

filedelete($pc)

EndIf

Run(@ComSpec & " /c " & 'getmac >> ' & $PC, "", @SW_HIDE)

$i = _FileCountLines($PC)

$line = 0

Do

$info = filereadline($PC, $line)

if $info = "" or StringInStr($info, "Physical", 0) or StringInStr($info, "===================", 0) or StringInStr($info, "Media disconnected", 0) then

sleep(1000)

$line = $line +1

else

$mac = stringleft(FileReadLine($pc, $line), 17)

$line = $i

filedelete($pc)

EndIf

until $line = $i

;msgbox(64, "MAC Address", "your mac address is " & $mac)

and I've used the file include, so whoever wrote it, thx and Kudos to them

sorry if i've posted this in the wrong place.

Edited by l15ard

RichE

[font="'Arial Narrow';"]Current projects[/font]

[font="'Arial Narrow';"]are on my site [/font]Sellostring

Share this post


Link to post
Share on other sites



Tested on XP and the func works fine, if I could now ask someone to point me in the right direction for creating it as a UDF as with the code inside a .AU3 script it works fine, but when I try the run it as an include it doesn't bring back any result..???


RichE

[font="'Arial Narrow';"]Current projects[/font]

[font="'Arial Narrow';"]are on my site [/font]Sellostring

Share this post


Link to post
Share on other sites

You would need to use Func/EndFunc, and then instead of a Msgbox use Return

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

You would need to use Func/EndFunc, and then instead of a Msgbox use Return

Thanks Pete1234, it was the Return part i was missing, anyways here's the GetMAC UDf, which displays your first active nic's mac address, which will be useful for some of my future apps.

CODE

#include-once

#include <file.au3>

; #INDEX# ====================================================================================================

===================

; Title .........: GetMac

; AutoIt Version: v3.2.12.1

; Language: English

; Description ...: Functions MAC Address of first none disconnected NIC.

; ====================================================================================================

===========================

; #CURRENT# ====================================================================================================

=================

;_getmac

====================================================================================================

===========================

; #FUNCTION# ====================================================================================================

================

; Name...........: _Getmac

; Description ...: Returns the first active physical MAC Address.

; Syntax.........: _Getmac()

; Parameters ....: None

; Return values .: Success - Returns MAC Address.

; Author ........: Richard Easton

; Modified.......:

; Remarks .......: Tested on XP and Vista.

; Remarks .......: Thanks to Pete1234 for pointing me in the right direction to create this UDF.

; Link ..........; http://thesauce.webs.com

; Example .......; No

; ====================================================================================================

===========================

Func _GetMAC()

local $mac

$PC = @ComputerName & ".tmp"

if fileexists($pc) then

filedelete($pc)

EndIf

Run(@ComSpec & " /c " & 'getmac >> ' & $PC, "", @SW_HIDE)

$i = _FileCountLines($pc)

$line = 0

Do

$info = filereadline($PC, $line)

if $info = "" or StringInStr($info, "Physical", 0) or StringInStr($info, "===================", 0) or StringInStr($info, "Media disconnected", 0) then

sleep(1000)

$line = $line +1

else

$mac = stringleft(FileReadLine($pc, $line), 17)

$line = $i

filedelete($pc)

Return $mac

EndIf

until $line = $i

EndFunc

thanks again.

GetMAC.au3

Edited by l15ard

RichE

[font="'Arial Narrow';"]Current projects[/font]

[font="'Arial Narrow';"]are on my site [/font]Sellostring

Share this post


Link to post
Share on other sites

Is It neccessary that u have internet Connection To Get mac Address... =O =S


i'm very responsible, when ever something goes wrong they always say I'm responsible.Life is like an Adventure... BUT COOL GRAPHICS<====================----=LEGEND KILLER=----=========================>

Share this post


Link to post
Share on other sites

No, not as long as the computer is in your network.

Share this post


Link to post
Share on other sites

Hi,

Try this function that I have just implemented. This function returns an array of MAC addresses of the active cards.

#cs ----------------------------------------------------------------------------
AutoIt Version: v3.3.4.0
Author: MaxuN
Returns: Array[]
Script Function: Returns an array of MAC addresses of the active cards
#ce ----------------------------------------------------------------------------

#RequireAdmin
Func _GetMACAddress()
    Local $objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
    Local $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=true", "WQL", 0x10 + 0x20)
    Local $i = 0, $Before = ""
    Local $MACs[1]
    For $objItem in $colItems
        Local $item = $objItem.MACAddress
        If $Before <> $item Then
            $MACs[$i] = $item
            $Before   = $item
            $i += 1
        EndIf
        If (UBound($MACs)-1) = $i Then ReDim $MACs[UBound($MACs)]
    Next
    Return $MACs
EndFunc

I'm waiting your replys.

P.S.: Sorry my English.

P.S.1: How can I upload an "Attached File" ?. Thks

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Hello, Try this:

#include <Constants.au3>

MsgBox(0,"The mac Adress is",_GetMAC())

;Return Value:
;OK : The Mac Adress
;Wrong : Return ""
Func _GetMAC()
local $Result, $mac,$line

$mac = Run(@ComSpec & " /c " & 'GETMAC /FO table',@SystemDir, @SW_HIDE,$STDERR_CHILD + $STDOUT_CHILD)

While 1
    $line &= StdoutRead($mac)
    If @error Then ExitLoop
Wend
$line = StringSplit($line,@CRLF,1)
if @error then Return ""
$Result = StringSplit($line[4]," ",1)
if @error then Return ""

Return $Result[1]

EndFunc

P.S.: Sorry my English.

Edited by novii

Share this post


Link to post
Share on other sites

Hello, I seen the script for the GetMac message box. I was wondering if anyone could help me? I need the mac address of the computer and the computer name outputed to an excel file. I need to use this script on 200+ computers for inventory so the script would need to tab down to the next line after it ran on each computer. Could you help? Thank you!!

J

Share this post


Link to post
Share on other sites

The inventory scripts I write simply append the data to a file in CSV format, then just cut-n-paste or import into a spreadsheet. Done thousand of school PC's in hundreds of school classrooms in dozens of school districts.

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  
Followers 0