ping 大量 IP 地址的最快方法是什么?

发布于 2024-12-13 16:09:31 字数 1162 浏览 1 评论 0原文

我的数据表中有大量 IP 地址列表,我必须快速对它们执行 ping 操作, 我使用了这段代码:

public bool PingIP(string IP)
{
    bool result = false;
    try
    {                
        Ping ping = new Ping();
        PingReply pingReply = ping.Send(IP);
        if (pingReply.Status == IPStatus.Success)
            result = true;
    }
    catch
    {
        result = false;
    }
    return result;
}

然后我在 while 循环中调用它:

private void CheckNetworkState()
{
    while (rowindexping > -1)
    {
        if (rowindexping == tbl_ClientInfo.Rows.Count)
        {
            rowindexping = -1;
            return;
        }

        string ip = tbl_ClientInfo.Rows[rowindexping]["clientip"].ToString();
        if (!PingIP(ip))
        {
           do something
        }
        rowindexping++;
        Thread.Sleep(100);
    }
}

因为我想在项目的后台完成这项工作,所以我在线程中调用该类:

threadping = new Thread(CheckNetworkState);            
threadping.IsBackground = true;
threadping.Start(); 

我的问题是它需要很多时间并且不能在后台工作。我的意思是系统正忙,直到 tbl_clientinfo 中的所有 IP 地址都通过 ping 类。 我希望该系统检查所有行,因为我正在处理项目的其他部分。

我做对了吗?

I have a large list of IP addresses in a datatable and i have to ping them so quickly,
I used this code :

public bool PingIP(string IP)
{
    bool result = false;
    try
    {                
        Ping ping = new Ping();
        PingReply pingReply = ping.Send(IP);
        if (pingReply.Status == IPStatus.Success)
            result = true;
    }
    catch
    {
        result = false;
    }
    return result;
}

then i call it in while loop :

private void CheckNetworkState()
{
    while (rowindexping > -1)
    {
        if (rowindexping == tbl_ClientInfo.Rows.Count)
        {
            rowindexping = -1;
            return;
        }

        string ip = tbl_ClientInfo.Rows[rowindexping]["clientip"].ToString();
        if (!PingIP(ip))
        {
           do something
        }
        rowindexping++;
        Thread.Sleep(100);
    }
}

Since i want to do this work at the background of my project i call the class in a thread:

threadping = new Thread(CheckNetworkState);            
threadping.IsBackground = true;
threadping.Start(); 

my problem is that it takes so many time and does not work at the background. i mean the system is busy till all ip addresses in tbl_clientinfo go through the ping class.
i want that system check all rows since i'm working with other part of my project.

Did i do correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

嘦怹 2024-12-20 16:09:31

您的代码在单个线程上运行所有代码;你没有使用多线程。另外,为什么里面有一个 Thread.Sleep ?

请尝试以下操作:

  1. 查询数据库并获取所有 IP
  2. 在循环中,启动一个新线程,该线程将为每个 IP 地址运行PingIP

注意:您还可以在每次从数据库获取新行时启动一个新线程

示例:

    static void Main(string[] args)
    {
        // get the IPs from the database so you can iterate over them
        List<string> ips = new List<string>()
        {
            "google.com",
            "127.0.0.1",
            "stackoverflow.com"
        };

        foreach (var ip in ips)
        {
            // See http://stackoverflow.com/questions/4744630/unexpected-behaviour-for-threadpool-queueuserworkitem
            // for reason to use another variable in the loop
            string loopIp = ip;
            WaitCallback func = delegate(object state)
            {
                if (PingIP(loopIp))
                {
                    Console.WriteLine("Ping Success");
                }
                else
                {
                    Console.WriteLine("Ping Failed");
                }
            };
            ThreadPool.QueueUserWorkItem(func);
        }

        Console.ReadLine();
    }

    public static bool PingIP(string IP)
    {
        bool result = false;
        try
        {
            Ping ping = new Ping();
            PingReply pingReply = ping.Send(IP);

            if (pingReply.Status == IPStatus.Success)
                result = true;
        }
        catch
        {
            result = false;
        }

        return result;
    }

Your code is running all the code on a single thread; you're not using multiple threads. Also, why do you have a Thread.Sleep in there?

Try the following:

  1. Query the database and get all the IPs
  2. In a loop, spin up a new thread that will run PingIP for each IP address

Note: You can also spin up a new thread every time you get a new row from the database

Sample:

    static void Main(string[] args)
    {
        // get the IPs from the database so you can iterate over them
        List<string> ips = new List<string>()
        {
            "google.com",
            "127.0.0.1",
            "stackoverflow.com"
        };

        foreach (var ip in ips)
        {
            // See http://stackoverflow.com/questions/4744630/unexpected-behaviour-for-threadpool-queueuserworkitem
            // for reason to use another variable in the loop
            string loopIp = ip;
            WaitCallback func = delegate(object state)
            {
                if (PingIP(loopIp))
                {
                    Console.WriteLine("Ping Success");
                }
                else
                {
                    Console.WriteLine("Ping Failed");
                }
            };
            ThreadPool.QueueUserWorkItem(func);
        }

        Console.ReadLine();
    }

    public static bool PingIP(string IP)
    {
        bool result = false;
        try
        {
            Ping ping = new Ping();
            PingReply pingReply = ping.Send(IP);

            if (pingReply.Status == IPStatus.Success)
                result = true;
        }
        catch
        {
            result = false;
        }

        return result;
    }
哽咽笑 2024-12-20 16:09:31

我要做的是在后台运行一个单独的多线程守护进程,执行 ping 操作,并将结果放入数据库中。有一个稍后通过 AJAX 加载结果的页面。

What I would do is have a separate multi-threaded daemon running in the background, doing the pings, and putting the results in a database. Have a page that loads results via AJAX later.

如何视而不见 2024-12-20 16:09:31

考虑对 fping 实用程序进行系统调用。当然,它不是托管代码,但 fping 针对您的特定任务进行了很好的优化,并将您的问题简化为单个调用,然后处理基于文本的结果列表。

Consider making a system call to the fping utility. Granted, it's not managed code, but fping is very well optimized for your specific task and would simplify your problem down to a single call followed by the processing of a text-based list of results.

流星番茄 2024-12-20 16:09:31

您可以使用 powershell

private string resultat(string s)
{
    Runspace space = RunspaceFactory.CreateRunspace();
    space.Open();

    Pipeline pipeline = space.CreatePipeline();
    pipeline.Commands.AddScript(s);
    pipeline.Commands.Add("Out-String");
    Collection<PSObject> results = pipeline.Invoke();

    StringBuilder stringBuilder = new StringBuilder();
    foreach (PSObject obj in results)
    {
        stringBuilder.AppendLine(obj.ToString());
    }

    return stringBuilder.ToString();
}

,然后使用 resultat("ping -l 10 -n 2 " + your_ip);

you can use powershell

private string resultat(string s)
{
    Runspace space = RunspaceFactory.CreateRunspace();
    space.Open();

    Pipeline pipeline = space.CreatePipeline();
    pipeline.Commands.AddScript(s);
    pipeline.Commands.Add("Out-String");
    Collection<PSObject> results = pipeline.Invoke();

    StringBuilder stringBuilder = new StringBuilder();
    foreach (PSObject obj in results)
    {
        stringBuilder.AppendLine(obj.ToString());
    }

    return stringBuilder.ToString();
}

and then use resultat("ping -l 10 -n 2 " + your_ip);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文