1

I have tried a lot of solutions on internet for getting IP's from my local network and all of them have been failed to get all IP's. I also try to get IP's using ARP commands "arp /a" and "arp -a" but this command is also unable to do the job finally after searching for a while I found a software called "Advance IP Scanner" and when I run this software it gets all the IP's from local network and the most strange thing for me is that after running the Advance IP Scanner when I run the ARP command "arp /a" or "arp -a" it gets all the IP's from my local network.

This is what I have tried.

 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;
        }
  • Does this answer your question? [How to get IP of all hosts in LAN?](https://stackoverflow.com/questions/4042789/how-to-get-ip-of-all-hosts-in-lan) – Mikael Apr 02 '21 at 13:41
  • 1
    An ARP message usually get sent when a machine starts and periodically like every 20 minutes after starting. It usually does not get passed through routers so you only see the results in the local subnet or after you ping an IP address. To fill the ARP table with more addresses you can ping every IP in a range and then all machine in the range will respond. You can ignore the IP addresses with no response. Then read the ARP table after you complete the PING. – jdweng Apr 02 '21 at 14:19

1 Answers1

1

The Problem with arp is it only lists IP-Addresses your system is aware of (it already communicated with).

If you really want to get all IPs you should loop a ping for your subnet. (see the link posted by Mikael for example)

Edit 1)

If the ICM Protocol is blocked by the machine (is enabled by default) and you know a port that is open you can use a tool like PPing to ping a specific tcp/udp port. Maybe theres some other tool I don't know about with an api or exitcode support, if not you can use this wrapper I wrote for pping:

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,
}
Leppin
  • 241
  • 1
  • 7
  • I had also tried with ping but the issue with ping is that if there is any computer on network whose ping is disable then it will not get that pc's ip – user15539024 Apr 02 '21 at 14:55
  • Ping is not usually disabled. You can try ping from machine you think is disabled to same machine to prove it is disabled. You can also ping by machine name. It sound like you may not have routes to machines with no response. Are all machines on same network? – jdweng Apr 02 '21 at 15:16
  • @user15539024 see Edit 1 in my answer but as jdweng said ping is enabled by default and if somebody disabled it, it might have it's reason that this machine should not be found by a networkscanners. – Leppin Apr 02 '21 at 16:20
  • @jdweng yes all systems are on same network and using same router – user15539024 Apr 05 '21 at 11:56
  • @Leppin I have tried your solution but the issue with this is that when i try to ping an IP with a port 110 that is not exists on network it replies "Open" that is invalid response. – user15539024 Apr 05 '21 at 12:02
  • To test for a route you using PING (not PPING) initially. Ping doesn't get blocked because it doesn't use a port number. If PPING fails you don't know if it is a route issue or a port being blocked. – jdweng Apr 05 '21 at 12:14
  • @jdweng Ping uses the ICMP (https://de.wikipedia.org/wiki/Internet_Control_Message_Protocol) that is open by default in the windows firewall but also can get blocked, if that's the case you need to send a request to an open port on the destination system to see if there's a system connected (for example with pping). To user15539024 maybe you hit an internet pop3 Server (tcp 110) with your request that answered, just tried it myself and it seems to work. – Leppin Apr 05 '21 at 14:24
  • @Leppin : Ping doesn't usually get blocked. – jdweng Apr 05 '21 at 15:02