MAC地址扫描工具

效果演示图:

源代码:

#include <GUIConstantsEx.au3>
#include <GuiIPAddress.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <ButtonConstants.au3>

Opt("MustDeclareVars", 1)
Global $hGUI, $IPAdd0, $IPAdd1, $Button1, $Button2, $hListView, $hImage, $hProgress
Global $B_DESCENDING
Main()
Func Main()
Local $nMsg, $i, $n, $aIP0, $aIP1, $nIP, $nHost, $nMAC, $aItem
Local $iStep, $gross, $nOut
$hGUI = GUICreate("MAC 地址扫描器", 424, 320, -1, -1, BitOR($WS_CAPTION, $WS_SYSMENU), $WS_EX_CONTEXTHELP)
GUICtrlCreateGroup("扫描 MAC 地址", 7, 6, 410, 290)

GUICtrlCreateLabel("从", 21, 26, 16, 18, $SS_LEFT + $SS_CENTERIMAGE)
$IPAdd0 = _GUICtrlIpAddress_Create($hGUI, 36, 26, 110, 18)
_GUICtrlIpAddress_Set($IPAdd0, @IPAddress1)
GUICtrlCreateLabel("到", 152, 26, 16, 18, $SS_LEFT + $SS_CENTERIMAGE)
$IPAdd1 = _GUICtrlIpAddress_Create($hGUI, 170, 26, 110, 18)
_GUICtrlIpAddress_Set($IPAdd1, "0.0.0.254")
$Button1 = GUICtrlCreateCheckbox("扫描(&S)", 290, 25, 53, 20, $BS_PUSHLIKE)
$Button2 = GUICtrlCreateButton("保存(&Z)", 350, 25, 53, 20)
$hListView = GUICtrlCreateListView("", 21, 54, 382, 230);, BitOR($LVS_SHOWSELALWAYS, $LVS_NOSORTHEADER, $LVS_REPORT))
GUICtrlSendMsg($hListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
GUICtrlSendMsg($hListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_FULLROWSELECT, $LVS_EX_FULLROWSELECT)
GUICtrlSetState($hListView, $GUI_FOCUS)

_GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES, $LVS_EX_SUBITEMIMAGES, $LVS_SORTASCENDING))
$hImage = _GUIImageList_Create(16, 16, 5, 3)
_GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 18)
_GUICtrlListView_SetImageList($hListView, $hImage, 1)
_GUICtrlListView_AddColumn($hListView, "MAC 地址", 140)
_GUICtrlListView_AddColumn($hListView, "计算机名", 100)
_GUICtrlListView_AddColumn($hListView, "IP地址", 122)

$hProgress = GUICtrlCreateProgress(7, 300, 410, 18)

GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
Global $B_DESCENDING[_GUICtrlListView_GetColumnCount($hListView)]
While 1
$nMsg = GUIGetMsg()
$aIP0 = _GUICtrlIpAddress_GetArray($IPAdd0)
$aIP1 = _GUICtrlIpAddress_GetArray($IPAdd1)
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
Switch GUICtrlRead($Button1)
Case 1
_GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($hListView))
TCPStartup()
$n = 0
$iStep = 1
$nOut = ""
$gross = $aIP0[3] - $aIP1[3]
If $gross > 0 Then
$iStep = -1
Else
$gross = Abs($gross)
EndIf
$gross += 1
$aIP0[3] -= $iStep
Do
$aIP0[3] += $iStep
$nIP = $aIP0[0] & "." & $aIP0[1] & "." & $aIP0[2] & "." & $aIP0[3]
;_GUICtrlIpAddress_SetArray($IPAdd0, $aIP0)
WinSetTitle($hGUI, "", "正在扫描: " & $nIP)
GUICtrlSetData($hProgress, Int(100 * ($gross - Abs($aIP1[3] - $aIP0[3])) / $gross));进度条百分点
;Ping($nIP, 5)
;If @error Then ContinueLoop
$nMAC = _API_Get_NetworkAdapterMAC($nIP)
If @error Then ContinueLoop
$nHost = __TCPIpToName($nIP)
If @error Or Not $nHost Then ContinueLoop
;GUICtrlCreateListViewItem($nMAC & "|" & $nHost & "|" & $nIP, $hListView)
;GUICtrlSetImage(-1, "shell32.dll", 19)
_GUICtrlListView_AddItem($hListView, $nMAC);, 0)
_GUICtrlListView_AddSubItem($hListView, $n, $nHost, 1)
_GUICtrlListView_AddSubItem($hListView, $n, $nIP, 2)
$nOut = $nOut & $nMAC & "=" & $nHost & "|" & $nIP & @CRLF
$n += 1
Until GUICtrlRead($Button1) <> 1 Or $aIP0[3] = $aIP1[3] Or GUIGetMsg() = $GUI_EVENT_CLOSE
TCPShutdown()
GUICtrlSetState($Button1, 4)
GUICtrlSetData($hProgress, 0)
WinSetTitle($hGUI, "", "扫描完毕!共找到 " & $n & " 个 MAC 地址。")
Case Else
GUICtrlSetData($hProgress, 0)
EndSwitch
Case $Button2
$n = _GUICtrlListView_GetItemCount($hListView)
If Not $n Then
MsgBox(16, "对不起", "没有要保存的资料!")
Else
;MsgBox(0, $n, $nOut)
IniWrite("config.ini", "Mac2Ip", "最:后:扫:描:时:间", _
StringFormat("%4d-%1d-%1d, %1d:%02d:%02d", @YEAR, @MON, @MDAY, @HOUR, @MIN, @SEC))
For $i = 0 To $n - 1
$aItem = _GUICtrlListView_GetItemTextArray($hListView, $i)
If $aItem[0] <> 3 Then ContinueLoop
IniWrite("config.ini", "Mac2Ip", $aItem[1], $aItem[2] & "|" & $aItem[3])
Next
MsgBox(64, "提示", "保存完成!")
EndIf
Case Else
For $i = 0 To 2
If $aIP1[$i] >= $aIP0[$i] Then ContinueLoop
$aIP1[$i] = $aIP0[$i]
_GUICtrlIpAddress_SetArray($IPAdd1, $aIP1)
Next
EndSwitch
WEnd
EndFunc   ;==>Main

Func __TCPIpToName($sIp, $iOption = Default, $hDll_Ws2_32 = Default)
Local $vbinIP, $vaDllCall, $vptrHostent, $vHostent, $sHostnames, $vh_aliases, $i
Local $INADDR_NONE = 0xffffffff, $AF_INET = 2, $sSeperator = @CR
If $iOption = Default Then $iOption = 0
If $hDll_Ws2_32 = Default Then $hDll_Ws2_32 = "Ws2_32.dll"
$vaDllCall = DllCall($hDll_Ws2_32, "long", "inet_addr", "str", $sIp)
If @error Then Return SetError(1, 0, "") ; inet_addr DllCall Failed
$vbinIP = $vaDllCall[0]
If $vbinIP = $INADDR_NONE Then Return SetError(2, 0, "") ; inet_addr Failed
$vaDllCall = DllCall($hDll_Ws2_32, "ptr", "gethostbyaddr", "long*", $vbinIP, "int", 4, "int", $AF_INET)
If @error Then Return SetError(3, 0, "") ; gethostbyaddr DllCall Failed
$vptrHostent = $vaDllCall[0]
If $vptrHostent = 0 Then
$vaDllCall = DllCall($hDll_Ws2_32, "int", "WSAGetLastError")
If @error Then Return SetError(5, 0, "") ; gethostbyaddr Failed, WSAGetLastError Failed
Return SetError(4, $vaDllCall[0], "") ; gethostbyaddr Failed, WSAGetLastError = @Extended
EndIf
$vHostent = DllStructCreate("ptr;ptr;short;short;ptr", $vptrHostent)
$sHostnames = __TCPIpToName_szStringRead(DllStructGetData($vHostent, 1))
If @error Then Return SetError(6, 0, $sHostnames) ; strlen/sZStringRead Failed
If $iOption = 1 Then
$sHostnames &= $sSeperator
For $i = 0 To 63 ; up to 64 Aliases
$vh_aliases = DllStructCreate("ptr", DllStructGetData($vHostent, 2) + ($i * 4))
If DllStructGetData($vh_aliases, 1) = 0 Then ExitLoop ; Null Pointer
$sHostnames &= __TCPIpToName_szStringRead(DllStructGetData($vh_aliases, 1))
If @error Then
SetError(7) ; Error reading array
ExitLoop
EndIf
Next
Return StringSplit(StringStripWS($sHostnames, 2), @CR)
Else
Return $sHostnames
EndIf
EndFunc   ;==>__TCPIpToName

Func __TCPIpToName_szStringRead($iszPtr, $iLen = -1, $hDll_msvcrt = "msvcrt.dll")
Local $aStrLen, $vszString
If $iszPtr < 1 Then Return "" ; Null Pointer
If $iLen < 0 Then
$aStrLen = DllCall($hDll_msvcrt, "int:cdecl", "strlen", "ptr", $iszPtr)
If @error Then Return SetError(1, 0, "") ; strlen Failed
$iLen = $aStrLen[0] + 1
EndIf
$vszString = DllStructCreate("char[" & $iLen & "]", $iszPtr)
If @error Then Return SetError(2, 0, "")
Return SetError(0, $iLen, DllStructGetData($vszString, 1))
EndFunc   ;==>__TCPIpToName_szStringRead

Func _API_Get_NetworkAdapterMAC($sIp)
Local $MAC, $MACSize
Local $i, $s, $r, $iIP

$MAC = DllStructCreate("byte[6]")
$MACSize = DllStructCreate("int")

DllStructSetData($MACSize, 1, 6)
$r = DllCall("Ws2_32.dll", "int", "inet_addr", "str", $sIp)
$iIP = $r[0]
$r = DllCall("iphlpapi.dll", "int", "SendARP", "int", $iIP, "int", 0, "ptr", DllStructGetPtr($MAC), "ptr", DllStructGetPtr($MACSize))
$s = ""
For $i = 0 To 5
If $i Then $s = $s & ":"
$s = $s & Hex(DllStructGetData($MAC, 1, $i + 1), 2)
Next
If $s = "00:00:00:00:00:00"  Then SetError(1)
Return $s
EndFunc   ;==>_API_Get_NetworkAdapterMAC

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView
$hWndListView = $hListView
If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)

$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hWndListView
Switch $iCode
Case $LVN_COLUMNCLICK ; A column was clicked
Local $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
_GUICtrlListView_SimpleSort($hWndListView, $B_DESCENDING, DllStructGetData($tInfo, "SubItem"))
Case $LVN_KEYDOWN ; A key has been pressed
Local $tInfo = DllStructCreate($tagNMLVKEYDOWN, $ilParam)
Case $NM_CLICK ; Sent by a list-view control when the user clicks an item with the left mouse button
Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
Case $NM_DBLCLK ; Sent by a list-view control when the user double-clicks an item with the left mouse button
Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
Case $NM_KILLFOCUS ; The control has lost the input focus
Case $NM_RCLICK ; Sent by a list-view control when the user clicks an item with the right mouse button
Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
Return 0 ; allow the default processing
Case $NM_RDBLCLK ; Sent by a list-view control when the user double-clicks an item with the right mouse button
Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
Case $NM_RETURN ; The control has the input focus and that the user has pressed the ENTER key
Case $NM_SETFOCUS ; The control has received the input focus
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
申明:本站资源大部分来自网络,如果无意侵犯了你的权利,请及时通知我们 AutoIt@FoxMail.Com,我们将尽快处理
Copyright © 2008-2009 随便复制 共享精神 人人有责 All Right Reserved
粤ICP备08009024号