C# 轮询多个设备以确定 IP 类型:静态或 DHCP

发布于 2024-08-26 20:18:48 字数 1642 浏览 10 评论 0原文

想知道实际检索 IP 类型的 C# 代码:基于我将输入的设备列表的静态或 DHCP。

要查看的输出:

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());  

这给了我 90% 的我想要实现的目标 - 代码运行良好。下一部分是指定网络上的设备以远程获取信息。我注意到下面的一条评论指出,如果没有 WMI,这是不可能的。这肯定比我更接近。我坚信这是可以实现的。我愿意接受这里的建议。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流云如水 2024-09-02 20:18:48

编辑:抱歉,某些适配器上的某些属性可能为 NULL。已修复如下

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());
}

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());
}
故人如初 2024-09-02 20:18:48

如果您尝试对远程主机执行此操作,您将无法执行此操作。您需要访问 DHCP 服务器及其日志记录来识别此信息。

编辑:当然,如果可用,可以通过 WMI 进行操作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文