Что нового

[Сеть, интернет] Получение ARP таблицы через вызов Iphlpapi.dll

Houl777

Знающий
Сообщения
38
Репутация
5
AutoIt: 3.3
Версия: 3.3.8.1

Категория: Сеть, Система.

Описание:

Получение таблицы ARP через вызов Iphlpapi.dll.
Нативно, зависаний на x64 не вызывает, в отличии от arp -a.

Код/Пример:
Код:
PrintIpNetTable()


Func PrintIpNetTable()

    ; Start with initial call to calculate the size of the sructure
    Local $aCall = DllCall("iphlpapi.dll", "dword", "GetIpNetTable", _
            "ptr*", 0, _
            "dword*", 0, _
            "bool", 1)
    ; Check for errors
    If @error Then Return SetError(1, 0, 0)
    If $aCall[0] <> 122 Then Return SetError(2, 0, 0) ; ERROR_INSUFFICIENT_BUFFER

    ; Read the size
    Local $iSize = $aCall[2]
    ; Allocate that much
    Local $tByteStructure = DllStructCreate("byte[" & $iSize & "]")
    ; Get pointer
    Local $pPointer = DllStructGetPtr($tByteStructure)

    ; Now fill the struct
    $aCall = DllCall("iphlpapi.dll", "dword", "GetIpNetTable", _
            "ptr", $pPointer, _
            "dword*", $iSize, _
            "bool", 1)

    ; Interpret as modified MIB_IPNETTABLE structure to read the number of entries
    Local $tMIB_IPNETTABLE_NumEnries = DllStructCreate("dword dwNumEntries;", $pPointer)
    ; Read that number
    Local $iNumEntries = DllStructGetData($tMIB_IPNETTABLE_NumEnries, "dwNumEntries")

    $pPointer += 4 ; skip the size of that dword

    Local Const $MAXLEN_PHYSADDR = 8
    Local $tMIB_IPNETROW

    ; Loop through the array of structures printing read data
    For $i = 0 To $iNumEntries - 1
        $tMIB_IPNETROW = DllStructCreate("dword dwIndex;" & _
                "dword dwPhysAddrLen;" & _
                "byte bPhysAddr[" & $MAXLEN_PHYSADDR & "];" & _
                "dword dwAddr;" & _
                "dword dwType;", _
                $pPointer)

        ; Print read
        ConsoleWrite("+Index " & DllStructGetData($tMIB_IPNETROW, "dwIndex") & ":" & @CRLF & _
                @TAB & "Physical address = " & BinToMAC(DllStructGetData($tMIB_IPNETROW, "bPhysAddr"), DllStructGetData($tMIB_IPNETROW, "dwPhysAddrLen")) & @CRLF & _
                @TAB & "IPv4 address = " & NumToIP(DllStructGetData($tMIB_IPNETROW, "dwAddr")) & @CRLF & _
                @TAB & "Type = " & ARPType(DllStructGetData($tMIB_IPNETROW, "dwType")) & @CRLF & @CRLF)

        ; Skip this struct and continue looping
        $pPointer += DllStructGetSize($tMIB_IPNETROW)
    Next

    ; Bye, bye...
EndFunc   ;==>PrintIpNetTable


; Few helper functions
Func NumToIP($iIP)
    Return Int(BinaryMid($iIP, 1, 1)) & "." & Int(BinaryMid($iIP, 2, 1)) & "." & Int(BinaryMid($iIP, 3, 1)) & "." & Int(BinaryMid($iIP, 4, 1))
EndFunc

Func BinToMAC($bIn, $iSeg)
    If $iSeg = 0 Then Return "00-00-00-00-00-00"
    Local $sOut
    For $i = 1 To $iSeg
        $sOut &= Hex(BinaryMid($bIn, $i, 1)) & "-"
    Next
    Return StringTrimRight($sOut, 1)
EndFunc

Func ARPType($iType)
    Local Static $aType[5] = ["", "Unknown", "Invalid", "Dynamic", "Static"]
    If $iType < 1 Or $iType > 4 Then $iType = 1
    Return $aType[$iType]
EndFunc

История версий:
1.0 Final


Автор(ы): trancexx c форума http://www.autoitscript.com (за что ему личная благодарность :beer:smile:
 
Верх