找回密码
 加入
搜索
查看: 4613|回复: 8

[AU3基础] 求助DHCP UDF 谁给几个应用的事例?

  [复制链接]
发表于 2010-3-9 17:29:47 | 显示全部楼层 |阅读模式
本帖最后由 lsq726 于 2010-5-8 10:36 编辑

http://www.autoitscript.com/forum/index.php?showtopic=74295
这是原始连接

这个东西要怎么用? 能给几个例子。
#include <Date.au3>
中文是按照2楼朋友翻译添加的。

;===============================================================================
;
; Description:      Get DHCP Client Attributes by IP Address, MAC or Name
;描述:找IP地址,MAC或DHCP客户端属性名称
; Parameter(s):     $sDHCP - IP Address of DHCP Server
;参数(补):$ sDHCP - DHCP服务器的IP地址 
;                   $ClientID - Client IP, MAC or Name (String or Native Format)
;~ $的ClientID - 客户端IP,MAC或名称(String或本机格式)
;                     Integer - native format for IP Address/Mask
;整数 - 原始格式为IP地址/掩码 
;                     Binary  - native format for MAC Address (6 bytes)
;二进制 - 原生格式的MAC地址(6字节) 
;                   $iIDType - type of ClientID information:
;~ $ iIDType - 类型的ClientID信息: 
;                     0 - Auto-define by value
; 0 - 自动定义值 
;                     1 - Client IP address
; 1 - 客户端IP地址 
;                     2 - Client Hardware (MAC) address
;2 - 客户端硬件(MAC)地址 
;                     3 - Client Name
; 3 - 客户名称
;                   $iFlags - Config Flags:
; 配置标志: 
;                     0x1- Output Format (0-Text, 1-Native)
;0x1 -输出格式(0 -文本,1 -母语)
; Requirement(s):   Testing
;要求:测试 
; Return Value(s):  On Success - The array of parameters with indexes
;返回值:成功 - 与指标参数数组
;                     0 - Client IP address
; 0 - 客户端IP地址
;                     1 - Client IP subnet mask
; 1 - 客户端IP子网掩码
;                     2 - Client MAC address
;2 - 客户端MAC地址 
;                     3 - Client Name
; 3 - 客户名称
;                     4 - Client Comment
; 4 - 客户评论 
;                     5 - Client Lease Expires Time
; 5 - 客户端租约到期时间 
;                   On Failure - Null, @error set to
;失效 - 空,设置为错误
;                     1 - Client not found on DHCP server
; 1 - 客户未找到DHCP服务器 
;                     2 - Invalid ClientID parameter
;~ 2 - 无效的ClientID参数 
;                     3 - Operating System not supported
; 3 - 操作系统不支持 
;                     4 - Any Runtime Error, API Error Code set to @extended
;                                          4 - 任何运行时错误,错误代码的API设置为扩展 
; Author(s):         amel27 (Alexander Melnichuk)
;~ 作者:amel27
; Note(s):           Client Name is case sensitive
;注:                                客户名称是区分大小写 
;===============================================================================

Func _DHCP_GetClientInfo($sDHCP, $ClientID, $iIDType=0, $iFlags=0)
    ; Create DHCP_SEARCH_INFO Structure
    Local $tSearchInfo= DllStructCreate("int SearchType;int DataLength;ptr DataPtr")
    Local $iSearchInfoPtr = DllStructGetPtr($tSearchInfo), $aSubnets[2]=[1,0], $iType=0
    Local $tBinaryData = DllStructCreate("dword SubnetIPAddress;ubyte HardwareID;ubyte MACAddress[6]")
    ; Check Client ID Parameter and Define SearchInfo Type
    If IsInt($ClientID) Then
        If $iIDType=0 Or $iIDType=1 Then $iType = 1
    ElseIf IsBinary($ClientID) Then
        If BinaryLen($ClientID)=6 And ($iIDType=0 Or $iIDType=2) Then $iType = 2
    ElseIf IsString($ClientID) Then
        If StringRegExp($ClientID, "^(\d+\.){3}\d+$") Then
            ; Get IP DWord type from String
            Local $aOctets = StringSplit($ClientID, "."), $iClientIP = 0
            For $i=1 To 4
                If BitAND($aOctets[$i], 0xFFFFFF00) Then Return SetError(2) ; ERR: Invalid Client IP Address
                $iClientIP = BitOR(BitRotate($iClientIP, 8, "D"), $aOctets[$i])
            Next
            $ClientID = $iClientIP
            If $iIDType=0 Or $iIDType=1 Then $iType = 1
        ElseIf StringRegExp($ClientID,"^(0[xX])?[[:xdigit:]]{2}((:|-)?[[:xdigit:]]{2}){5}$") Then
            $ClientID = Binary("0x"& StringRegExpReplace($ClientID,"(0[xX]|:|-)",""))
            If $iIDType=0 Or $iIDType=2 Then $iType = 2
        Else
            If $iIDType=0 Or $iIDType=3 Then $iType = 3
        EndIf
    EndIf
    If $iType =0 Then Return SetError(2)
    ; Route the filling of DHCP_SEARCH_INFO structure
    Switch $iType
        Case 1
            ; Filling DHCP_SEARCH_INFO for search by client IP address
            Local $tSearchInfo_IP = DllStructCreate("int SearchType;dword ClientIPAddress", $iSearchInfoPtr)
            DllStructSetData($tSearchInfo_IP, "SearchType", 0)
            DllStructSetData($tSearchInfo_IP, "ClientIPAddress", $ClientID)
        Case 2
            ; Filling DHCP_SEARCH_INFO for search by client MAC address
            DllStructSetData($tSearchInfo, "SearchType", 1)
            DllStructSetData($tSearchInfo, "DataLength", 11)
            DllStructSetData($tSearchInfo, "DataPtr", DllStructGetPtr($tBinaryData))
            ; Filling DHCP_BINARY_DATA
            DllStructSetData($tBinaryData, "HardwareID", 0x01)
            DllStructSetData($tBinaryData, "MACAddress", $ClientID)
            ; Get Array of DHCP Subnets
            $aSubnets = _DHCP_EnumSubnets($sDHCP, 1)
            If @error=1 Then Return SetError(1)
            If @error=2 Then Return SetError(4, @extended)
        Case 3
            ; Filling DHCP_SEARCH_INFO for search by client Name
            Local $tSearchInfo_Name = DllStructCreate("int SearchType;ptr ClientNamePtr", $iSearchInfoPtr)
            Local $tClientNameString= DllStructCreate("wchar ClientName["& StringLen($ClientID)+1 &"]")
            DllStructSetData($tSearchInfo_Name, "SearchType", 2)
            DllStructSetData($tSearchInfo_Name, "ClientNamePtr", DllStructGetPtr($tClientNameString))
            DllStructSetData($tClientNameString, "ClientName", $ClientID)
    EndSwitch
    ; Call  DhcpGetClientInfo API function
    Local $tClientInfo_Ptr = DllStructCreate("ptr"), $aRet, $aRes[6]
    For $i=1 To $aSubnets[0]
        DllStructSetData($tBinaryData, "SubnetIPAddress", $aSubnets[$i])
        $aRet = DllCall("Dhcpsapi.dll", "int", "DhcpGetClientInfo", _
            "wstr", $sDHCP, _ 
            "ptr", $iSearchInfoPtr, _
            "ptr", DllStructGetPtr($tClientInfo_Ptr) )
        If @error Then Return SetError(3, @error) ; ERR: Invalid DLL or Function Name
        If $aRet[0]<>20013 Then ExitLoop
    Next
    If $aRet[0]=20013 Then Return SetError(1, $aRet[0]) ; ERR: Client not found
    If $aRet[0] Then Return SetError(4, $aRet[0]) ; ERR: Any runtime errors
    ; DHCP_CLIENT_INFO structure
    Local $tClientInfo = DllStructCreate("dword ClientIpAddress;dword SubnetMask;int BinaryLen;ptr BinaryPtr;" & _
        "ptr ClientNamePtr;ptr ClientCommentPtr;ubyte ClientLeaseExpires[8];dword OwnerHostIPAddress;" & _
        "ptr OwnerHostNetBiosNamePtr;ptr OwnerHostNamePtr", DllStructGetData($tClientInfo_Ptr,1))
    Local $tClientBinary = DllStructCreate("ubyte BinaryData[6]", DllStructGetData($tClientInfo, "BinaryPtr"))
    ; Get IP Address
    $aRes[0] = DllStructGetData($tClientInfo, "ClientIpAddress")
    If BitAND($iFlags,1)=0 Then $aRes[0] = BitRotate(BitAND($aRes[0],0xFF000000), 8,"D") &"."& _
        BitRotate(BitAND($aRes[0],0x00FF0000),16,"D") &"."& _
        BitRotate(BitAND($aRes[0],0x0000FF00),-8,"W") &"."& BitAND($aRes[0],0x000000FF)
    ; Get IP Mask
    $aRes[1] = DllStructGetData($tClientInfo, "SubnetMask")
    If BitAND($iFlags,1)=0 Then $aRes[1]=BitRotate(BitAND($aRes[1],0xFF000000), 8,"D") &"."& _
        BitRotate(BitAND($aRes[1],0x00FF0000),16,"D") &"."& _
        BitRotate(BitAND($aRes[1],0x0000FF00),-8,"W") &"."& BitAND($aRes[1],0x000000FF)
    ; Get MAC Address
    $aRes[2] = DllStructGetData($tClientBinary, "BinaryData")
    If BitAND($iFlags,1)=0 Then $aRes[2] = String($aRes[2])
    ; Get Client Name
    Local $tClientNameString = DllStructCreate("wchar ClientName[255]", DllStructGetData($tClientInfo, "ClientNamePtr"))
    $aRes[3] = DllStructGetData($tClientNameString, "ClientName")
    ; Get Client Comment
    Local $tClientNameString = DllStructCreate("wchar ClientComment[255]", DllStructGetData($tClientInfo, "ClientCommentPtr"))
    $aRes[4] = DllStructGetData($tClientNameString, "ClientComment")
    ; Get Client Lease Expire Time
    $aRes[5] = DllStructGetData($tClientInfo, "ClientLeaseExpires")
    If BitAND($iFlags,1)=0 Then
        $aRes[5] = _Date_Time_FileTimeToLocalFileTime(DllStructGetPtr($tClientInfo, "ClientLeaseExpires"))
        $aRes[5] = _Date_Time_FileTimeToStr($aRes[5])
    EndIf
    ; Freeing a memory
    DllCall("Dhcpsapi.dll", "none", "DhcpRpcFreeMemory", "ptr", DllStructGetData($tClientInfo_Ptr,1))
    Return $aRes
EndFunc ;==> _DHCP_GetClientInfo


;===============================================================================
;
; Description:      Get List of DHCP Scopes
; Parameter(s):     $sDHCP - IP Address of DHCP Server
;                   $iFlags - Config Flags:
;                     0x1- Output Format (0-Text, 1-Native)
; Requirement(s):   Testing
; Return Value(s):  On Success - The array subnet of IP Addresses
;                     element with index 0 is count of scopes
;                   On Failure - @error set to
;                     1 - Scopes not defined, returned empty array
;                     2 - Any Runtime Error, invalid array,
;                         API Error Code set to @extended
; Author(s):         amel27 (Alexander Melnichuk)
; Note(s):           
;
;===============================================================================
Func _DHCP_EnumSubnets($sDHCP, $iFlags=0)
    Local $tEnumSubnetsParms = DllStructCreate("hwnd ResumeHandle;ptr EnumInfoPtr;int ElementsRead;int ElementsTotal")
    Local $aSubnets[1]=[0], $tIPArray, $tAddress, $aRet
    Do
        $aRet = DllCall("Dhcpsapi.dll", "int", "DhcpEnumSubnets", _
            "wstr", $sDHCP, _
            "ptr" , DllStructGetPtr($tEnumSubnetsParms, "ResumeHandle"), _
            "int", 100, _
            "ptr" , DllStructGetPtr($tEnumSubnetsParms, "EnumInfoPtr") , _
            "ptr" , DllStructGetPtr($tEnumSubnetsParms, "ElementsRead"), _
            "ptr" , DllStructGetPtr($tEnumSubnetsParms, "ElementsTotal") )
        If $aRet[0] Then Return SetError(2, $aRet[0]) ; ERR: Any runtime errors
        If DllStructGetData($tEnumSubnetsParms,"EnumInfoPtr")=0 Then Return SetError(1, 0, $aSubnets) ; ERR: Not Found
        $tIPArray = DllStructCreate("int NumElements;ptr Elements", DllStructGetData($tEnumSubnetsParms,"EnumInfoPtr"))
        ReDim $aSubnets[$aSubnets[0] + DllStructGetData($tIPArray,"NumElements") +1]
        For $i=$aSubnets[0]+1 To UBound($aSubnets)-1
            $tAddress = DllStructCreate("dword SubNetAddess", DllStructGetData($tIPArray,2) + ($i-$aSubnets[0]-1)*4)
            $aSubnets[$i]=DllStructGetData($tAddress, "SubNetAddess")
            If BitAND($iFlags,1)=0 Then $aSubnets[$i] = BitRotate(BitAND($aSubnets[$i],0xFF000000), 8,"D") _
                &"."& BitRotate(BitAND($aSubnets[$i],0x00FF0000),16,"D") _
                &"."& BitRotate(BitAND($aSubnets[$i],0x0000FF00),-8,"W") _
                &"."& BitAND($aSubnets[$i],0x000000FF)
        Next
        $aSubnets[0] += DllStructGetData($tIPArray,"NumElements")
    Until DllStructGetData($tEnumSubnetsParms,"ElementsRead") = DllStructGetData($tEnumSubnetsParms,"ElementsTotal")
    ; Freeing a memory
    DllCall("Dhcpsapi.dll", "none", "DhcpRpcFreeMemory", "ptr", DllStructGetData($tEnumSubnetsParms,"EnumInfoPtr"))
    Return $aSubnets
EndFunc ; => _DHCP_EnumSubnets

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2010-3-10 02:32:54 | 显示全部楼层
;================================================= ==============================
;
;描述:找IP地址,MAC或DHCP客户端属性名称
;参数(补):$ sDHCP - DHCP服务器的IP地址
$的ClientID - 客户端IP,MAC或名称(String或本机格式)
;整数 - 原始格式为IP地址/掩码
;二进制 - 原生格式的MAC地址(6字节)
$ iIDType - 类型的ClientID信息:
; 0 - 自动定义值
; 1 - 客户端IP地址
;2 - 客户端硬件(MAC)地址
; 3 - 客户名称
; 配置标志:
;0x1 -输出格式(0 -文本,1 -母语)
;要求:测试
;返回值:成功 - 与指标参数数组
; 0 - 客户端IP地址
; 1 - 客户端IP子网掩码
;2 - 客户端MAC地址
; 3 - 客户名称
; 4 - 客户评论
; 5 - 客户端租约到期时间
;失效 - 空,设置为错误
; 1 - 客户未找到DHCP服务器
2 - 无效的ClientID参数
; 3 - 操作系统不支持
;4 - 任何运行时错误,错误代码的API设置为扩展
;作者:amel27
;注:客户名称是区分大小写
;
;================================================= ==============================

评分

参与人数 1金钱 +30 贡献 +1 收起 理由
lsq726 + 30 + 1 感谢帮助

查看全部评分

 楼主| 发表于 2010-3-13 10:57:04 | 显示全部楼层
谢谢。。帮助。
可以设置其他的信息如 067 选项之类的么?
发表于 2010-3-13 11:18:14 | 显示全部楼层
呵呵,翻译的好啊
 楼主| 发表于 2010-4-2 04:36:00 | 显示全部楼层
不知道要怎么用。。
 楼主| 发表于 2010-4-23 10:55:54 | 显示全部楼层
期待有人能,,给几个使用他的实例~~~~~
发表于 2010-6-13 17:33:26 | 显示全部楼层
好复杂的东西呀。。。
发表于 2010-6-14 12:11:07 | 显示全部楼层
呵呵不错收藏一下。。
发表于 2010-6-14 19:51:48 | 显示全部楼层
不错,也来看一下
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-6-11 02:50 , Processed in 0.089055 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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