如何让局域网中的所有主机的IP

如何让局域网中的所有主机的IP

本文介绍了如何让局域网中的所有主机的IP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要列出所有连接的主机的IP地址在我的网络。什么是最简单的方法是什么?

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

推荐答案

你必须做一个ping扫描。有一平一类System.Net命名空间。举例如下。此外,这是唯一可能的,如果你的电脑没有防火墙的运行。如果他们已经有了启用了防火墙,有没有办法确定这个信息的短做SNMP查询您的开关。

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
}

另一个问题是要确定你的网络有多大。在大多数家庭的情况下,您的网络掩码为24位。这意味着其设置为255.255.255.0。如果你的网关是192.168.1.1,这意味着你的网络上的有效地址将是192.168.1.1到192.168.1.254。这里有一个 IP计算器的帮助。你必须遍历每个地址,然后ping使用Ping类的地址,并检查PingReply。

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.

如果你只是寻找信息,不关心你如何得到它,你可以使用NMap的。将作为命令如下

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

编辑:

至于速度去,因为你是在本地网络上,你可以减少超时间隔大大为您的机器不应该超过100毫秒的回复。您还可以使用SendAsync来ping他们都在并行。下面的程序将平254地址在半秒。

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();
        }
    }
}

编辑:一些使用它自己之后,我修改了程序的输出有多少IP地址回应计数。有一个常量布尔值,如果设置为true,将导致程序解决了IP地址的主机名。这显著减慢了扫描,但。 (在半秒到16秒),还发现,如果不正确地指定的IP地址(做了一个错字自己),得到的答复对象可以是空的,所以我处理了。

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.

这篇关于如何让局域网中的所有主机的IP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 17:10