如何扫描 IP 范围 C#

发布于 2024-08-07 23:42:10 字数 4476 浏览 5 评论 0原文

如何扫描特定范围的 IP 并将其增加到用户定义的范围..就像大多数端口扫描仪一样。但如何增加主机位..它增加了网络位..

private static void sendAsyncPingPacket(string hostToPing)
    {
        try
        {
            int timeout = 5000;
            AutoResetEvent waiter = new AutoResetEvent(false);
            Ping pingPacket = new Ping();
            //ping completion event reaised
            pingPacket.PingCompleted += new PingCompletedEventHandler(PingCompletedCallback);
            string data = "Ping test check";
            byte[] byteBuffer = Encoding.ASCII.GetBytes(data);
            PingOptions pingOptions = new PingOptions(64, true);
            Console.WriteLine("Time to live: {0}", pingOptions.Ttl);
            //Console.WriteLine("Don't fragment: {0}", pingOptions.DontFragment);
            pingPacket.SendAsync(hostToPing, timeout, byteBuffer, pingOptions, waiter);


            //do something useful
            waiter.WaitOne();
            Console.WriteLine("Ping RoundTrip returned, Do something useful here...");
        }
        catch (PingException pe)
        {
            Console.WriteLine("INVALID IP ADDRESS FOUND");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exceptin " + ex.Message);
        }

    }
    private static void PingCompletedCallback(object sender, PingCompletedEventArgs e)
    {
        try
        {
            if (e.Cancelled)
            {
                Console.WriteLine("Ping canceled.");

                // Let the main thread resume. 
                // UserToken is the AutoResetEvent object that the main thread 
                // is waiting for.
                ((AutoResetEvent)e.UserState).Set();
            }

            // If an error occurred, display the exception to the user.
            if (e.Error != null)
            {
                Console.WriteLine("Ping failed>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ");
                //this will print exception
                //Console.WriteLine (e.Error.ToString ());

                // Let the main thread resume. 
                ((AutoResetEvent)e.UserState).Set();
            }

            PingReply reply = e.Reply;

            DisplayReply(reply);

            // Let the main thread resume.
            ((AutoResetEvent)e.UserState).Set();
        }
        catch (PingException pe)
        {
            Console.WriteLine("INVALID IP ADDRESS");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception " + ex.Message);
        }
    }

    public static void DisplayReply (PingReply reply)
    {
        if (reply == null)
            return;

        Console.WriteLine ("ping status: {0}", reply.Status);
        if (reply.Status == IPStatus.Success)
        {
            Console.WriteLine ("Address: {0}", reply.Address.ToString ());
            Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime);
            Console.WriteLine ("Time to live: {0}", reply.Options.Ttl);
            //Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment);
            Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length);
        }
    }

  private static long ToInt(string addr) 
    {

        return (long)(uint)System.Net.IPAddress.NetworkToHostOrder(
            (int)System.Net.IPAddress.Parse(addr).Address);    
    }

   private static string ToAddr(long address)
    {
        return System.Net.IPAddress.Parse(address.ToString()).ToString();
    }

static int temp = 0;
    private static void scanLiveHosts(string ipFrom, string ipTo)
    {
        long from =  Program.ToInt(ipFrom);
        long to =  Program.ToInt(ipTo);

        long ipLong = Program.ToInt(ipFrom);
        while ( from < to)
        {

            string address = Program.ToAddr(ipLong);
            Program.sendAsyncPingPacket(address);
            ipLong++;
        }

    }
        static void Main(string[] args)
    {
        try
        {
            Program.getDeviceList();
            Program.sendAsyncPingPacket("192.168.3.72");
            Program.scanLiveHosts("192.168.3.1", "192.168.3.41");



        }
        catch (InvalidOperationException ioe)
        {
            Console.WriteLine(ioe.Message);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

How to scan a specific range of IP and also increment it to user defined range.. in like most of the port scanners. but how to increase host bits.. it increases network bits..

private static void sendAsyncPingPacket(string hostToPing)
    {
        try
        {
            int timeout = 5000;
            AutoResetEvent waiter = new AutoResetEvent(false);
            Ping pingPacket = new Ping();
            //ping completion event reaised
            pingPacket.PingCompleted += new PingCompletedEventHandler(PingCompletedCallback);
            string data = "Ping test check";
            byte[] byteBuffer = Encoding.ASCII.GetBytes(data);
            PingOptions pingOptions = new PingOptions(64, true);
            Console.WriteLine("Time to live: {0}", pingOptions.Ttl);
            //Console.WriteLine("Don't fragment: {0}", pingOptions.DontFragment);
            pingPacket.SendAsync(hostToPing, timeout, byteBuffer, pingOptions, waiter);


            //do something useful
            waiter.WaitOne();
            Console.WriteLine("Ping RoundTrip returned, Do something useful here...");
        }
        catch (PingException pe)
        {
            Console.WriteLine("INVALID IP ADDRESS FOUND");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exceptin " + ex.Message);
        }

    }
    private static void PingCompletedCallback(object sender, PingCompletedEventArgs e)
    {
        try
        {
            if (e.Cancelled)
            {
                Console.WriteLine("Ping canceled.");

                // Let the main thread resume. 
                // UserToken is the AutoResetEvent object that the main thread 
                // is waiting for.
                ((AutoResetEvent)e.UserState).Set();
            }

            // If an error occurred, display the exception to the user.
            if (e.Error != null)
            {
                Console.WriteLine("Ping failed>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ");
                //this will print exception
                //Console.WriteLine (e.Error.ToString ());

                // Let the main thread resume. 
                ((AutoResetEvent)e.UserState).Set();
            }

            PingReply reply = e.Reply;

            DisplayReply(reply);

            // Let the main thread resume.
            ((AutoResetEvent)e.UserState).Set();
        }
        catch (PingException pe)
        {
            Console.WriteLine("INVALID IP ADDRESS");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception " + ex.Message);
        }
    }

    public static void DisplayReply (PingReply reply)
    {
        if (reply == null)
            return;

        Console.WriteLine ("ping status: {0}", reply.Status);
        if (reply.Status == IPStatus.Success)
        {
            Console.WriteLine ("Address: {0}", reply.Address.ToString ());
            Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime);
            Console.WriteLine ("Time to live: {0}", reply.Options.Ttl);
            //Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment);
            Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length);
        }
    }

  private static long ToInt(string addr) 
    {

        return (long)(uint)System.Net.IPAddress.NetworkToHostOrder(
            (int)System.Net.IPAddress.Parse(addr).Address);    
    }

   private static string ToAddr(long address)
    {
        return System.Net.IPAddress.Parse(address.ToString()).ToString();
    }

static int temp = 0;
    private static void scanLiveHosts(string ipFrom, string ipTo)
    {
        long from =  Program.ToInt(ipFrom);
        long to =  Program.ToInt(ipTo);

        long ipLong = Program.ToInt(ipFrom);
        while ( from < to)
        {

            string address = Program.ToAddr(ipLong);
            Program.sendAsyncPingPacket(address);
            ipLong++;
        }

    }
        static void Main(string[] args)
    {
        try
        {
            Program.getDeviceList();
            Program.sendAsyncPingPacket("192.168.3.72");
            Program.scanLiveHosts("192.168.3.1", "192.168.3.41");



        }
        catch (InvalidOperationException ioe)
        {
            Console.WriteLine(ioe.Message);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

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

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

发布评论

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

评论(2

逆夏时光 2024-08-14 23:42:10

使用的主要方法是

private static long ToInt(string addr) 
{

    return (long)(uint)System.Net.IPAddress.NetworkToHostOrder(
        (int)System.Net.IPAddress.Parse(addr).Address);    
}



private static string ToAddr(long address)
{
    return System.Net.IPAddress.Parse(address.ToString()).ToString();
}

上面的帖子深入介绍了如何发送 ping 数据包和扫描网络。

Main Methods used are

private static long ToInt(string addr) 
{

    return (long)(uint)System.Net.IPAddress.NetworkToHostOrder(
        (int)System.Net.IPAddress.Parse(addr).Address);    
}



private static string ToAddr(long address)
{
    return System.Net.IPAddress.Parse(address.ToString()).ToString();
}

above post is in depth how to send ping packet and scan network.

一身软味 2024-08-14 23:42:10

嘿,我通过谷歌找到了代码,我做了一些更改来适应我的代码,并进行了其他更改以适合您的代码...也许您应该考虑更改:

while ( from < to)
{

    string address = Program.ToAddr(ipLong);
    Program.sendAsyncPingPacket(address);
    ipLong++;
}

到类似:

while ( ipLong < to)
{

    string address = Program.ToAddr(ipLong);
    Program.sendAsyncPingPacket(address);
    ipLong++;
}

或对 ips 的“追求”不会停止于到地址(到)

Hey i found the code by google, i've make little changes to fit my code and other changes to fit your code... maybe you should think about change:

while ( from < to)
{

    string address = Program.ToAddr(ipLong);
    Program.sendAsyncPingPacket(address);
    ipLong++;
}

to something like:

while ( ipLong < to)
{

    string address = Program.ToAddr(ipLong);
    Program.sendAsyncPingPacket(address);
    ipLong++;
}

or the "quest" for ips will not stop at ToAddr(to)

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