这里的第一个问题!
我编写并借用了代码来形成一个 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;
}
}
}