2009-10-19 109 views
0

如何掃描特定範圍的IP並將其增加到用戶定義的範圍..與大多數端口掃描器一樣。但如何提高主機位..這增加了網絡的位..如何掃描IP範圍C#

private static void sendAsyncPingPacket(string hostToPing) 
    { 
     try 
     { 
      int timeout = 5000; 
      AutoResetEvent waiter = new AutoResetEvent(false); 
      Ping pingPacket = new Ping(); 
      //ping completion event reaised 
      pingPacket.PingCompleted += new PingCompletedEventHandler(PingCompletedCallback); 
      string data = "Ping test check"; 
      byte[] byteBuffer = Encoding.ASCII.GetBytes(data); 
      PingOptions pingOptions = new PingOptions(64, true); 
      Console.WriteLine("Time to live: {0}", pingOptions.Ttl); 
      //Console.WriteLine("Don't fragment: {0}", pingOptions.DontFragment); 
      pingPacket.SendAsync(hostToPing, timeout, byteBuffer, pingOptions, waiter); 


      //do something useful 
      waiter.WaitOne(); 
      Console.WriteLine("Ping RoundTrip returned, Do something useful here..."); 
     } 
     catch (PingException pe) 
     { 
      Console.WriteLine("INVALID IP ADDRESS FOUND"); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("Exceptin " + ex.Message); 
     } 

    } 
    private static void PingCompletedCallback(object sender, PingCompletedEventArgs e) 
    { 
     try 
     { 
      if (e.Cancelled) 
      { 
       Console.WriteLine("Ping canceled."); 

       // Let the main thread resume. 
       // UserToken is the AutoResetEvent object that the main thread 
       // is waiting for. 
       ((AutoResetEvent)e.UserState).Set(); 
      } 

      // If an error occurred, display the exception to the user. 
      if (e.Error != null) 
      { 
       Console.WriteLine("Ping failed>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> "); 
       //this will print exception 
       //Console.WriteLine (e.Error.ToString()); 

       // Let the main thread resume. 
       ((AutoResetEvent)e.UserState).Set(); 
      } 

      PingReply reply = e.Reply; 

      DisplayReply(reply); 

      // Let the main thread resume. 
      ((AutoResetEvent)e.UserState).Set(); 
     } 
     catch (PingException pe) 
     { 
      Console.WriteLine("INVALID IP ADDRESS"); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("Exception " + ex.Message); 
     } 
    } 

    public static void DisplayReply (PingReply reply) 
    { 
     if (reply == null) 
      return; 

     Console.WriteLine ("ping status: {0}", reply.Status); 
     if (reply.Status == IPStatus.Success) 
     { 
      Console.WriteLine ("Address: {0}", reply.Address.ToString()); 
      Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime); 
      Console.WriteLine ("Time to live: {0}", reply.Options.Ttl); 
      //Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment); 
      Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length); 
     } 
    } 

    private static long ToInt(string addr) 
    { 

     return (long)(uint)System.Net.IPAddress.NetworkToHostOrder(
      (int)System.Net.IPAddress.Parse(addr).Address);  
    } 

    private static string ToAddr(long address) 
    { 
     return System.Net.IPAddress.Parse(address.ToString()).ToString(); 
    } 

static int temp = 0; 
    private static void scanLiveHosts(string ipFrom, string ipTo) 
    { 
     long from = Program.ToInt(ipFrom); 
     long to = Program.ToInt(ipTo); 

     long ipLong = Program.ToInt(ipFrom); 
     while (from < to) 
     { 

      string address = Program.ToAddr(ipLong); 
      Program.sendAsyncPingPacket(address); 
      ipLong++; 
     } 

    } 
     static void Main(string[] args) 
    { 
     try 
     { 
      Program.getDeviceList(); 
      Program.sendAsyncPingPacket("192.168.3.72"); 
      Program.scanLiveHosts("192.168.3.1", "192.168.3.41"); 



     } 
     catch (InvalidOperationException ioe) 
     { 
      Console.WriteLine(ioe.Message); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 
    } 
+0

你能告訴你有這麼遠的代碼? – 2009-10-19 08:05:00

+0

getDeviceList()方法的定義在哪裏? – TheSpy 2014-02-24 08:34:50

+0

我現在不記得了,只是從適配器獲取設備列表。 – 2014-02-24 09:32:20

回答

1

主要使用的方法是

private static long ToInt(string addr) 
{ 

    return (long)(uint)System.Net.IPAddress.NetworkToHostOrder(
     (int)System.Net.IPAddress.Parse(addr).Address);  
} 



private static string ToAddr(long address) 
{ 
    return System.Net.IPAddress.Parse(address.ToString()).ToString(); 
} 

上述職位的深度如何發送ping包和掃描網絡。

1

嘿,我發現了由谷歌的代碼,我已經做出變化不大適合我的代碼和其他的變化,以適應你的代碼......也許你應該考慮一下改變:

while (from < to) 
{ 

    string address = Program.ToAddr(ipLong); 
    Program.sendAsyncPingPacket(address); 
    ipLong++; 
} 

喜歡的東西:

while (ipLong < to) 
{ 

    string address = Program.ToAddr(ipLong); 
    Program.sendAsyncPingPacket(address); 
    ipLong++; 
} 

或「追求」爲IPS不會在ToAddr(到)停止