Sign in to follow this  
Followers 0
ChrisL

Getting Mac Addresses

13 posts in this topic

If any one is interested..

#include <Constants.au3>
$line = ""
Dim $macs[1]
$foo = Run(@ComSpec & " /c ipconfig /all", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

While 1
    $line = $line & StdoutRead($foo)
    If @error = -1 Then ExitLoop
Wend
$line = StringSplit ($line, @crlf)

For $i = 1 to Ubound ($line) - 1
    ;MsgBox (0,"",$line[$i])
    If StringinStr ($line[$i], "Physical Address. . . . . . . . . : ") then _ArrayAdd ($macs, StringStripWS ( StringReplace ($line[$i] ,"Physical Address. . . . . . . . . : ",""), 8 ))
    Next
    
    For $i = 1 to Ubound ($macs)-1
        MsgBox(0, "STDOUT read:", $Macs[$i])
    Next
    
MsgBox(0, "Debug", "Exiting...")


Func _ArrayAdd(ByRef $avArray, $sValue)
    If IsArray($avArray) Then
        ReDim $avArray[UBound($avArray) + 1]
        $avArray[UBound($avArray) - 1] = $sValue
        ;MsgBox (0,"","Redim")
        SetError(0)
        Return 1
    Else
        ;MsgBox (0,"","Error")
        SetError(1)
        Return 0
    EndIf
EndFunc   ;==>_ArrayAdd

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

@Wouter

Maybe this can help you out.

; ----------------------------------------------------------------------------
;
;
;
; ----------------------------------------------------------------------------

#include <bk-logfile.au3>
#include <date.au3>

Dim $strComputer = "."
Dim $objWMIService, $colAdapters, $n
Dim $utcLeaseObtained, $utcLeaseObtained, $utcLeaseExpired,  $strLeaseObtained, $utcLeaseExpires, $strLeaseExpires


 $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")

 $colAdapters = $objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True") 

$n = 1

 
For $objAdapter in $colAdapters
   ConsoleWrite ("Network Adapter " & $n & @CR)
   ConsoleWrite ("================="& @CR)
   ConsoleWrite ("  Description: " & $objAdapter.Description& @CR)
 
   ConsoleWrite ("  Physical (MAC) address: " & $objAdapter.MACAddress& @CR)
   ConsoleWrite ("  Host name:              " & $objAdapter.DNSHostName& @CR)
 
   If ($objAdapter.IPAddress) Then
      For $i = 0 To UBound($objAdapter.IPAddress)
         ConsoleWrite ("  IP address:             " & $objAdapter.IPAddress($i)& @CR)
      Next
   EndIf
 
   If ($objAdapter.IPSubnet) Then
      For $i = 0 To UBound($objAdapter.IPSubnet)
         ConsoleWrite ("  Subnet:                 " & $objAdapter.IPSubnet($i)& @CR)
      Next
   EndIf
 
   If ($objAdapter.DefaultIPGateway) Then
      For $i = 0 To UBound($objAdapter.DefaultIPGateway)
         ConsoleWrite ("  Default gateway:        " & _
             $objAdapter.DefaultIPGateway($i)& @CR)
      Next
   EndIf
 
   ConsoleWrite ("  DNS"& @CR)
   ConsoleWrite ("  ---"& @CR)
   ConsoleWrite ("    DNS servers in search order:"& @CR)
 
   If ($objAdapter.DNSServerSearchOrder) Then
      For $i = 0 To UBound($objAdapter.DNSServerSearchOrder)
         ConsoleWrite ("      " & $objAdapter.DNSServerSearchOrder($i)& @CR)
      Next
   EndIf
 
   ConsoleWrite ("    DNS domain: " & $objAdapter.DNSDomain& @CR)
 
   If ($objAdapter.DNSDomainSuffixSearchOrder) Then
      For $i = 0 To UBound($objAdapter.DNSDomainSuffixSearchOrder)
         ConsoleWrite ("    DNS suffix search list: " & _
             $objAdapter.DNSDomainSuffixSearchOrder($i)& @CR)
      Next
   EndIf
 
   ConsoleWrite ("  DHCP"& @CR)
   ConsoleWrite ("  ----"& @CR)
   ConsoleWrite ("    DHCP enabled:        " & $objAdapter.DHCPEnabled& @CR)
   ConsoleWrite ("    DHCP server:         " & $objAdapter.DHCPServer& @CR)
 
   If  ($objAdapter.DHCPLeaseObtained) Then
      $utcLeaseObtained = $objAdapter.DHCPLeaseObtained
      $strLeaseObtained = WMIDateStringToDate($utcLeaseObtained)
   Else
      $strLeaseObtained = ""
   EndIf
   ConsoleWrite ("    DHCP lease obtained: " & $strLeaseObtained& @CR)
 
   If ($objAdapter.DHCPLeaseExpires) Then
      $utcLeaseExpires = $objAdapter.DHCPLeaseExpires
      $strLeaseExpires = WMIDateStringToDate($utcLeaseExpires)
   Else
      $strLeaseExpires = ""
   EndIf
   ConsoleWrite ("    DHCP lease expires:  " & $strLeaseExpires& @CR)
 
   ConsoleWrite ("  WINS"& @CR)
   ConsoleWrite ("  ----"& @CR)
   ConsoleWrite ("    Primary WINS server:   " & $objAdapter.WINSPrimaryServer& @CR)
   ConsoleWrite ("    Secondary WINS server: " & $objAdapter.WINSSecondaryServer& @CR)
 
   $n = $n + 1
 
Next
 
Func WMIDateStringToDate($utcDate)
    Local $Return
   $Return = (StringMid($utcDate, 5, 2)  & "/" & _
       StringMid($utcDate, 7, 2)  & "/" & _
           StringLeft($utcDate, 4)    & " " & _
               StringMid ($utcDate, 9, 2) & ":" & _
                   StringMid($utcDate, 11, 2) & ":" & _
                      StringMid($utcDate, 13, 2))
    Return $Return
EndFunc

Output is like IPconfig data.

Enjoy.

Edited by ptrex

Share this post


Link to post
Share on other sites

HI,

the second one works, but you do not need this line, do you?

#include <bk-logfile.au3>

So long,

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

DOESNT WORK !!!!!!

lol could not resist. Ipconfig is localised.

why not use regexp to just capture the MAC ?

Oh, works on the few machines I tried it on. :D

As for regexp I have never understood it, I've read posts on here and also the break down of it but I just don't get it, I guess it's one of those functions that untill you have used it a few times it just doesn't make sense!

I did look at the WMI stuff but there was a good reason for not using it, the computer ghost image supports 3 models of hardware and about 6 or 7 different network cards which all show up in WMI even though they are not present hense why I used the above method, it worked for what I needed so I'm happy :D

Share this post


Link to post
Share on other sites

Added this to my FTP :D

Nice work


--------------------------------------------------------------------------------------------------------------------------------Scripts : _Encrypt UDF_UniquePCCode UDF MS like calculatorInstall programm *UPDATED* --------------------------------------------------------------------------------------------------------------------------------[quote name='Helge' post='213117' date='Jul 26 2006, 10:22 AM']Have you ever tried surfing the internet with a milk-carton ?This is similar to what you're trying to do.[/quote]

Share this post


Link to post
Share on other sites

why not use regexp to just capture the MAC ?

w0uter, please show us how to do this with regexp's... :D

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format

Share this post


Link to post
Share on other sites

If any one is interested..

Can you determine which mac id belongs to the wireless network card and which to the wired card?

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format

Share this post


Link to post
Share on other sites

Can you determine which mac id belongs to the wireless network card and which to the wired card?

You can find out lots with WMI

;Scriptomatic code

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=True", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
      $Output = $Output & "ArpAlwaysSourceRoute: " & $objItem.ArpAlwaysSourceRoute & @CRLF
      $Output = $Output & "ArpUseEtherSNAP: " & $objItem.ArpUseEtherSNAP & @CRLF
      $Output = $Output & "Caption: " & $objItem.Caption & @CRLF
      $Output = $Output & "DatabasePath: " & $objItem.DatabasePath & @CRLF
      $Output = $Output & "DeadGWDetectEnabled: " & $objItem.DeadGWDetectEnabled & @CRLF
      $strDefaultIPGateway = $objItem.DefaultIPGateway(0)
      $Output = $Output & "DefaultIPGateway: " & $strDefaultIPGateway & @CRLF
      $Output = $Output & "DefaultTOS: " & $objItem.DefaultTOS & @CRLF
      $Output = $Output & "DefaultTTL: " & $objItem.DefaultTTL & @CRLF
      $Output = $Output & "Description: " & $objItem.Description & @CRLF
      $Output = $Output & "DHCPEnabled: " & $objItem.DHCPEnabled & @CRLF
      $Output = $Output & "DHCPLeaseExpires: " & WMIDateStringToDate($objItem.DHCPLeaseExpires) & @CRLF
      $Output = $Output & "DHCPLeaseObtained: " & WMIDateStringToDate($objItem.DHCPLeaseObtained) & @CRLF
      $Output = $Output & "DHCPServer: " & $objItem.DHCPServer & @CRLF
      $Output = $Output & "DNSDomain: " & $objItem.DNSDomain & @CRLF
      $strDNSDomainSuffixSearchOrder = $objItem.DNSDomainSuffixSearchOrder(0)
      $Output = $Output & "DNSDomainSuffixSearchOrder: " & $strDNSDomainSuffixSearchOrder & @CRLF
      $Output = $Output & "DNSEnabledForWINSResolution: " & $objItem.DNSEnabledForWINSResolution & @CRLF
      $Output = $Output & "DNSHostName: " & $objItem.DNSHostName & @CRLF
      $strDNSServerSearchOrder = $objItem.DNSServerSearchOrder(0)
      $Output = $Output & "DNSServerSearchOrder: " & $strDNSServerSearchOrder & @CRLF
      $Output = $Output & "DomainDNSRegistrationEnabled: " & $objItem.DomainDNSRegistrationEnabled & @CRLF
      $Output = $Output & "ForwardBufferMemory: " & $objItem.ForwardBufferMemory & @CRLF
      $Output = $Output & "FullDNSRegistrationEnabled: " & $objItem.FullDNSRegistrationEnabled & @CRLF
      $strGatewayCostMetric = $objItem.GatewayCostMetric(0)
      $Output = $Output & "GatewayCostMetric: " & $strGatewayCostMetric & @CRLF
      $Output = $Output & "IGMPLevel: " & $objItem.IGMPLevel & @CRLF
      $Output = $Output & "Index: " & $objItem.Index & @CRLF
      $strIPAddress = $objItem.IPAddress(0)
      $Output = $Output & "IPAddress: " & $strIPAddress & @CRLF
      $Output = $Output & "IPConnectionMetric: " & $objItem.IPConnectionMetric & @CRLF
      $Output = $Output & "IPEnabled: " & $objItem.IPEnabled & @CRLF
      $Output = $Output & "IPFilterSecurityEnabled: " & $objItem.IPFilterSecurityEnabled & @CRLF
      $Output = $Output & "IPPortSecurityEnabled: " & $objItem.IPPortSecurityEnabled & @CRLF
      $strIPSecPermitIPProtocols = $objItem.IPSecPermitIPProtocols(0)
      $Output = $Output & "IPSecPermitIPProtocols: " & $strIPSecPermitIPProtocols & @CRLF
      $strIPSecPermitTCPPorts = $objItem.IPSecPermitTCPPorts(0)
      $Output = $Output & "IPSecPermitTCPPorts: " & $strIPSecPermitTCPPorts & @CRLF
      $strIPSecPermitUDPPorts = $objItem.IPSecPermitUDPPorts(0)
      $Output = $Output & "IPSecPermitUDPPorts: " & $strIPSecPermitUDPPorts & @CRLF
      $strIPSubnet = $objItem.IPSubnet(0)
      $Output = $Output & "IPSubnet: " & $strIPSubnet & @CRLF
      $Output = $Output & "IPUseZeroBroadcast: " & $objItem.IPUseZeroBroadcast & @CRLF
      $Output = $Output & "IPXAddress: " & $objItem.IPXAddress & @CRLF
      $Output = $Output & "IPXEnabled: " & $objItem.IPXEnabled & @CRLF
      $strIPXFrameType = $objItem.IPXFrameType(0)
      $Output = $Output & "IPXFrameType: " & $strIPXFrameType & @CRLF
      $Output = $Output & "IPXMediaType: " & $objItem.IPXMediaType & @CRLF
      $strIPXNetworkNumber = $objItem.IPXNetworkNumber(0)
      $Output = $Output & "IPXNetworkNumber: " & $strIPXNetworkNumber & @CRLF
      $Output = $Output & "IPXVirtualNetNumber: " & $objItem.IPXVirtualNetNumber & @CRLF
      $Output = $Output & "KeepAliveInterval: " & $objItem.KeepAliveInterval & @CRLF
      $Output = $Output & "KeepAliveTime: " & $objItem.KeepAliveTime & @CRLF
      $Output = $Output & "MACAddress: " & $objItem.MACAddress & @CRLF
      $Output = $Output & "MTU: " & $objItem.MTU & @CRLF
      $Output = $Output & "NumForwardPackets: " & $objItem.NumForwardPackets & @CRLF
      $Output = $Output & "PMTUBHDetectEnabled: " & $objItem.PMTUBHDetectEnabled & @CRLF
      $Output = $Output & "PMTUDiscoveryEnabled: " & $objItem.PMTUDiscoveryEnabled & @CRLF
      $Output = $Output & "ServiceName: " & $objItem.ServiceName & @CRLF
      $Output = $Output & "SettingID: " & $objItem.SettingID & @CRLF
      $Output = $Output & "TcpipNetbiosOptions: " & $objItem.TcpipNetbiosOptions & @CRLF
      $Output = $Output & "TcpMaxConnectRetransmissions: " & $objItem.TcpMaxConnectRetransmissions & @CRLF
      $Output = $Output & "TcpMaxDataRetransmissions: " & $objItem.TcpMaxDataRetransmissions & @CRLF
      $Output = $Output & "TcpNumConnections: " & $objItem.TcpNumConnections & @CRLF
      $Output = $Output & "TcpUseRFC1122UrgentPointer: " & $objItem.TcpUseRFC1122UrgentPointer & @CRLF
      $Output = $Output & "TcpWindowSize: " & $objItem.TcpWindowSize & @CRLF
      $Output = $Output & "WINSEnableLMHostsLookup: " & $objItem.WINSEnableLMHostsLookup & @CRLF
      $Output = $Output & "WINSHostLookupFile: " & $objItem.WINSHostLookupFile & @CRLF
      $Output = $Output & "WINSPrimaryServer: " & $objItem.WINSPrimaryServer & @CRLF
      $Output = $Output & "WINSScopeID: " & $objItem.WINSScopeID & @CRLF
      $Output = $Output & "WINSSecondaryServer: " & $objItem.WINSSecondaryServer & @CRLF
      if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
      $Output=""
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_NetworkAdapterConfiguration" )
Endif


Func WMIDateStringToDate($dtmDate)

    Return (StringMid($dtmDate, 5, 2) & "/" & _
    StringMid($dtmDate, 7, 2) & "/" & StringLeft($dtmDate, 4) _
    & " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate,13, 2))
EndFunc

Share this post


Link to post
Share on other sites

If you're just looking for the mac address, why not just put the following command into a command prompt, and do a line search for the physical address.

netsh interface ip show interface "Local Area Connection"

Writing AutoIt scripts since
_DateAdd("d", -2, _NowCalcDate())

Share this post


Link to post
Share on other sites

If you're just looking for the mac address, why not just put the following command into a command prompt, and do a line search for the physical address.

netsh interface ip show interface "Local Area Connection"
That won't work if their local machines remote access and routing isn't running... will it?

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

I don't trust WMI, at least not yet. Too many COM errors seem to occur when trying to use it. I prefer the (what I would call) more traditional way of using DOS-type commands and reading their output to get the information.

;**********************************************************************
Func _MACAddress($sIP)
   Local $vLines, $aParts, $sMAC = ''

   If (StringLen($sIP) > 0) Then
      If _IsPingable($sIP) Then
         $vLines = _PipeDOS('nbtstat -A ' & $sIP, 'find /I "MAC Address"')
         $vLines = StringSplit(StringStripCR($vLines),@LF)
         If $vLines[0] > 0 Then
            $aParts = StringSplit(StringStripWS($vLines[1], 8), "=")
            If $aParts[0] = 2 Then
               $sMAC = $aParts[2]
            EndIf
         EndIf
      EndIf
   EndIf
   Return $sMAC
EndFunc

;**********************************************************************
Func _IPAddress($sNode)
   Local $vLines, $aParts, $sIP = ''

   If (StringLen($sNode) > 0) Then
      $vLines = _PipeDOS('Ping -n 1 ' & $sNode, 'FindStr /i /r /c:"Ping statistics for"')
      $vLines = StringSplit(StringStripCR($vLines),@LF)
      If $vLines[0] > 0 Then
         $aParts = StringSplit($vLines[1],' ')
         If $aParts[0] = 4 Then
            $sIP = StringLeft($aParts[4],StringLen($aParts[4])-1)
         EndIf
      EndIf
   EndIf
   Return $sIP
EndFunc

;**********************************************************************
Func _IsPingable($sNode)
   Return (Ping($sNode, 1000) > 0)
EndFunc   ;==>_IsPingable

;**********************************************************************
; Run a "DOS" command, taking it's input from another "DOS" command
; Returns the text of the output of the second command.
; @Error is set to the exit code of the first command.
; @Extended is set to the exit code of the second command.
;**********************************************************************
Func _PipeDOS($sCommand1, $sCommand2)
   Local $sOutput1, $sOutput2, $Terror

   $sOutput1 = _RunDOSo($sCommand1)
   $Terror = @Error
   $sOutput2 = _RunDOSio($sCommand2, $sOutput1)
   SetExtended(@Error)
   SetError($Terror)
   Return $sOutput2
EndFunc   ;==>_PipeDOS

;**********************************************************************
; Run a "DOS" command, taking it's input from a string
; Returns the text of the output.
;**********************************************************************
Func _RunDOSio($sCommand, $sInput)
   Local $sResult, $sTempFile = _TempFile()

   FileWrite($sTempFile, $sInput)
   $sResult = _RunDOSo($sCommand & ' <' & _Quote($sTempFile))
   FileDelete($sTempFile)
   Return $sResult
EndFunc   ;==>_RunDOSio

;**********************************************************************
; Run a "DOS" command and capture the output into a file.
; Returns the output of the command.
;**********************************************************************
Func _RunDOSo($sCommand)
   Local $sTempOutput, $sResult = ''

   $sTempOutput = _TempFile()
   SetError(_RunDOS($sCommand & ' >' & _Quote($sTempOutput)))
   $sResult = FileRead($sTempOutput, FileGetSize($sTempOutput))
   FileDelete($sTempOutput)
   Return $sResult
EndFunc   ;==>_RunDOSo

;**********************************************************************
; Run a "DOS" command without showing any window.
;**********************************************************************
Func _RunDOS($sCommand)
   Return RunWait(@ComSpec & ' /C' & $sCommand, @WorkingDir, @SW_HIDE)
EndFunc   ;==>_RunDOS

;**********************************************************************
Func _Quote($sTheString, $sQuoteChar = '"')
   Return ($sQuoteChar & $sTheString & $sQuoteChar)
EndFunc

;**********************************************************************
; Generate a name for a temporary file, making sure the file does
; not already exist.
;**********************************************************************
Func _TempFile($sExtension = 'tmp')
   Local $sTempName
   If StringLeft($sExtension, 1) <> '.' Then
      $sExtension = '.' & $sExtension
   EndIf
   Do
      $sTempName = "~"
      While StringLen($sTempName) < 8
         $sTempName = $sTempName & Chr(Round(Random(97, 122), 0))
      Wend
      $sTempName = @TempDir & "\" & $sTempName & $sExtension
   Until Not FileExists($sTempName)
   Return ($sTempName)
EndFunc   ;==>_TempFile

My Projects:DebugIt - Debug your AutoIt scripts with DebugIt!

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