开发者

C# Poll multiple devices to determine IP type: Static or DHCP

开发者 https://www.devze.com 2022-12-24 07:45 出处:网络
Would like开发者_如何学运维 to know the c# code to actually retrieve the IP type: Static or DHCP based on a list of devices I will enter.

Would like开发者_如何学运维 to know the c# code to actually retrieve the IP type: Static or DHCP based on a list of devices I will enter.

Output to be viewed:

Device name:  IP Address:   MAC Address:         Type:  
Marvell Yukon 88E8001/8003/8010 PCI Gigabit Ethernet Controller NULL    00:00:F3:44:C6:00   DHCP
Generic Marvell Yukon 88E8056 based Ethernet Controller 192.168.1.102   00:00:F3:44:D0:00   DHCP
            ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection objMOC = objMC.GetInstances();

        txtLaunch.Text = ("Name\tIP Address\tMAC Address\tType" +"\r\n");

        foreach (ManagementObject objMO in objMOC)
        {
            StringBuilder builder = new StringBuilder();

            object o = objMO.GetPropertyValue("IPAddress");
            object m = objMO.GetPropertyValue("MACAddress");

            if (o != null || m != null)
            {
                builder.Append(objMO["Description"].ToString());
                builder.Append("\t");
                    if (o != null)
                       builder.Append(((string[])(objMO["IPAddress"]))[0].ToString());
                    else
                       builder.Append("NULL");
                builder.Append("\t");
                builder.Append(m.ToString());
                builder.Append("\t");
                builder.Append(Convert.ToBoolean(objMO["DHCPEnabled"]) ? "DHCP" : "Static");
                builder.Append("\r\n");
            }

            txtLaunch.Text = txtLaunch.Text + (builder.ToString());  

This gave me 90% of what I am looking to achieve - the code worked out well. The next portion is to specify a device on the network to obtain the information remotely. I noticed the one comment below that stated this was impossible without WMI. This is certainly much closer than I was. I'm convinced this can be accomplished. I'm open to recommendations here.


EDIT: sorry, some properties can be NULL on some adapters. Fixed below

ManagementClass objMC = new ManagementClass(
                        "Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();

Console.WriteLine("Name\tIP Address\tMAC Address\tType");

foreach (ManagementObject objMO in objMOC)
{

  StringBuilder builder = new StringBuilder();

  builder.Append(objMO["Description"].ToString());
  builder.Append("\t");
  object o = objMO.GetPropertyValue("IPAddress");

  if (o != null)
      builder.Append(((string[])(objMO["IPAddress"]))[0].ToString());
  else
      builder.Append("NULL");

  builder.Append("\t");

  object m = objMO.GetPropertyValue("MACAddress");

  if (m != null)
      builder.Append(m.ToString());
  else
      builder.Append("NULL");

  builder.Append("\t");
  builder.Append(Convert.ToBoolean(objMO["DHCPEnabled"]) ? "DHCP" : "Static");
  Console.WriteLine(builder.ToString());
}


Look at http://www.codeguru.com/csharp/csharp/cs_network/internetweb/article.php/c6023/ hope this helps


If you're trying to do this for remote hosts, you won't be able to do this. You'll need access to the DHCP server and its logging to identify this information.

Edit: of course, through WMI works if that's available.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号