找回密码
 加入
搜索
查看: 2768|回复: 5

如何获取整个局域网(多个网段)的MAC

[复制链接]
发表于 2009-5-3 16:07:36 | 显示全部楼层 |阅读模式
怎样在局域网的一台机器上,获取整个局域网(多个网段)内的所有机器的MAC。
    本机网段内的已经实现。
发表于 2009-5-16 17:30:48 | 显示全部楼层
楼主能不能把本网段的代码贴出来与大家分享?
 楼主| 发表于 2009-5-19 17:56:20 | 显示全部楼层
book1.xls内,A列为网段内IP,192.168.1.1 ....... 192.168.1.255
            扫描到的MAC保存在B列。

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include <StaticConstants.au3>
#include <Excel.au3>
#include <Sound.au3>

$oExcel = _ExcelBookOpen("d:\book1.xls")
If @error = 1 Then
        MsgBox(0, "Error!", "Unable to Create the Excel Object")
        Exit
ElseIf @error = 2 Then
        MsgBox(0, "Error!", "File does not exist - Shame on you!")
        Exit
EndIf

$TheFrist1 = 1
While 1
        $sIP = _ExcelReadCell($oExcel, $TheFrist1, 1)
        If $sIP <> "" Then
                $TheFrist1 = $TheFrist1 + 1
        Else
                $TheEnd1 = $TheFrist1 - 1
                $TheFrist1 = 1
                ExitLoop
        EndIf
WEnd

GUICreate("IP To MAC", 360, 430, 0, 0)
$ListView1 = GUICtrlCreateEdit("", 0, 0, 350, 390)
$Font = "Verdana"
GUICtrlSetFont(-1, 10, 800, 0, $Font)
$Button1 = GUICtrlCreateButton("开始", 95, 400, 75, 25, 0)
$Button2 = GUICtrlCreateButton("退出", 190, 400, 75, 25, 0)
GUISetState(@SW_SHOW)

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        _ExcelBookSaveAs($oExcel, "D:\book1.xls", "xls", 0, 1)
                        _ExcelBookClose($oExcel, 1)
                        Exit
                Case $Button1
                        IPMAC()
                Case $Button2
                        _ExcelBookSaveAs($oExcel, "D:\book1.xls", "xls", 0, 1)
                        _ExcelBookClose($oExcel, 1)
                        Exit
        EndSwitch
WEnd

Func IPMAC()
        For $Strat = $TheFrist1 To $TheEnd1
                $sIP = _ExcelReadCell($oExcel, $strat, 1)
                $MAC = _GetMAC($sIP)
                If $MAC <> "00:00:00:00:00:00" Then
                        $OldMAC=_ExcelReadCell($oExcel, $strat, 2)
                        If $OldMAC<>"" And $OldMAC<>$MAC Then
                                _ExcelWriteCell($oExcel, $MAC, $strat, 3)
                        Else
                                _ExcelWriteCell($oExcel, $MAC, $strat, 2)
                        EndIf
                        Addlog($sIP & " -> " & $MAC)
                Else
                        Addlog($sIP)
                EndIf
        Next
EndFunc   ;==>Timer

Func Addlog($lcStr)
        GUICtrlSetData($ListView1, GUICtrlRead($ListView1) & @CRLF & $lcStr)
        GUICtrlSetData($ListView1, " ", True)
EndFunc   ;==>Addlog

Func _GetMAC($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
        Return $s
EndFunc   ;==>_GetMAC
发表于 2009-5-20 00:38:37 | 显示全部楼层
这个应该有人会需要的,谢谢楼主的分享
发表于 2009-5-21 10:46:30 | 显示全部楼层
感谢楼主分享!
发表于 2009-5-21 23:51:37 | 显示全部楼层
站位,已备用。。。。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-23 03:12 , Processed in 0.083499 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表