2

I am trying to get the list of all the Computers connected on My network and I was able to do so.

But then I need to get the Hostanme of the Ip address which i have stored in the String format with some other data like mac address.

I have tried using json But was not able to get the List of Ip from the String. I just List of ip from the String so that using Foreach I can find the hostname on that specific in.

Here is the code:

  static void Main(String[] args)
  {
        Process arp = new Process();
        arp.StartInfo.UseShellExecute = false;
        arp.StartInfo.RedirectStandardOutput = true;
        arp.StartInfo.FileName = "C://Windows//System32//cmd.exe";
        arp.StartInfo.Arguments = "/c arp -a";
        arp.StartInfo.RedirectStandardOutput = true;
        arp.Start();
        arp.WaitForExit();
        string output = arp.StandardOutput.ReadToEnd();
        Console.WriteLine(output);

        Console.WriteLine(data.Internet_Address);
        Console.ReadLine();            
    }
}

And here is the Output :

enter image description here

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291

2 Answers2

1

You can use regex to extract the IPs using Regex.Matches

var matches1 = Regex.Matches(output, @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b");    

As the first IP you may not need you can skip it.

for(int i=1; i < matches1.Count; i++)
     Console.WriteLine("IPs " + i + "\t" + matches1[i].Value);
Adil
  • 146,340
  • 25
  • 209
  • 204
0

Generally one would use regular expression to parse such text. Alternatively you can get CSV library to parse similar format, or if this is just one-off case basic String.Split would do.

var byLine = output.Split('\n') // split into lines
   .Skip(1); // skip header
var ips = byLine.Select(s => s.Split(' ')[0]);

Notes:

  • it is likely better to obtain information you are looking for by direct calls instead of calling command line tool
  • local addresses generally don't have "hostnames". Windows machine name not necessary visible as DNS entry.
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179