Search code examples
c#network-programmingipapr

Apr -a does not pull ALL macs


string macAddress = string.Empty;
            System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
            pProcess.StartInfo.FileName = "arp";
            pProcess.StartInfo.Arguments = "-a " + IP;
            pProcess.StartInfo.UseShellExecute = false;
            pProcess.StartInfo.RedirectStandardOutput = true;
            pProcess.StartInfo.CreateNoWindow = true;
            pProcess.Start();
            string strOutput = pProcess.StandardOutput.ReadToEnd();
            string[] substrings = strOutput.Split('-');
            if (substrings.Length >= 8)
            {
                macAddress = substrings[3].Substring(Math.Max(0, substrings[3].Length - 2))
                         + "-" + substrings[4] + "-" + substrings[5] + "-" + substrings[6]
                         + "-" + substrings[7] + "-"
                         + substrings[8].Substring(0, 2);
                return macAddress;
            }
            else
            {
                return "00:00:00:00:00:00";
            }

I have some devices where the mac address and ips are not being written to the console. I can ping and do a manual arp -a in a cmd prompt just fine and the results look exactly the same as the ones being displayed.

Is this an issue with how I am using arp?


Solution

  • Arp -a

    Shows the entries which are already present in the arp table. An entry will be entered into the arp table just at the time you connect to a machine in your private network.

    Hence you can only be sure about the entry for your gateway in the table. To get all the live machines' mac addresses in the network you have to try to connect to all the machines manually. You can add code to ping to all the machines before using the arp command in your program.