0

ここで最初の質問!

IPアドレスとMACアドレスファインダーコンソールアプリケーションを作成するためのコードを記述して借用しました。非同期Ping要求を送信し、検出したすべてのIPアドレスに対して、MACアドレスを検出するためのARP要求を送信します。

/ 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アドレスのオクテットごとに1つ。サブネットの計算に既存のクラスを使用できるかもしれませんが、そのようなクラスはわかりません

于 2012-05-27T21:13:28.953 に答える