первый вопрос здесь!C# Ping различные подсети
Я написал и заимствовал код, чтобы сформировать приложение для поиска 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;
}
}
}
Быстрый ответ! Я мог бы написать класс, который принимает Subnetand IPAddress, разбивает его на октеты и узнает количество сетей. открытый класс IPAddress IPAddress ip; //192.168.32.0 int [] октеты; 192 | 168 | 32 | 0 int subnetMask; 21 public IPAddress (IPAddress ip, int subnetmask); частный int [] calcSubnet (21); return int subnetOctets; //255.255.248.0 частный findHosts(); Извините! Ваш пост был хорошим, с пальцами. –
, если это было полезно для вас, пожалуйста, отметьте его как ответ и закройте вопрос – user1008764