Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get IP of all hosts in LAN?

I need to list IP addresses of all connected hosts in my LAN. What is the simplest way to do this?

like image 486
Saint Avatar asked Oct 28 '10 12:10

Saint


People also ask

How do you find all IP address in LAN using CMD?

From the desktop, navigate through; Start > Run> type "cmd.exe". A command prompt window will appear. At the prompt, type "ipconfig /all". All IP information for all network adapters in use by Windows will be displayed.


2 Answers

You'll have to do a ping sweep. There's a Ping class in the System.Net namespace. Example follows. Also this is only possible if your computers don't have firewalls running. If they've got a firewall enabled, there's no way to determine this information short of doing SNMP queries on your switches.

System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping(); System.Net.NetworkInformation.PingReply rep = p.Send("192.168.1.1"); if (rep.Status == System.Net.NetworkInformation.IPStatus.Success) {     //host is active } 

The other issue is to determine how large your network is. In most home situations, your network mask will be 24 bits. This means that its set to 255.255.255.0. If your gateway is 192.168.1.1, this means that valid addresses on your network will be 192.168.1.1 to 192.168.1.254. Here's an IP Calculator to help. You'll have to loop through each address and ping the address using the Ping class and check the PingReply.

If you're just looking for the information and aren't concerned with how you get it, you can use NMap. The command would be as follows

nmap -sP 192.168.1.0/24 

EDIT:

As far as speed goes, since you're on a local network, you can cut down the timeout interval considerably as your machines shouldn't take more than 100 milliseconds to reply. You can also use SendAsync to ping them all in parallel. The following program will ping 254 address in under half a second.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.NetworkInformation; using System.Diagnostics; using System.Net; using System.Threading; using System.Net.Sockets;  namespace ConsoleApplication1 {     class Program     {         static CountdownEvent countdown;         static int upCount = 0;         static object lockObj = new object();         const bool resolveNames = true;          static void Main(string[] args)         {             countdown = new CountdownEvent(1);             Stopwatch sw = new Stopwatch();             sw.Start();             string ipBase = "10.22.4.";             for (int i = 1; i < 255; i++)             {                 string ip = ipBase + i.ToString();                  Ping p = new Ping();                 p.PingCompleted += new PingCompletedEventHandler(p_PingCompleted);                 countdown.AddCount();                 p.SendAsync(ip, 100, ip);             }             countdown.Signal();             countdown.Wait();             sw.Stop();             TimeSpan span = new TimeSpan(sw.ElapsedTicks);             Console.WriteLine("Took {0} milliseconds. {1} hosts active.", sw.ElapsedMilliseconds, upCount);             Console.ReadLine();         }          static void p_PingCompleted(object sender, PingCompletedEventArgs e)         {             string ip = (string)e.UserState;             if (e.Reply != null && e.Reply.Status == IPStatus.Success)             {                 if (resolveNames)                 {                     string name;                     try                     {                         IPHostEntry hostEntry = Dns.GetHostEntry(ip);                         name = hostEntry.HostName;                     }                     catch (SocketException ex)                     {                         name = "?";                     }                     Console.WriteLine("{0} ({1}) is up: ({2} ms)", ip, name, e.Reply.RoundtripTime);                 }                 else                 {                     Console.WriteLine("{0} is up: ({1} ms)", ip, e.Reply.RoundtripTime);                 }                 lock(lockObj)                 {                     upCount++;                 }             }             else if (e.Reply == null)             {                 Console.WriteLine("Pinging {0} failed. (Null Reply object?)", ip);             }             countdown.Signal();         }     } } 

EDIT: After some use of it myself, I modified the program to output a count of how many IPs responded. There's a const bool that if set to true, will cause the program resolve the host names of the IPs. This significantly slows down the scan, though. (under half a second to 16 seconds) Also found that if the IP address is incorrectly specified (made a typo myself), the reply object can be null, so I handled that.

like image 182
Tim Coker Avatar answered Oct 17 '22 02:10

Tim Coker


You would need to build an address range (e.g. 192.168.0.1 - 192.168.255.254) and ping each of those addresses. If a response is received, then that host is active.

Asynchronous Ping Tutorial:

http://www.geekpedia.com/tutorial234_Asynchronous-Ping-using-Csharp.html

However, some newer operating systems block ping requests (ICMP). This would need to be disabled in the firewall on each computer for you to receive a response.

like image 35
Evan Mulawski Avatar answered Oct 17 '22 01:10

Evan Mulawski