0

这里的第一个问题!

我编写并借用了代码来形成一个 IP 地址和 MAC 地址查找器控制台应用程序。它发送异步 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;
        }
    }
}
4

1 回答 1

0

首先,您必须从子网掩码计算网络。如果您的子网掩码是 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 个 for 循环。一个 IP 地址的每个八位字节。也许您可以使用现有的类来计算子网,但我不知道这样的类

于 2012-05-27T21:13:28.953 回答