【问题标题】:Problem while getting all IP's from local network C#从本地网络C#获取所有IP时出现问题
【发布时间】:2021-06-29 08:54:22
【问题描述】:

我在互联网上尝试了很多从本地网络获取 IP 的解决方案,但都未能获得所有 IP。 我也尝试使用 ARP 命令“arp /a”和“arp -a”来获取 IP,但是在搜索了一段时间后,这个命令也无法完成这项工作,我找到了一个名为“Advance IP Scanner”的软件,当我运行这个软件它从本地网络获取所有IP,对我来说最奇怪的是,在运行高级IP扫描仪后,当我运行ARP命令“arp /a”或“arp -a”时,它会从我的本地获取所有IP网络。

这是我尝试过的。

 public List<IPScanEntity> GetArpResult()
        {
            List<IPScanEntity> List = new List<IPScanEntity>();
            
            string baseIp = ConfigurationManager.AppSettings["baseIp"];

            //getting my own system IP
            List.Add(HostIPScanResult());
            for (int subnet = 1; subnet < 255; subnet++)
            {
                bool a = List.Any(x => x.Ip.Equals(baseIp+subnet.ToString()));

                if (a == true)
                {
                    continue;
                }

                try
                {
                    var p = Process.Start(new ProcessStartInfo("arp", "/a " + baseIp + subnet)
                    {
                        CreateNoWindow = true,
                        UseShellExecute = false,
                        RedirectStandardOutput = true
                    });

                    var output = p?.StandardOutput.ReadToEnd();
                    p?.Close();
                    var lines = output.Split('\n').Where(l => !string.IsNullOrWhiteSpace(l));
                    if (!lines.Contains("No ARP Entries Found.\r"))
                    {
                        var result =
                        (from line in lines
                         select Regex.Split(line, @"\s+")
                            .Where(i => !string.IsNullOrWhiteSpace(i)).ToList()
                            into items
                         where items.Count == 3                        
                         select new IPScanEntity
                         {
                             Ip = items[0],
                             MacAddress = items[1],
                         });
                        List.Add(result.FirstOrDefault());
                    }
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);                    
                }                
            }
         
            return List;
        }

【问题讨论】:

  • 这能回答你的问题吗? How to get IP of all hosts in LAN?
  • ARP 消息通常在机器启动时发送,并且在启动后每隔 20 分钟定期发送一次。它通常不会通过路由器,因此您只能在本地子网中或 ping IP 地址后看到结果。要使用更多地址填充 ARP 表,您可以 ping 范围内的每个 IP,然后该范围内的所有计算机都会响应。您可以忽略不响应的 IP 地址。然后在完成 PING 后读取 ARP 表。

标签: c# .net ip arp


【解决方案1】:

arp 的问题在于它只列出了您的系统知道的 IP 地址(它已经与之通信)。

如果您真的想获取所有 IP,您应该为您的子网循环 ping。 (例如见the link posted by Mikael

编辑 1)

如果 ICM 协议被机器阻止(默认启用)并且您知道一个打开的端口,您可以使用类似 PPing 的工具来 ping 特定的 tcp/udp 端口​​。也许还有一些我不知道的其他工具支持 api 或 exitcode 支持,如果没有,您可以使用我为 ping 编写的这个包装器:

public static bool PPing(string host, int port, PPingProtocolType type = PPingProtocolType.tcp)
{
  var cmdOutput = new StringBuilder();
  var cmdStartInfo = new ProcessStartInfo()
  {
    //Path to PPing.exe
    FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tools", "pping.exe"),
    Arguments = $"-r 1 -w 0{(type == PPingProtocolType.udp ? " -u " : " ")}{host} {port}",
    UseShellExecute = false,
    RedirectStandardOutput = true,
    CreateNoWindow = true,
  };

  using (Process proc = Process.Start(cmdStartInfo))
  {
    while (!proc.StandardOutput.EndOfStream)
    {
      cmdOutput.AppendLine(proc.StandardOutput.ReadLine());
    }
  }

  return cmdOutput.ToString().Contains("(1 OPEN, 0 CLOSED)");
}

public enum PPingProtocolType
{
  tcp = 0,
  udp = 1,
}

【讨论】:

  • 我也尝试过 ping,但 ping 的问题是,如果网络上有任何计算机的 ping 被禁用,那么它将无法获取该计算机的 ip
  • Ping 通常不会被禁用。您可以尝试从您认为已禁用的机器 ping 到同一台机器以证明它已禁用。您也可以按机器名称 ping。听起来您可能没有路由到没有响应的机器。所有机器都在同一个网络上吗?
  • @user15539024 请参阅我的回答中的编辑 1,但正如 jdweng 所说,默认情况下启用了 ping,如果有人禁用它,可能是网络扫描仪找不到这台机器的原因。
  • @jdweng 是的,所有系统都在同一个网络上并使用同一个路由器
  • @Leppin 我已经尝试过您的解决方案,但问题是当我尝试使用网络上不存在的端口 110 ping IP 时,它会回复“打开”,这是无效的响应。
猜你喜欢
  • 2015-02-23
  • 1970-01-01
  • 2016-11-02
  • 2014-09-22
  • 1970-01-01
  • 1970-01-01
  • 2013-03-31
  • 2012-09-29
  • 2018-09-01
相关资源
最近更新 更多