Jump to content

Is it possible to detect internet connections to wired and wireless adapters separately?


Recommended Posts

I have a solution but I need testers, and anyone who can improve it.  Guinness I tested your code again today and it returned the Virtualbox adapter instead of my wireless, so I came up with something different.

 

UPDATE 5/4/15: There was a computer I tested it on that had duplicate entries in the registry which caused the script to error, so I added a redim $outputar so the script wouldn't error, and the function would reflect the duplicate.  In the future it would be cool if the function would identify the adapter thats in use and skip over the duplicate entry, which I think it should be possible.

 

#include <Array.au3>
Global $NetAdapterInterface = GetAdapterAndConType()
_ArrayDisplay($NetAdapterInterface, 'output')

Func GetAdapterAndConType()
    Local $NetAdapterAr = GetTangibleiNetAdapters(), _;get network adapters with ip Addresses and not virutal box
            $NetworkAdapterKey = "HKLM64\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}", _;this key seems to be static
            $NetAdapterKeyAr = regsubkeys($NetworkAdapterKey), _
            $loopAdapter = 0, _
            $InterfaceType = 0, _
            $Outputar[$NetAdapterAr[0] - 1][2], _
            $outputcnt = 0
    For $i = 0 To UBound($NetAdapterKeyAr) - 1
        $loopAdapter = RegRead($NetworkAdapterKey & "\" & $NetAdapterKeyAr[$i], "DriverDesc")
        For $l = 0 To $NetAdapterAr[0] - 1
            If $loopAdapter = $NetAdapterAr[$l] Then
                $InterfaceType = RegRead($NetworkAdapterKey & "\" & $NetAdapterKeyAr[$i] & "\Ndi\Interfaces", "LowerRange")
                If StringInStr($InterfaceType, "wifi") Or StringInStr($InterfaceType, "wlan") Then
                    ;wireless
if $outputcnt = $NetAdapterAr[0] - 1 then ReDim $Outputar[$outputcnt + 1][2] ;if duplicate entries exist in your registry then this will prevent the script from error
                    $Outputar[$outputcnt][0] = $loopAdapter
                    $Outputar[$outputcnt][1] = "Wireless"
                    $outputcnt += 1
                ElseIf StringInStr($InterfaceType, "ethernet") And Not StringInStr($InterfaceType, "wifi") And Not StringInStr($InterfaceType, "wlan") Then
                    ;wired
if $outputcnt = $NetAdapterAr[0] - 1 then ReDim $Outputar[$outputcnt + 1][2] ;if duplicate entries exist in your registry then this will prevent the script from error
                    $Outputar[$outputcnt][0] = $loopAdapter
                    $Outputar[$outputcnt][1] = "Wired"
                    $outputcnt += 1
                EndIf
            EndIf
        Next
    Next
    Return $Outputar
EndFunc   ;==>GetAdapterAndConType

Func GetTangibleiNetAdapters()
    ;Generated by AutoIt Scriptomatic
    Local $wbemFlagReturnImmediately = 0x10, _
            $wbemFlagForwardOnly = 0x20, _
            $colItems = "", _
            $strComputer = "localhost", _
            $Output = "", _
            $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration", "WQL", _
            $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj($colItems) Then
        For $objItem In $colItems
            If StringInStr($objItem.IPAddress(0), '.') And Not StringInStr($objItem.Description, "virtual") Then
                $Output = $Output & $objItem.Description & @CR
            EndIf
        Next
    Else
        MsgBox(0, "WMI Output", "No WMI Objects Found for class: " & "Win32_NetworkAdapterConfiguration")
    EndIf
    Local $returnar = StringSplit($Output, @CR)
    _ArrayDelete($returnar, $returnar[0]);delete extra @cr
    Return $returnar
EndFunc   ;==>GetTangibleiNetAdapters

Func regsubkeys($key)
    Local $sSubKey[50000], _
            $i = 1
    While 1
        $sSubKey[$i - 1] = RegEnumKey($key, $i)
        $i += 1
        If @error Then ExitLoop
    WEnd
    ReDim $sSubKey[$i - 1]
    Return $sSubKey
EndFunc   ;==>regsubkeys

 

Edited by TouchOdeath
To prevent function from breaking on invalid registry settings
Link to comment
Share on other sites

@ TouchOdeath

 Code seems to work fine m8 and simple enough that i can add my regkey addition straight into it
So thanks very helpfull

@ guinness

Ive started to find odd occasions where it doesn't give consistent results so ill keep it to one side for now as its not as simple as i thought clearly :)
Many thanks for your time though

Link to comment
Share on other sites

I guessed the code wouldn't be perfect.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

I'm glad it worked, It seems to work fine for me as well.  If it wasn't for Chimaera and guinness's code that sparked my brain, I wouldn't have been able to do it.  argumentum, I tried converting some of the code but am having troubles, great find for sure tho.  This is alittle offtopic, but I've started on some of the work from arumentum's link, here it is for anyone who wants to figure out the rest:

 

GetWiFi_AdapterAssocStatsInfo()
Func GetWiFi_AdapterAssocStatsInfo();

    Local Const $WbemUser = '';
    Local Const $WbemPassword = '';
    Local Const $WbemComputer = 'localhost';
    Local Const $wbemFlagForwardOnly = 0x20;
    ;-------I dono-------------;
  local $FSWbemLocator : OLEVariant;
  local $FWMIService   : OLEVariant;
  local $FWbemObjectSet: OLEVariant;
  local $FWbemObject   : OLEVariant;
  local $oEnum         : IEnumvariant;
  local $iValue        : LongWord;
  ;----------end----------;
    $FSWbemLocator = ObjCreate('WbemScripting.SWbemLocator');
    $FWMIService = $FSWbemLocator.ConnectServer($WbemComputer, 'root\CIMV2', $WbemUser, $WbemPassword);
    $FWbemObjectSet = $FWMIService.ExecQuery('SELECT * FROM WiFi_AdapterAssocStats', 'WQL', $wbemFlagForwardOnly);
    ;-------I dono-------------;
  $oEnum = IUnknown($FWbemObjectSet._NewEnum) as IEnumVariant;
  while $oEnum.Next(1, $FWbemObject, $iValue) = 0
  ;---------end-----------;
    consolewrite($FWbemObject.ApDidNotTx);// Uint32
    consolewrite($FWbemObject.ApMacAddr);// String
    consolewrite($FWbemObject.Caption);// String
    consolewrite($FWbemObject.CrcErrs);// Uint32
    consolewrite($FWbemObject.Description);// String
    consolewrite($FWbemObject.DroppedByAp);// Uint32
    consolewrite($FWbemObject.LoadBalancing);// Uint32
    consolewrite($FWbemObject.LowRssi);// Uint32
    consolewrite($FWbemObject.NumAps);// Uint32
    consolewrite($FWbemObject.NumAssociations);// Uint32
    consolewrite($FWbemObject.NumFullScans);// Uint32
    consolewrite($FWbemObject.NumPartialScans);// Uint32
    consolewrite($FWbemObject.PercentMissedBeacons);// Uint32
    consolewrite($FWbemObject.PercentTxErrs);// Uint32
    consolewrite($FWbemObject.PoorBeaconQuality);// Uint32
    consolewrite($FWbemObject.PoorChannelQuality);// Uint32
    consolewrite($FWbemObject.RoamCount);// Uint32
    consolewrite($FWbemObject.Rssi);// String
    consolewrite($FWbemObject.RxBeacons);// Uint32
    consolewrite($FWbemObject.SettingID);// String
    consolewrite($FWbemObject.TxRetries);// Uint32
    $FWbemObject=0;
  WEnd
EndFunc   ;==>GetWiFi_AdapterAssocStatsInfo

 

Edited by TouchOdeath
Link to comment
Share on other sites

I only have a lenovo S10 laptop with server 2003, so there is a limitation on what I can test here at home, but this code did work:
 

; Generated by AutoIt Scriptomatic

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

$Output=""
$Output = $Output & "Computer: " & $sComputer  & @CRLF
$oWMIService = ObjGet("winmgmts:\\" & $sComputer & "\root\WMI")
$colItems = $oWMIService.ExecQuery("SELECT * FROM MSNdis_80211_ReceivedSignalStrength", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) Then
   For $objItem In $colItems
      $Output = $Output & "Active: " & $objItem.Active & @CRLF
      $Output = $Output & "InstanceName: " & $objItem.InstanceName & @CRLF
      $Output = $Output & "Ndis80211ReceivedSignalStrength: " & $objItem.Ndis80211ReceivedSignalStrength & @CRLF
   Next
   ConsoleWrite($Output)
   FileWrite(@TempDir & "\MSNdis_80211_ReceivedSignalStrength.TXT", $Output )
   Run(@ComSpec & " /c start " & @TempDir & "\MSNdis_80211_ReceivedSignalStrength.TXT" )
Else
   MsgBox(0,"WMI Output","No WMI Objects Found for class: " & "MSNdis_80211_ReceivedSignalStrength" )
EndIf

maybe you can use it, as in

#include <Array.au3>

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

$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
Local $a[1000][6]
$a[0][0] = 0
If IsObj($colItems) then
    ConsoleWrite( UBound( $colItems ) &@CRLF )

    For $objItem In $colItems
        If $objItem.IPConnectionMetric Then
            If Not $a[0][0] Then
            ;   $a[0][0] = "$objItem.IPConnectionMetric"
                $a[0][1] = "$objItem.Description"
                $a[0][2] = "$objItem.MACAddress"
                $a[0][3] = "$objItem.IPAddress(0)"
                $a[0][4] = "$objItem.SettingID"
                $a[0][5] = "Is_WiFi"
            EndIf

            $a[0][0] = $a[0][0] + 1
            $a[ $a[0][0] ][0] = $objItem.IPConnectionMetric
            $a[ $a[0][0] ][1] = $objItem.Description
            $a[ $a[0][0] ][2] = $objItem.MACAddress
            $a[ $a[0][0] ][3] = $objItem.IPAddress(0)
            $a[ $a[0][0] ][4] = $objItem.SettingID
            $a[ $a[0][0] ][5] = 0


            ConsoleWrite(  $objItem.Description &@TAB& "IPConnectionMetric: " & $objItem.IPConnectionMetric & @CRLF )

            $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
            $Output = $Output & "InterfaceIndex: " & $objItem.InterfaceIndex & @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
            ConsoleWrite( $Output &@CRLF )
            $Output = ""
        EndIf
;~       $Output = $Output & "IPConnectionMetric: " & $objItem.IPConnectionMetric & @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

ReDim $a[ $a[0][0] + 1 ][6]

Local $n
$oWMIService = ObjGet("winmgmts:\\" & $sComputer & "\root\WMI")
$colItems = $oWMIService.ExecQuery("SELECT * FROM MSNdis_80211_ReceivedSignalStrength", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If IsObj($colItems) Then
    ConsoleWrite('--------------- MSNdis_80211_ReceivedSignalStrength ---------------'&@CRLF)
    For $objItem In $colItems
        $Output = $Output & "Active: " & $objItem.Active & @CRLF
        $Output = $Output & "InstanceName: " & $objItem.InstanceName & @CRLF
        $Output = $Output & "Ndis80211ReceivedSignalStrength: " & $objItem.Ndis80211ReceivedSignalStrength & @CRLF
        For $n = 1 To $a[0][0]
            If $a[$n][1] = $objItem.InstanceName Then $a[$n][5] = 1
        Next

;~      If $objItem.InstanceName
    Next
    ConsoleWrite($Output)
    ConsoleWrite('--------------- ----------------------------------- ---------------'&@CRLF)
   ;FileWrite(@TempDir & "\MSNdis_80211_ReceivedSignalStrength.TXT", $Output )
   ;Run(@ComSpec & " /c start " & @TempDir & "\MSNdis_80211_ReceivedSignalStrength.TXT" )
Else
   MsgBox(0,"WMI Output","No WMI Objects Found for class: " & "MSNdis_80211_ReceivedSignalStrength" )
EndIf

_ArrayDisplay($a,"IPConnectionMetric")

the code is ugly, but it shows what I mean.

 

Edited by argumentum
to add the 2nd part of the code

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

  • 5 months later...

Not entirely happy with my post above, I have found another/better answer:

#include <Array.au3>
; Generated by AutoIt Scriptomatic

Local $Output[1][3], $colItems = "", $objWMIService

$objWMIService = ObjGet("winmgmts:\\")
$colItems = $objWMIService.ExecQuery("select Name, MacAddress, NetConnectionID from Win32_NetworkAdapter where NetEnabled = 'True'")

If IsObj($colItems) Then
    Local $i = 0
    For $objItem In $colItems
        $Output[$i][0] = $objItem.Name
        $Output[$i][1] = $objItem.MacAddress
        $Output[$i][2] = $objItem.NetConnectionID
        $i += 1
        ReDim $Output[$i + 1][3]
    Next
EndIf
_ArrayDelete($Output, $i)
_ArrayDisplay($Output)

NetEnabled means that the Adapter is atleast established a connection (I haven't tested if its it has to be connected to the internet, but I'm pretty sure it just means connected to the router).  So say for example, I have a wired network adapter.  If I unplug the network cable from the adapter, NetEnabled will be False for that adapter.  As soon as I plug the RJ45 back in, NetEnabled = True for that adapter.  All you have to do now is Parse column 3 of the output to determine if its wired or wireless, if  stringinstr "Wireless" then wireless, if stringinstr "Local Area" then wired, if stringinstr "Virtual" then virtual.

EDIT:

If you want the adapter name, IP, MAC, wired/wireless all in one go, here it is:

MsgBox(0,'',__GetConnectedAdapterIPMAC())


Func __GetConnectedAdapterIPMAC()
    Local $Output = "", $colItems = "", $objWMIService

    $objWMIService = ObjGet("winmgmts:\\")
    $colItems = $objWMIService.ExecQuery("select Description, IPAddress, MacAddress, Index from Win32_NetworkAdapterConfiguration where IPEnabled = 1")

    If IsObj($colItems) Then
        Local $NetworkAdapterKey = "HKLM64\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}", $InterfaceType, _
        $NetAdapterKeyAr = regsubkeys($NetworkAdapterKey)
        For $objItem In $colItems
            $InterfaceType = RegRead($NetworkAdapterKey & "\" & $NetAdapterKeyAr[$objItem.Index] & "\Ndi\Interfaces", "LowerRange")
            If StringInStr($InterfaceType, "wifi") Or StringInStr($InterfaceType, "wlan") Then
                $InterfaceType = "Wireless"
            ElseIf StringInStr($InterfaceType, "ethernet") And Not StringInStr($InterfaceType, "wifi") And Not StringInStr($InterfaceType, "wlan") Then
                $InterfaceType = "Wired"
            EndIf
            $Output &= $objItem.Description & " | " & $objItem.IPAddress(0) & " | " & $objItem.MacAddress & " | " & $InterfaceType & @CRLF
        Next
    EndIf
    $Output = StringLeft($Output, (StringLen($Output) - 2)) ;remove last CRLF
    Return $Output
EndFunc   ;==>__GetConnectedAdapterIPMAC

Func regsubkeys($key)
    Local $sSubKey[50000], _
            $i = 1
    While 1
        $sSubKey[$i - 1] = RegEnumKey($key, $i)
        $i += 1
        If @error Then ExitLoop
    WEnd
    ReDim $sSubKey[$i - 1]
    Return $sSubKey
EndFunc   ;==>regsubkeys

 This is probably the best implementation, another good implementation would be this:  getting adapter name, mac, NetConnectionID through Win32_NetworkAdapter and within that loop, select * from Win32_NetworkAdapterConfiguration where IPEnabled = 1 and Description = 'adapternamefromWin32_NetworkAdapter' and grab the IPaddress.

I didn't go through the above paragraph because I figured regread would be faster than using WMI, and it would be easier to write (laziness mwuahah).  However the above paragraph might be more accurate/fail safe.

Edited by TouchOdeath
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...