I have a really large network. Class A network and due to security reasons broadcast and any other PING has been blocked. I want to get all the PC IP that are alive.

Is there any way to query DHCP server using C# to get all leased IP ?

link|improve this question
From the answers below, you have ARP, SNMP or the API. I guess you need to pick one now – Ben Quick Oct 14 '10 at 8:07
SNMP is also blocked.. I will see ARP API. Thnx – Algo Oct 20 '10 at 5:41
feedback

5 Answers

up vote 2 down vote accepted

There is only native win32 api to the dhcp server. So you will have to pinvoke it. Here is the DHCP Server Management API. Here you can find samples of using part of this api.

link|improve this answer
Thnx dear for giving me complete solution of my problem. – Algo Oct 20 '10 at 5:52
This helped me in Querying Microsoft DHCP server. Do you have any idea about how to query any Linux flavor DHCP server? – Algo Oct 21 '10 at 4:48
feedback

Connect to the server using SSH and ask for and parse /var/lib/dhcpd/dhcpd.leases.

link|improve this answer
Thanks very much but Is this works with Microsoft DHCP Server. As you know if in the same network two DHCP servers are running 1) Linux Primary and 2) Microsoft DHCP secondary. All computers running Microsoft windows will get IP from microsoft DHCP server, the secondary one. – Algo Oct 13 '10 at 11:55
Actually that's false, but your network might be set up that way. As for how to query the Windows server, there's probably somthing in WMI for that. – Ignacio Vazquez-Abrams Oct 13 '10 at 12:01
Thanks Ignacio Vazquez-Abrams, but one thing let me clear it. I want to query DHCP server by sitting to any other computer that can PING (see) DHCP server. – Algo Oct 13 '10 at 12:12
feedback

Microsoft's DHCP servers can be monitored using SNMP. Consult the MS DHCP MIB to see if it includes the information you seek.

link|improve this answer
feedback

You must be able to send out ARP requests for sure. What you can do is ARP all the IPs in your network one by one and see who answers.

link|improve this answer
I don't want to PING or ARP any one. I have found a way to query DHCP server and it returns me all the IP that are leased by it. But It is not working on LINUX. – Algo Oct 20 '10 at 5:40
feedback
//http://www.apachejava.blogspot.com


using System;

using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.ComponentModel; using System.Xml; using System.Net;

namespace DHCPTry {

public struct CUSTOM_CLIENT_INFO
{
    public string ClientName;
    public string IpAddress;
    public string MacAddress;
}

[StructLayout(LayoutKind.Sequential)]
public struct DHCP_CLIENT_INFO_ARRAY
{
    public uint NumElements;
    public IntPtr Clients;
}

[StructLayout(LayoutKind.Sequential)]
public struct DHCP_CLIENT_UID
{
    public uint DataLength;
    public IntPtr Data;
}

[StructLayout(LayoutKind.Sequential)]
public struct DATE_TIME
{
    public uint dwLowDateTime;
    public uint dwHighDateTime;

    public DateTime Convert()
    {
        if (dwHighDateTime == 0 && dwLowDateTime == 0)
        {
            return DateTime.MinValue;
        }
        if (dwHighDateTime == int.MaxValue && dwLowDateTime == UInt32.MaxValue)
        {
            return DateTime.MaxValue;
        }
        return DateTime.FromFileTime((((long)dwHighDateTime) << 32) | (UInt32)dwLowDateTime);
    }
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct DHCP_HOST_INFO
{
    public uint IpAddress;
    public string NetBiosName;
    public string HostName;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct DHCP_CLIENT_INFO
{
    public uint ClientIpAddress;
    public uint SubnetMask;
    public DHCP_CLIENT_UID ClientHardwareAddress; //no pointer -> structure !!
    [MarshalAs(UnmanagedType.LPWStr)]
    public string ClientName;
    [MarshalAs(UnmanagedType.LPWStr)]
    public string ClientComment;
    public DATE_TIME ClientLeaseExpires; //no pointer -> structure !!
    public DHCP_HOST_INFO OwnerHost; //no pointer -> structure
}

class Program
{
    [DllImport("dhcpsapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
    public static extern uint DhcpEnumSubnetClients(
        string ServerIpAddress,
        uint SubnetAddress,
        ref uint ResumeHandle,
        uint PreferredMaximum,
        out IntPtr ClientInfo,
        ref uint ElementsRead,
        ref uint ElementsTotal
    );

    static void Main()
    {
        try
        {
            enum_clients("192.168.3.1", "192.168.3.0");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        Console.ReadLine();
    }

    static void enum_clients(string Server, string Subnet)
    {
        string ServerIpAddress = Server;
        uint Response = 0;
        uint SubnetMask = StringIPAddressToUInt32(Subnet);
        IntPtr info_array_ptr;
        uint ResumeHandle = 0;
        uint nr_clients_read = 0;
        uint nr_clients_total = 0;

        Response = DhcpEnumSubnetClients(ServerIpAddress, SubnetMask, ref ResumeHandle,
           65536, out info_array_ptr, ref nr_clients_read, ref nr_clients_total);
        DHCP_CLIENT_INFO_ARRAY clients = (DHCP_CLIENT_INFO_ARRAY)Marshal.PtrToStructure(info_array_ptr, typeof(DHCP_CLIENT_INFO_ARRAY));
        Console.WriteLine("NumElements{0}",clients.NumElements.ToString());
        int size = (int)clients.NumElements;
        IntPtr[] ptr_array = new IntPtr[size];
        IntPtr current = clients.Clients;
        for (int i = 0; i < size; i++)
        {
            Console.WriteLine("{0}", i);
            ptr_array[i] = Marshal.ReadIntPtr(current);
            current = (IntPtr)((int)current + (int)Marshal.SizeOf(typeof(IntPtr)));
        }
        CUSTOM_CLIENT_INFO[] clients_array = new CUSTOM_CLIENT_INFO[size];

        for (int i = 0; i < size; i++)
        {
            DHCP_CLIENT_INFO curr_element = (DHCP_CLIENT_INFO)Marshal.PtrToStructure(ptr_array[i], typeof(DHCP_CLIENT_INFO));
            clients_array[i].IpAddress = UInt32IPAddressToString(curr_element.ClientIpAddress);
            clients_array[i].ClientName = curr_element.ClientName;
            Console.WriteLine("Client name {0}", clients_array[i].ClientName);
            clients_array[i].MacAddress = String.Format("{0:x2}-{1:x2}-{2:x2}-{3:x2}-{4:x2}-{5:x2}",
                Marshal.ReadByte(curr_element.ClientHardwareAddress.Data),
                Marshal.ReadByte(curr_element.ClientHardwareAddress.Data, 1),
                Marshal.ReadByte(curr_element.ClientHardwareAddress.Data, 2),
                Marshal.ReadByte(curr_element.ClientHardwareAddress.Data, 3),
                Marshal.ReadByte(curr_element.ClientHardwareAddress.Data, 4),
                Marshal.ReadByte(curr_element.ClientHardwareAddress.Data, 5));
            Console.WriteLine("Client MAC {0}", UInt32IPAddressToString(curr_element.ClientIpAddress));
            //This section will throw an AccessViolationException
             //Marshal.DestroyStructure(current, typeof(DHCP_CLIENT_INFO));
            // current = (IntPtr)((int)current + (int)Marshal.SizeOf(curr_element));
            //Replace with:
            //Marshal.DestroyStructure(ptr_array[i], typeof(DHCP_CLIENT_INFO));
        }
      }

    public static uint StringIPAddressToUInt32(string ip_string)
    {
        IPAddress IpA = System.Net.IPAddress.Parse(ip_string);
        byte[] ip_bytes = IpA.GetAddressBytes();
        uint ip_uint = (uint)ip_bytes[0] << 24;
        ip_uint += (uint)ip_bytes[1] << 16;
        ip_uint += (uint)ip_bytes[2] << 8;
        ip_uint += (uint)ip_bytes[3];
        return ip_uint;
    }

    public static string UInt32IPAddressToString(uint ipAddress)
    {
        IPAddress ipA = new IPAddress(ipAddress);
        string[] sIp = ipA.ToString().Split('.');

        return sIp[3] + "." + sIp[2] + "." + sIp[1] + "." + sIp[0];
    }



}

}

link|improve this answer
You have to make your computer part of Domain on which doimain DHCP server is running then this code will work if you are not part of DHCP domain then this will not work. – Algo Oct 20 '10 at 5:45
Also It does not work on Linux Server. Is there any way to make it work with Linux DHCP server? – Algo Oct 20 '10 at 5:46
for complete code folollow link in this post... serverfault.com/questions/190461/querying-a-dhcp-server-in-c/… – Algo Oct 20 '10 at 5:53
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.