views:

68

answers:

4

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

A: 

You could ping the adress range and note if a host responded. Of course, this will require the host to respond to the ping packets.

Femaref
Pinging e.g 255 hosts is kind of time-consumming-I think so. Is there a faster method?
Saint_pl
a second is not time consuming. Just hit off all 255 pings at the same time, not one after another waiting for timeouts.
TomTom
How to do this faster? Even if I ping 10 hosts it take several sec.
Saint_pl
+2  A: 

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.

Evan Mulawski
+4  A: 

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;

namespace ConsoleApplication1
{
    class Program
    {
        static CountdownEvent countdown;

        static void Main(string[] args)
        {
            countdown = new CountdownEvent(1);
            Stopwatch sw = new Stopwatch();
            sw.Start();
            string ipBase = "10.10.5.";
            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", sw.ElapsedMilliseconds);
            Console.ReadLine();
        }

        static void p_PingCompleted(object sender, PingCompletedEventArgs e)
        {
            string ip = (string)e.UserState;
            //Console.WriteLine("{0}:\t{1} ({2})", ip, e.Reply.Status, e.Reply.RoundtripTime);
            if (e.Reply.Status == IPStatus.Success)
            {
                Console.WriteLine("{0} is up: ({1} ms)", ip, e.Reply.RoundtripTime);
            }
            countdown.Signal();
        }
    }
}
Tim Coker
@Tim, do you know if Nmap has an API?
Brad
Just for notice, RFC1918 says that these ranges are all valid for LANs: 10.0.0.0 – 10.255.255.255, 172.16.0.0 – 172.31.255.255, 192.168.0.0 – 192.168.255.255
joni
@Brad, not sure about a full API, but its designed in the spirit of a regular UNIX program, so you can easily execute it via the command line and capture Standard Output. There are options to format the via XML as well as a GREPable format (IE, use with regex). For info on how to execute a command and capture the output in c#, see this answer. http://stackoverflow.com/questions/3258704/issues-about-files-in-use-get-the-name-of-another-process-that-use-file/3258779#3258779
Tim Coker
Richard
@TimCoker, yes, it works but if I'm pinging e.g. 255 hosts it takes several sec. Can I accelerate this?
Saint_pl
A: 

Instead of arranging a wild ping-party, you could possibly (I really don't know exactly!) use NetBIOS (request the netbios nametable somehow?) http://technet.microsoft.com/en-us/library/cc757216%28WS.10%29.aspx. Or why not ask your DHCP server?

joni
DHCP server will unlikely return ACTIVE. You only know recently active - still have to ping and you may miss some stuff statically configured.
TomTom
yeah, the only way to be sure would be to search your network switch and then follow all the cables..
joni