Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
670 views
in Technique[技术] by (71.8m points)

windows - Querying a DHCP server in C#

I need to get the mapping of MAC to IP stored on DHCP server, either through a program running on the server itself or preferably through a program running on one of the DHCP clients.

I understand netsh utility can be used to get the dump however i have not had much success with that.

Any working examples or hint on that?

I have admin rights on DHCP server

Edit

I dont want to use arp cache as that would require me to either broadcast ping (which is not allowed on windows) or ping the all possible ip address of subnet( which takes lot of time).

I am sure that DHCP server stores the mapping of MAC to IP, how can i use that information, to map MAC to IP address?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the DHCP Objects component from the Windows 2000 Resource Kit for this. Even though the component is hard to find, is made for Windows 2000, goes out of life support in July 2010 according to Microsoft and has very little documentation, it does work.

  1. Download the Resource Kit Tool named DHCP Objects from for example here if you can't find it at Microsoft. This will give you an .exe file that in turn will install the DHCP Objects component.
  2. Register the DHCPOBJS.DLL file with regsvr32 or create a COM+ Application for it. Which is applicable depends on how the COM component is going to be used on your system.
  3. Use the Type Library Importer tlbimp.exe to create a managed wrapper around DHCPOBJS.DLL now that it's registered by the system.
  4. In Visual Studio, add a reference to the managed wrapper. Its default generated name is DhcpObjects.dll.

Now you can write code like this against the component:

using DhcpObjects;
class Program {
    static void Main(string[] args) {
        var manager = new Manager();
        var server = dhcpmgr.Servers.Connect("1.2.3.4");
        // query server here
    }
}

The installer also provides a Windows Help File which contains further documentation on how to query and manipulate a DHCP server. The section "The Object Model" is quite helpful.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...