Jump to content

How to find MAC address for the active wifi adapter?


ASut
 Share

Recommended Posts

Hi,it's me again. We can use @IPAddress1 , @IPAddress2 ... to find the IP address, but how about the MAC address.

Or we need to read the output of the "ipconfig /all".

And how to distinguish the MAC address is wifi adapter or not?

Thanks in advance

Link to comment
Share on other sites

Try this for getting the MAC from an IP Address. Found it on the forum a long time ago.

Func _GetMACFromIP($sIP = @IPAddress1, $sSeparator = ":")
;Part of the code is from LazyCat in AutoIt Forums.
Local $MAC, $MACSize
Local $i, $s, $r, $iIP
;Create the struct
;{
;   char    data[6];
;}MAC
$MAC = DllStructCreate("byte[6]")
;Create a pointer to an int
;   int *MACSize;
$MACSize = DllStructCreate("int")
;*MACSize = 6;
DllStructSetData($MACSize, 1, 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 & $sSeparator ;Added Separator variable to modify output format
  $s = $s & Hex(DllStructGetData($MAC, 1, $i + 1), 2)
Next
;Must free the memory after it is used
$MAC = 0
$MACSize = 0
;Return the user readble MAC address
Return $s
EndFunc   ;==>_GetMACFromIP

If the WiFi adapter is the only one actively pulling a valid IP Address, not 127.0.0.1 or 0.0.0.0, then it should be the WIFI adapter's IP Address. You could also look at WMI Win32_NetworkAdapterConfiguration and Caption properties and loop through for "Wireless" in the name. Use the DefaultIPGateway and MACAddress properties to confirm that it is the correct adapter.

Look at this from the AutoIt Scriptomatic:

; Generated by AutoIt Scriptomatic August 30, 2011
 
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"
 
$Output=""
$Output &= "Computer: " & $strComputer  & @CRLF
$Output &= "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration", "WQL", _
                                        $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If IsObj($colItems) then
   For $objItem In $colItems
    $Output &= "ArpAlwaysSourceRoute: " & $objItem.ArpAlwaysSourceRoute & @CRLF
    $Output &= "ArpUseEtherSNAP: " & $objItem.ArpUseEtherSNAP & @CRLF
    $Output &= "Caption: " & $objItem.Caption & @CRLF
    $Output &= "DatabasePath: " & $objItem.DatabasePath & @CRLF
    $Output &= "DeadGWDetectEnabled: " & $objItem.DeadGWDetectEnabled & @CRLF
    $strDefaultIPGateway = $objItem.DefaultIPGateway(0)
    $Output &= "DefaultIPGateway: " & $strDefaultIPGateway & @CRLF
    $Output &= "DefaultTOS: " & $objItem.DefaultTOS & @CRLF
    $Output &= "DefaultTTL: " & $objItem.DefaultTTL & @CRLF
    $Output &= "Description: " & $objItem.Description & @CRLF
    $Output &= "DHCPEnabled: " & $objItem.DHCPEnabled & @CRLF
    $Output &= "DHCPLeaseExpires: " & WMIDateStringToDate($objItem.DHCPLeaseExpires) & @CRLF
    $Output &= "DHCPLeaseObtained: " & WMIDateStringToDate($objItem.DHCPLeaseObtained) & @CRLF
    $Output &= "DHCPServer: " & $objItem.DHCPServer & @CRLF
    $Output &= "DNSDomain: " & $objItem.DNSDomain & @CRLF
    $strDNSDomainSuffixSearchOrder = $objItem.DNSDomainSuffixSearchOrder(0)
    $Output &= "DNSDomainSuffixSearchOrder: " & $strDNSDomainSuffixSearchOrder & @CRLF
    $Output &= "DNSEnabledForWINSResolution: " & $objItem.DNSEnabledForWINSResolution & @CRLF
    $Output &= "DNSHostName: " & $objItem.DNSHostName & @CRLF
    $strDNSServerSearchOrder = $objItem.DNSServerSearchOrder(0)
    $Output &= "DNSServerSearchOrder: " & $strDNSServerSearchOrder & @CRLF
    $Output &= "DomainDNSRegistrationEnabled: " & $objItem.DomainDNSRegistrationEnabled & @CRLF
    $Output &= "ForwardBufferMemory: " & $objItem.ForwardBufferMemory & @CRLF
    $Output &= "FullDNSRegistrationEnabled: " & $objItem.FullDNSRegistrationEnabled & @CRLF
    $strGatewayCostMetric = $objItem.GatewayCostMetric(0)
    $Output &= "GatewayCostMetric: " & $strGatewayCostMetric & @CRLF
    $Output &= "IGMPLevel: " & $objItem.IGMPLevel & @CRLF
    $Output &= "Index: " & $objItem.Index & @CRLF
    $strIPAddress = $objItem.IPAddress(0)
    $Output &= "IPAddress: " & $strIPAddress & @CRLF
    $Output &= "IPConnectionMetric: " & $objItem.IPConnectionMetric & @CRLF
    $Output &= "IPEnabled: " & $objItem.IPEnabled & @CRLF
    $Output &= "IPFilterSecurityEnabled: " & $objItem.IPFilterSecurityEnabled & @CRLF
    $Output &= "IPPortSecurityEnabled: " & $objItem.IPPortSecurityEnabled & @CRLF
    $strIPSecPermitIPProtocols = $objItem.IPSecPermitIPProtocols(0)
    $Output &= "IPSecPermitIPProtocols: " & $strIPSecPermitIPProtocols & @CRLF
    $strIPSecPermitTCPPorts = $objItem.IPSecPermitTCPPorts(0)
    $Output &= "IPSecPermitTCPPorts: " & $strIPSecPermitTCPPorts & @CRLF
    $strIPSecPermitUDPPorts = $objItem.IPSecPermitUDPPorts(0)
    $Output &= "IPSecPermitUDPPorts: " & $strIPSecPermitUDPPorts & @CRLF
    $strIPSubnet = $objItem.IPSubnet(0)
    $Output &= "IPSubnet: " & $strIPSubnet & @CRLF
    $Output &= "IPUseZeroBroadcast: " & $objItem.IPUseZeroBroadcast & @CRLF
    $Output &= "IPXAddress: " & $objItem.IPXAddress & @CRLF
    $Output &= "IPXEnabled: " & $objItem.IPXEnabled & @CRLF
    $strIPXFrameType = $objItem.IPXFrameType(0)
    $Output &= "IPXFrameType: " & $strIPXFrameType & @CRLF
    $Output &= "IPXMediaType: " & $objItem.IPXMediaType & @CRLF
    $strIPXNetworkNumber = $objItem.IPXNetworkNumber(0)
    $Output &= "IPXNetworkNumber: " & $strIPXNetworkNumber & @CRLF
    $Output &= "IPXVirtualNetNumber: " & $objItem.IPXVirtualNetNumber & @CRLF
    $Output &= "KeepAliveInterval: " & $objItem.KeepAliveInterval & @CRLF
    $Output &= "KeepAliveTime: " & $objItem.KeepAliveTime & @CRLF
    $Output &= "MACAddress: " & $objItem.MACAddress & @CRLF
    $Output &= "MTU: " & $objItem.MTU & @CRLF
    $Output &= "NumForwardPackets: " & $objItem.NumForwardPackets & @CRLF
    $Output &= "PMTUBHDetectEnabled: " & $objItem.PMTUBHDetectEnabled & @CRLF
    $Output &= "PMTUDiscoveryEnabled: " & $objItem.PMTUDiscoveryEnabled & @CRLF
    $Output &= "ServiceName: " & $objItem.ServiceName & @CRLF
    $Output &= "SettingID: " & $objItem.SettingID & @CRLF
    $Output &= "TcpipNetbiosOptions: " & $objItem.TcpipNetbiosOptions & @CRLF
    $Output &= "TcpMaxConnectRetransmissions: " & $objItem.TcpMaxConnectRetransmissions & @CRLF
    $Output &= "TcpMaxDataRetransmissions: " & $objItem.TcpMaxDataRetransmissions & @CRLF
    $Output &= "TcpNumConnections: " & $objItem.TcpNumConnections & @CRLF
    $Output &= "TcpUseRFC1122UrgentPointer: " & $objItem.TcpUseRFC1122UrgentPointer & @CRLF
    $Output &= "TcpWindowSize: " & $objItem.TcpWindowSize & @CRLF
    $Output &= "WINSEnableLMHostsLookup: " & $objItem.WINSEnableLMHostsLookup & @CRLF
    $Output &= "WINSHostLookupFile: " & $objItem.WINSHostLookupFile & @CRLF
    $Output &= "WINSPrimaryServer: " & $objItem.WINSPrimaryServer & @CRLF
    $Output &= "WINSScopeID: " & $objItem.WINSScopeID & @CRLF
    $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

Edit: Fixed code output by AutoIt Scriptomatic. I missed some code when coping from the edit box. Sorry.

Adam

Edited by AdamUL
Link to comment
Share on other sites

Thanks for the help, the 1st method can return the MAC address base on the @IPAddress1,@IPAddress2,@IPAddress3,@IPAddress4

But the 2nd one is not work

C:\au3\test.au3(2,41) : WARNING: $strComputer: possibly used before declaration.
$Output &= "Computer: " & $strComputer  &
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\au3\test.au3(6,68) : WARNING: $wbemFlagReturnImmediately: possibly used before declaration.
                                        $wbemFlagReturnImmediately +
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\au3\test.au3(6,90) : WARNING: $wbemFlagForwardOnly: possibly used before declaration.
                                        $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\au3\test.au3(2,41) : ERROR: $strComputer: undeclared global variable.
$Output &= "Computer: " & $strComputer  &
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\au3\test.au3 - 1 error(s), 3 warning(s)
Link to comment
Share on other sites

Put these lines to the top of the script:

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

and it should run.

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Hello ASut,

for $strComputer, you've missed to assign the value for the local computer (@ComputerName)

For the other two variables, you've forgotten to add some includes.

Edit: too slow. :mellow:

Edited by hannes08
Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

Try this:

Global $oErrorHandler = ObjEvent("AutoIt.Error", "ObjErrorHandler")
 
MsgBox(0, "Test", WMI_GetWirelessMAC("localhost"))
 
Func WMI_GetWirelessMAC($host, $usr = "", $pass = "") ;coded by UEZ 2011
Local $ping = Ping($host, 1000)
If @error Then SetError(1, 0, 0)
Local $objWMILocator = ObjCreate("WbemScripting.SWbemLocator")
Local $objWMIService = $objWMILocator.ConnectServer($host, "\root\cimv2", $usr, $pass, "", "", "&H80")
If @error Then Return SetError(2, 0, 0)
Local $colItems = $objWMIService.ExecQuery("SELECT Description,MACAddress FROM Win32_NetworkAdapter WHERE NetConnectionID LIKE '%Wireless%'", "WQL", 0x30)
If IsObj($colItems) Then
Local $MAC
For $objItem In $colItems
$MAC &= $objItem.Description & "|" & $objItem.MACAddress & @LF
Next
Return $MAC
Else
Return SetError(3, 0, 0)
EndIf
Return 0
EndFunc
 
Func ObjErrorHandler()
ConsoleWrite( "A COM Error has occured!" & @CRLF  & @CRLF & _
"err.description is: "    & @TAB & $oErrorHandler.description    & @CRLF & _
"err.windescription:"     & @TAB & $oErrorHandler & @CRLF & _
"err.number is: "         & @TAB & Hex($oErrorHandler.number, 8)  & @CRLF & _
"err.lastdllerror is: "   & @TAB & $oErrorHandler.lastdllerror   & @CRLF & _
"err.scriptline is: "     & @TAB & $oErrorHandler.scriptline     & @CRLF & _
"err.source is: "         & @TAB & $oErrorHandler.source         & @CRLF & _
"err.helpfile is: "       & @TAB & $oErrorHandler.helpfile       & @CRLF & _
"err.helpcontext is: "    & @TAB & $oErrorHandler.helpcontext & @CRLF _
)
EndFunc

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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
 Share

  • Recently Browsing   0 members

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