C#Ping不同的子网

时间:2012-05-27 20:31:26

标签: c# ip-address ping subnet

这里的第一个问题!

我已编写并借用代码以形成IP地址和MAC地址查找器控制台应用程序。它发送Asynchrous Ping请求,并为它找到的每个IP地址和ARP请求找到MAC地址。

如何将其配置为使用与/ 24(255.255.255.0)不同的子掩码来查找IP地址?

这不适用于僵尸网络。我的朋友是网络技术人员。

using System;
using System.Diagnostics;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

namespace FindIpAndMacPro
{
    internal class Program
    {
        private static CountdownEvent _countdown;
        private static int _upCount;
        private static readonly object LockObj = new object();

        private static void Main()
        {
            _countdown = new CountdownEvent(1);
            var sw = new Stopwatch();
            sw.Start();
            Console.Write("Skriv in IP-Adress");
            string ipBase = Console.ReadLine();

            for (int i = 0; i < 255; i++)
            {
                string ip = ipBase + "." + i;
                //Console.WriteLine(ip);
                var p = new Ping();
                p.PingCompleted += PPingCompleted;
                _countdown.AddCount();
                p.SendAsync(ip, 100, ip);
            }

            _countdown.Signal();
            _countdown.Wait();
            sw.Stop();
            new TimeSpan(sw.ElapsedTicks);
            Console.WriteLine("Took {0} milliseconds. {1} hosts active.", sw.ElapsedMilliseconds, _upCount);
            Console.WriteLine("External IP (whatismyip.com): {0}", GetExternalIp());
            Console.ReadLine();
        }

        private static void PPingCompleted (object sender, PingCompletedEventArgs e)
        {
            var ip = (string) e.UserState;
            if (e.Reply != null && e.Reply.Status == IPStatus.Success)
            {

                {
                    string name;
                    string macAddress = "";

                    try
                    {
                        IPHostEntry hostEntry = Dns.GetHostEntry(ip);
                        name = hostEntry.HostName;
                        macAddress = GetMac(ip);
                    }
                    catch (SocketException)
                    {
                        name = "?";
                    }
                    Console.WriteLine("{0} | {1} ({2}) is up: ({3} ms)", ip, macAddress, name, e.Reply.RoundtripTime);
                }
                lock (LockObj)
                {
                    _upCount++;
                }
            }
            else if (e.Reply == null)
            {
                Console.WriteLine("Pinging {0} failed. (Null Reply object?)", ip);
            }
            _countdown.Signal();
        }

        [DllImport ("iphlpapi.dll")]
        public static extern int SendARP (int destIp, int srcIp, [Out] byte[] pMacAddr, ref int phyAddrLen);

        private static string GetMac (String ip)
        {
            IPAddress addr = IPAddress.Parse(ip);
            var mac = new byte[6];
            int len = mac.Length;
            SendARP(ConvertIpToInt32(addr), 0, mac, ref len);
            return BitConverter.ToString(mac, 0, len);
        }

        private static Int32 ConvertIpToInt32 (IPAddress apAddress)
        {
            byte[] bytes = apAddress.GetAddressBytes();
            return BitConverter.ToInt32(bytes, 0);
        }

        private static string GetExternalIp()
        {
            const string whatIsMyIp = "http://automation.whatismyip.com/n09230945.asp";
            var wc = new WebClient();
            var utf8 = new UTF8Encoding();
            string requestHtml = "";
            try
            {
                requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp));
            }
            catch (WebException we)
            {
                Console.WriteLine(we.ToString());
            }

            return requestHtml;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

首先你必须从subnetmask计算网络。如果您的子网掩码是255.255.248.0,那么你可以通过从255减去子网掩码来轻松计算。例如:192.168.32.0/21(255.255.248.0)

 255.255.255.255
-255.255.248.0
 ---------------
  0 . 0 . 7 . 255

网络范围从192.168.32.0到192.168.39.255(32 + 7) 每0都是你的ipBase 192.168。其余的是用for循环完成的。因此,对于所有可能的网络,您最多需要4个循环。一个ip地址的每个八位字节。 也许你可以使用现有的类来计算子网,但我不知道这样的类