使用 DHCP 从 IP 获取 MAC 地址?
我正在尝试创建允许通过 WOL 唤醒 Windows 域中的 PC 的脚本/服务。现在我想让用户选择一个 AD 容器作为唤醒其中包含的 PC 的起点。我最初的想法是使用 DHCP 作为存储库来查询给定主机名的 MAC 地址(给定容器,我可以轻松地从 AD 中提取主机名)。
有没有办法以编程方式查询 DHCP 服务/服务器、传递主机名并恢复关联的 MAC 地址?
或者,有更好/更简单的方法来解决我的问题吗?
I am trying to create scripts/services that allow for waking PCs in a windows domain via WOL. Now i want to give the user the option to select an AD container as a starting point for the waking of PCs contained within. My initial thought is using DHCP as a repository to query for MAC addresses given the hostnames (which i can easily enough pull from AD given the container).
Is there a way to programmatically query the DHCP service/server, passing hostnames and recover the associated MAC addresses?
Or, is there a better/easier way to solve my problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试
dhcpexim。 exe
来自微软。或者,如果您更喜欢使用纯 C。 DhcpEnumSubnetClientsV4
Try
dhcpexim.exe
from microsoft.or, if you prefer using pure C. DhcpEnumSubnetClientsV4
没问题;因为所有计算机都在您的域中,所以您可以编写一个 VBScript,该脚本将从本地计算机获取 MAC 地址并将其作为计算机对象的属性存储在 Active Directory 中。
以下是有关如何执行此操作的快速技巧(将其另存为 .vbs 文件):
因为您的客户端并不全部位于上面的“成员服务器”OU 中,所以您需要修改上面的脚本以包含 目录搜索
strComputerName< /code> 获取
COMPUTERLOCATION
。当您有一个工作脚本时,请您的域管理员将该脚本作为针对您需要监视的计算机的启动脚本;这样它就会在计算机启动时执行。您还可以将脚本作为计划任务运行,以从任何尚未重新启动的客户端获取数据或使用 psexec 或您可以想到的其他方式来立即获取数据。或者,您可以完全重写脚本以远程连接到所有计算机并以这种方式获取数据(由于本地防火墙,这可能无法实现)。或者您可以编写一个小型 .NET 控制台应用程序来执行相同的操作,这取决于您...
此外,尽管为计算机对象定义了一个
networkAddress
属性;但默认情况下,计算机对象本身无权写入此属性。因为启动脚本在特定计算机上的SYSTEM
帐户上下文中运行,所以最简单的事情是使用计算机对象 (SELF
) 具有写入访问权限的属性。otherTelephone
属性是多值的,并且是 个人信息属性集,默认情况下所有计算机对象都具有写入权限。如果您想使用networkAddress
属性,您需要为所有计算机设置对该属性的显式写入访问权限。此外,您还需要记住,将 MAC 地址存储在 Active Directory 中意味着您域中的所有用户都将具有对其的读取访问权限,这反过来可能(取决于您的环境)造成较小的安全风险。
No problem; because all of the machines are in your domain you can put together a VBScript that will get the MACAddress(es) from the local machine and store it as an attribute of the computer object in Active Directory.
Here's a quick hack on how to do that (save this as a .vbs-file):
Because your clients aren't all in the "Member Servers" OU above you'll need to modify the above script to include a directory search for the
strComputerName
do get theCOMPUTERLOCATION
.When you have a working script, ask your domain administrator to put the script as a start-up script targetting the computers you need to monitor; that way it'll execute whenever a computer boots up. You can also run the script as a scheduled task to get your data from any clients that haven't rebooted or use psexec or some other way you can think of to get the data immediately. Or you can rewrite the script entirely to remote connect to all of your machines and get the data that way (which might not be possible due to local firewalls). Or you could write a small .NET console application which does the same thing, it's up to you...
Also, although there is a
networkAddress
-attribute defined for computer objects; by default the computer object itself does not have access to write to this property. Because start up-scripts run in the context of theSYSTEM
account on the particular machine the easiest thing is to use an attribute that the computer object (SELF
) has write access to. TheotherTelephone
-attribute is multivalued and part of the Personal-Information Property Set which all computer objects has write access to by default. If you want to use thenetworkAddress
-attribute you need to set explicit write access to that attribute for all of your computers.Also you need to bear in mind that storing the the MAC address in Active Directory means that all of the users in your domain will have read access to it which in turn might possibly (depending on your environment) pose a small security risk.
按照网络的方式来做。
获取 SharpPcap(C# 的 Pcap 包装器)和 WinPcap (Windows) 或 libpcap (*nix)。编写一个应用程序,创建 SNMP 数据包来查询路由器上的 ARP 表。
注意:ARP(地址解析协议)表是包含 IP 地址到 MAC 地址映射的表。
我最近一直在考虑实现一个执行此操作的示例,但我没有一个示例尚未展示。一旦完成,我将确保将其添加到项目源代码树中的 SharpPcap 示例中。
To do it the way the network does.
Grab SharpPcap (Pcap wrapper for C#) and WinPcap (Windows) or libpcap (*nix). Write an application that creates SNMP packets to query the ARP table on the router.
Note: The ARP (Address Resolution Protocol) table is the table containing the mapping of IP address to MAC address.
I've been thinking about implementing an example that does this lately but I don't have one to show yet. Once I do, I'll make sure it gets added to the SharpPcap examples found in the project's source tree.
使用 DHCP 无法做到这一点。 DHCP 从 MAC 分配 IP,而不是相反。
ARP 是将 IP 转换为 MAC 的,但是是机器本身应答 ARP 请求,所以如果它关闭,显然不会应答......
我建议你将 MAC 直接存储在 AD 中(我猜 AD 支持自定义属性?)
You can't do that with DHCP. DHCP attributes IP from MAC, not the other way around.
ARP is what converts IP into MAC but it's the machine itself that answers ARP requests so if it's off it's obviously not gonna answer ...
I suggest you store the MAC in your AD directly (I guess AD supports custom attributes ?)
你需要使用arp来获取mac地址,在C语言中这样做是一个漫长的过程。
Mac 地址是硬编码的,因此如果您有 X 台计算机,请获取 X 个 mac 地址并将它们绑定到 AD。
请注意,计算机必须处于开机状态才能请求其 MAC 地址。
根据IP地址查找MAC地址
you need to use arp to get a mac adress and doing so In C is a long process.
Mac adresses are hard coded, so if you have X computers go and get X mac addresses and tie them to the AD.
Note that the computer will have to be on to request its mac address.
Finding MAC address from IP address
是的,不用担心,如果 PC 有租约,您可以直接从 DHCP 获取此信息。
知道如何在 DHCP 中右键单击并添加保留吗?
在 DHCP 中查找“唯一 ID”。这是 MAC 地址,不带冒号。
Yeah dun worry about it, you can pull this info directly from DHCP if the PC has a lease.
Know how you right click and add a reservation in DHCP?
Look in DHCP for the 'unique ID'. It's the MAC address, sans the colons.
这有点古怪,似乎无法以编程方式查询 DHCP 服务器。感谢科察克提出这个问题。我知道 DHCP 协议没有这样的查询,但我认为 Microsoft 的可执行文件可能有某种方式可以从命令行寻址它。我没有听到任何人说不存在这样的情况,但一定是这样。
哇,等一下...我想我找到了我们要找的东西:NETSH。比照:
http://social.technet .microsoft.com/Forums/en/ITCG/thread/afb4be16-09bd-4260-b515-8323d85d4ccb
其中表示如果您在 DHCP 服务器上打开命令提示符,则可以运行此命令:
netsh dhcp server scope 192.168 .1.0 向客户端显示
并获取如下报告:
10.10.98.53 - 255.255.255.0 -00-0c-29-02-a4-09 - NEVER EXPIRES -D
10.10.98.54 - 255.255.255.0 - 00-22-19-10-29-75 -1/21/2012 8:39:25 AM -D
耶皮!感谢您的线索!如果不是这个,我可能会把搜索范围缩小到 technet,然后找到那个。
This is a little bit wacky it seems that there's no way to query the DHCP server programmatically. Thanks cottsak for asking the question. I understand that the DHCP protocol doesn't have such a query, but I thought mayb the executable from Microsoft might have some way you can address it from the command line. I haven't heard anybody anywhere say that there is no such case, but it must be so.
WHOA, wait a minute... I think I found what we're looking for: NETSH. cf:
http://social.technet.microsoft.com/Forums/en/ITCG/thread/afb4be16-09bd-4260-b515-8323d85d4ccb
Where it says if you open a command prompt on the DHCP server you can run this command:
netsh dhcp server scope 192.168.1.0 show clients
and get a report such as this:
10.10.98.53 - 255.255.255.0 -00-0c-29-02-a4-09 - NEVER EXPIRES -D
10.10.98.54 - 255.255.255.0 - 00-22-19-10-29-75 -1/21/2012 8:39:25 AM -D
Yippeee! Thanks for the thread!! If it wasn't for this one, I enver would have narrowed my search to technet adn found that one.