Jump to content
Sign in to follow this  
dannydy

Display IP Address

Recommended Posts

#include <Array.au3>
#include <Constants.au3>

Local $aArray = _IPDetails()
If @error = 0 Then
    Local $sData = 'Your IP Address: ' & $aArray[1] & @CRLF & _
            'Your Gateway Address: ' & $aArray[3] & @CRLF & _
            'You DNS Servers: ' & $aArray[4]
    MsgBox($MB_SYSTEMMODAL, '', $sData)
EndIf
_ArrayDisplay($aArray)

Func _IPDetails()
    Local $oWMIService = ObjGet('winmgmts:{impersonationLevel = impersonate}!\\' & '.' & '\root\cimv2')
    Local $oColItems = $oWMIService.ExecQuery('Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True', 'WQL', 0x30), $aReturn[5] = [0]
    If IsObj($oColItems) Then
        For $oObjectItem In $oColItems
            If $oObjectItem.IPAddress(0) == @IPAddress1 Then
                $aReturn[0] = 4
                $aReturn[1] = $oObjectItem.IPAddress(0)
                $aReturn[2] = $oObjectItem.MACAddress
                $aReturn[3] = $oObjectItem.DefaultIPGateway(0)
                $aReturn[4] = _WMIArrayToString($oObjectItem.DNSServerSearchOrder(), ', ') ; You could use _ArrayToString() but I like creating my own Functions especially when I don't need alot of error checking.
            EndIf
        Next
    EndIf
    Return SetError($aReturn[0] = 0, 0, $aReturn)
EndFunc   ;==>_IPDetails

Func _WMIArrayToString($aArray, $sDelimeter = '|')
    Local $sString = 'Not Available'
    If UBound($aArray) Then
        For $i = 0 To UBound($aArray) - 1
            $sString &= $aArray[$i] & $sDelimeter
        Next
        $sString = StringTrimRight($sString, StringLen($sDelimeter))
    EndIf
    Return $sString
EndFunc   ;==>_WMIArrayToString

This is the code I found on AutoIT Forum, its worked on Wired but not wireless. Could anyone help me on this, please? 

Share this post


Link to post
Share on other sites

Try this:

$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
        ConsoleWrite("WMI Output" & $Output)
        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   ;==>WMIDateStringToDate

Share this post


Link to post
Share on other sites

 

Any difference using >this method to gather the IPs?

 

This is really useful, But how do i get the Default Gateway.

And how do i get the value from col 2. (I just want the ip adress, Default Gateway and MAC)

i amend it with trial and error, but no luck i couldnt get what i want.

#include <array.au3>

$aTest = GetNics()
_ArrayDisplay($aTest)

Func GetNics()
Local $NETWORK_REG_KEY = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\"
Local $tagIP_ADDRESS_STRING = "char IPAddress[16];"
Local $tagIP_MASK_STRING = "char IPMask[16];"
Local $tagIP_ADDR_STRING = "ptr Next;" & $tagIP_ADDRESS_STRING & $tagIP_MASK_STRING & "DWORD Context;"
Local $tagIP_ADAPTER_INFO = "ptr Next; DWORD ComboIndex; char AdapterName[260];char Description[132]; UINT AddressLength; BYTE Address[8]; dword Index; UINT Type;" & _
" UINT DhcpEnabled; ptr CurrentIpAddress; ptr IpAddressListNext; char IpAddressListADDRESS[16]; char IpAddressListMASK[16]; DWORD IpAddressListContext; " & _
"ptr GatewayListNext; char GatewayListADDRESS[16]; char GatewayListMASK[16]; DWORD GatewayListContext; " & _
"ptr DhcpServerNext; char DhcpServerADDRESS[16]; char DhcpServerMASK[16]; DWORD DhcpServerContext; " & _
"int HaveWins; " & _
"ptr PrimaryWinsServerNext; char PrimaryWinsServerADDRESS[16]; char PrimaryWinsServerMASK[16]; DWORD PrimaryWinsServerContext; " & _
"ptr SecondaryWinsServerNext; char SecondaryWinsServerADDRESS[16]; char SecondaryWinsServerMASK[16]; DWORD SecondaryWinsServerContext; " & _
"DWORD LeaseObtained; DWORD LeaseExpires;"
Local $dll = DllOpen("Iphlpapi.dll")
Local $ret = DllCall($dll, "dword", "GetAdaptersInfo", "ptr", 0, "dword*", 0)
Local $adapterBuffer = DllStructCreate("byte[" & $ret[2] & "]")
Local $adapterBuffer_pointer = DllStructGetPtr($adapterBuffer)
Local $return = DllCall($dll, "dword", "GetAdaptersInfo", "ptr", $adapterBuffer_pointer, "dword*", $ret[2])
Local $adapter = DllStructCreate($tagIP_ADAPTER_INFO, $adapterBuffer_pointer)
If Not @error Then
Dim $aAdapters[100][4]
$i = 0
Do
$aAdapters[$i][0] = DllStructGetData($adapter, "Description")
$aAdapters[$i][1] = RegRead($NETWORK_REG_KEY & DllStructGetData($adapter, "AdapterName") & "\Connection", "Name")
$aAdapters[$i][2] = DllStructGetData(DllStructCreate($tagIP_ADDR_STRING, DllStructGetPtr($adapter, "IpAddressListNext")), "IPAddress")
$aAdapters[$i][3] = DllStructGetData(DllStructCreate($tagIP_ADDR_STRING, DllStructGetPtr($adapter, "IpAddressListNext")), "IPMask")
$i += 1
$ptr = DllStructGetData($adapter, "Next")
$adapter = DllStructCreate($tagIP_ADAPTER_INFO, $ptr)
Until @error
ReDim $aAdapters[$i][4]
EndIf
Return $aAdapters
EndFunc ;==>GetNics

Share this post


Link to post
Share on other sites

Added gateway and MAC

NOTE: title="">All thanks to the work done by ProgAndy in this thread.

I think a rewrite of ProgAndy's work dumping all of this data to an array instead of XML would be pretty useful.  I may work on that when time allows.

#include <array.au3>

$aTest = GetNics()
_ArrayDisplay($aTest)


Func GetNics()
    Local $NETWORK_REG_KEY = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\"
    Local $tagIP_ADDRESS_STRING = "char IPAddress[16];"
    Local $tagIP_MASK_STRING = "char IPMask[16];"
    Local $tagIP_ADDR_STRING = "ptr Next;" & $tagIP_ADDRESS_STRING & $tagIP_MASK_STRING & "DWORD Context;"
    Local $tagIP_ADAPTER_INFO = "ptr Next; DWORD ComboIndex; char AdapterName[260];char Description[132]; UINT AddressLength; BYTE Address[8]; dword Index; UINT Type;" & _
            " UINT DhcpEnabled; ptr CurrentIpAddress; ptr IpAddressListNext; char IpAddressListADDRESS[16]; char IpAddressListMASK[16]; DWORD IpAddressListContext; " & _
            "ptr GatewayListNext; char GatewayListADDRESS[16]; char GatewayListMASK[16]; DWORD GatewayListContext; " & _
            "ptr DhcpServerNext; char DhcpServerADDRESS[16]; char DhcpServerMASK[16]; DWORD DhcpServerContext; " & _
            "int HaveWins; " & _
            "ptr PrimaryWinsServerNext; char PrimaryWinsServerADDRESS[16]; char PrimaryWinsServerMASK[16]; DWORD PrimaryWinsServerContext; " & _
            "ptr SecondaryWinsServerNext; char SecondaryWinsServerADDRESS[16]; char SecondaryWinsServerMASK[16]; DWORD SecondaryWinsServerContext; " & _
            "DWORD LeaseObtained; DWORD LeaseExpires;"
    Local $dll = DllOpen("Iphlpapi.dll")
    Local $ret = DllCall($dll, "dword", "GetAdaptersInfo", "ptr", 0, "dword*", 0)
    Local $adapterBuffer = DllStructCreate("byte[" & $ret[2] & "]")
    Local $adapterBuffer_pointer = DllStructGetPtr($adapterBuffer)
    Local $return = DllCall($dll, "dword", "GetAdaptersInfo", "ptr", $adapterBuffer_pointer, "dword*", $ret[2])
    Local $adapter = DllStructCreate($tagIP_ADAPTER_INFO, $adapterBuffer_pointer)
    Local $ptr2 = DllStructGetPtr($adapter, "GatewayListNext")
    Local $BinaryMAC
    If Not @error Then
        Dim $aAdapters[100][6]
        $i = 0
        Do
            ;Description / Name / IP / Subnet Mask
            $aAdapters[$i][0] = DllStructGetData($adapter, "Description")
            $aAdapters[$i][1] = RegRead($NETWORK_REG_KEY & DllStructGetData($adapter, "AdapterName") & "\Connection", "Name")
            $aAdapters[$i][2] = DllStructGetData(DllStructCreate($tagIP_ADDR_STRING, DllStructGetPtr($adapter, "IpAddressListNext")), "IPAddress")
            $aAdapters[$i][3] = DllStructGetData(DllStructCreate($tagIP_ADDR_STRING, DllStructGetPtr($adapter, "IpAddressListNext")), "IPMask")

            ;Gateway
            $IPStruct = DllStructCreate($tagIP_ADDR_STRING, $ptr2)
            $aAdapters[$i][4] = DllStructGetData($IPStruct, "IPAddress")

            ;MAC
            $BinaryMAC = BinaryMid(DllStructGetData($adapter, "Address"), 1, DllStructGetData($adapter, "AddressLength"))
            $aAdapters[$i][5] = StringTrimRight(StringRegExpReplace(StringTrimLeft($BinaryMAC, 2), "([[:xdigit:]]{2})", "$1:"), 1)

            $i += 1
            $ptr = DllStructGetData($adapter, "Next")
            $adapter = DllStructCreate($tagIP_ADAPTER_INFO, $ptr)
            $ptr2 = DllStructGetPtr($adapter, "GatewayListNext")
        Until @error
        ReDim $aAdapters[$i][6]
    EndIf
    Return $aAdapters
EndFunc   ;==>GetNics

Share this post


Link to post
Share on other sites

I have a UDF dedicated to Network Interface information and statistics that returns not only Network card info, but special interfaces, IPv6 addresses, etc. >Network Interface Info, Statistics, and Traffic


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  

×
×
  • Create New...