2012-05-27 3 views
0

첫 번째 질문은 여기!C# 다른 서브넷을 핑

IP 주소 및 MAC 주소 찾기 콘솔 응용 프로그램을 작성하기 위해 코드를 작성하고 빌려 왔습니다. Asynchrous Ping 요청을 보내고 모든 IP 주소에 대해 MAC 주소를 찾기위한 ARP 요청과 ARP 요청을 찾습니다.

IP 주소를 찾으려면/24 (255.255.255.0)와 다른 서브 마스크로 작동하도록 어떻게 구성 할 수 있습니까?

이것은 봇넷이 아닙니다. 그것은 네트워크 기술자 인 나의 친구를위한 것입니다.

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; 
     } 
    } 
} 

답변

0

는 무엇보다도 당신은 255.255.248.0은 다음 easly 255 예에서 서브넷 마스크를 차감하여 산출 할 수있다 당신의 서브넷 마스크를 subnetmask.if에서 네트워크를 계산했다 : 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 주소의 모든 옥텟에 하나씩. 아마도 서브넷 계산에 기존 클래스를 사용할 수 있지만 그런 클래스는 모른다.

+0

빠른 답변! 서브넷을 취하는 클래스를 작성할 수 있습니다. IPAddress는이를 옥텟으로 분할하고 네트워크 수를 찾습니다. 공용 클래스 IPAddress IPAddress ip; //192.168.32.0 int [] 옥텟; 192 | 168 | 32 | 0 int subnetMask; 21 공용 IPAddress (IPAddress ip, int subnetmask); private int [] calcSubnet (21); return int subnetOctets; //255.255.248.0 private findHosts(); 죄송합니다! 귀하의 게시물은 손가락으로 잘 렸습니다. –

+0

도움이된다면 답으로 표시하고 질문을 닫으십시오. – user1008764