2009-07-18 4 views
24

मैं यह पता लगाने की कोशिश कर रहा हूं कि हमारे LAN में कौन से डिवाइस ऑनलाइन और ऑफ़लाइन हैं। मैंने कई कार्यक्रमों को एक प्रकार का ग्राफिकल नेटवर्क अवलोकन देखा है, जिसमें लैन आईपी और मैक पते प्रस्तुत किए गए हैं। मैं जानना चाहता हूं कि कैसे और कैसे (एआरपी?) जानकारी सी #/नेट से खींचा जा सकता है?मैं .NET के माध्यम से एआरपी-प्रोटोकॉल जानकारी कैसे प्राप्त करूं?

कोई भी नमूना कोड स्निपेट/लिंक की सराहना की जाएगी।

+0

डेटा, मुझे नहीं पता, एसएनएमपी के माध्यम से उपलब्ध हो सकता है। – ChrisW

+0

आप लैन को कैसे परिभाषित कर रहे हैं? ईथरनेट सेगमेंट? आईपी ​​ब्लॉक में सबकुछ? –

+0

मैं "मेरे नेटवर्क कार्ड" से देखे गए स्थानीय ईथरनेट के रूप में लैन को परिभाषित करता हूं - मैं एक सेवा/डीएल (कुछ) चाहता हूं जिसे मैं उदाहरण से कॉल कर सकता हूं। एक वेबसर्वर या कुछ जो रिपोर्ट करेगा कि कौन से आईपी वर्तमान आईपी सेगमेंट में सक्रिय हैं (सभी संयोजनों को पिंग किए बिना) और फिर प्रत्येक सक्रिय आईपी के लिए मैक प्राप्त करने के लिए जो कनेक्ट है (जो हमें वर्तमान नेटवर्क को आसानी से लॉग/विज़ुअलाइज़ करने की अनुमति देगा) ।) – BerggreenDK

उत्तर

30

यदि आप जानते हैं कि कौन से डिवाइस बाहर हैं तो आप Ping Class का उपयोग कर सकते हैं। यह आपको कम से कम एआरपी तालिका भरने की अनुमति देगा। आप हमेशा एआरपी-ए निष्पादित कर सकते हैं और आउटपुट को पार्स कर सकते हैं यदि आपको करना है। यहां एक लिंक भी है जो दिखाता है कि GetIpNetTable पर कॉल करने के लिए पिनवोक कैसे करें। मैंने पिंग क्लास के नीचे उदाहरण और GetIpNetTable का उपयोग करके एआरपी तालिका तक पहुंच कैसे प्राप्त की है।

यह पिंग कक्षा

using System; 
using System.Net; 
using System.Net.NetworkInformation; 
using System.Text; 

namespace Examples.System.Net.NetworkInformation.PingTest 
{ 
    public class PingExample 
    { 
     // args[0] can be an IPaddress or host name. 
     public static void Main (string[] args) 
     { 
      Ping pingSender = new Ping(); 
      PingOptions options = new PingOptions(); 

      // Use the default Ttl value which is 128, 
      // but change the fragmentation behavior. 
      options.DontFragment = true; 

      // Create a buffer of 32 bytes of data to be transmitted. 
      string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; 
      byte[] buffer = Encoding.ASCII.GetBytes (data); 
      int timeout = 120; 
      PingReply reply = pingSender.Send (args[0], timeout, buffer, options); 
      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); 
      } 
     } 
    } 
} 

के लिए एक उदाहरण है यह GetIpNetTable का एक उदाहरण है।

using System; 
using System.Runtime.InteropServices; 
using System.ComponentModel; 
using System.Net; 

namespace GetIpNetTable 
{ 
    class Program 
    { 
     // The max number of physical addresses. 
     const int MAXLEN_PHYSADDR = 8; 

     // Define the MIB_IPNETROW structure. 
     [StructLayout(LayoutKind.Sequential)] 
     struct MIB_IPNETROW 
     { 
     [MarshalAs(UnmanagedType.U4)] 
     public int dwIndex; 
     [MarshalAs(UnmanagedType.U4)] 
     public int dwPhysAddrLen; 
     [MarshalAs(UnmanagedType.U1)] 
     public byte mac0; 
     [MarshalAs(UnmanagedType.U1)] 
     public byte mac1; 
     [MarshalAs(UnmanagedType.U1)] 
     public byte mac2; 
     [MarshalAs(UnmanagedType.U1)] 
     public byte mac3; 
     [MarshalAs(UnmanagedType.U1)] 
     public byte mac4; 
     [MarshalAs(UnmanagedType.U1)] 
     public byte mac5; 
     [MarshalAs(UnmanagedType.U1)] 
     public byte mac6; 
     [MarshalAs(UnmanagedType.U1)] 
     public byte mac7; 
     [MarshalAs(UnmanagedType.U4)] 
     public int dwAddr; 
     [MarshalAs(UnmanagedType.U4)] 
     public int dwType; 
     } 

     // Declare the GetIpNetTable function. 
     [DllImport("IpHlpApi.dll")] 
     [return: MarshalAs(UnmanagedType.U4)] 
     static extern int GetIpNetTable(
     IntPtr pIpNetTable, 
     [MarshalAs(UnmanagedType.U4)] 
     ref int pdwSize, 
     bool bOrder); 

     [DllImport("IpHlpApi.dll", SetLastError = true, CharSet = CharSet.Auto)] 
     internal static extern int FreeMibTable(IntPtr plpNetTable); 

     // The insufficient buffer error. 
     const int ERROR_INSUFFICIENT_BUFFER = 122; 

     static void Main(string[] args) 
     { 
     // The number of bytes needed. 
     int bytesNeeded = 0; 

     // The result from the API call. 
     int result = GetIpNetTable(IntPtr.Zero, ref bytesNeeded, false); 

     // Call the function, expecting an insufficient buffer. 
     if (result != ERROR_INSUFFICIENT_BUFFER) 
     { 
      // Throw an exception. 
      throw new Win32Exception(result); 
     } 

     // Allocate the memory, do it in a try/finally block, to ensure 
     // that it is released. 
     IntPtr buffer = IntPtr.Zero; 

     // Try/finally. 
     try 
     { 
      // Allocate the memory. 
      buffer = Marshal.AllocCoTaskMem(bytesNeeded); 

      // Make the call again. If it did not succeed, then 
      // raise an error. 
      result = GetIpNetTable(buffer, ref bytesNeeded, false); 

      // If the result is not 0 (no error), then throw an exception. 
      if (result != 0) 
      { 
       // Throw an exception. 
       throw new Win32Exception(result); 
      } 

      // Now we have the buffer, we have to marshal it. We can read 
      // the first 4 bytes to get the length of the buffer. 
      int entries = Marshal.ReadInt32(buffer); 

      // Increment the memory pointer by the size of the int. 
      IntPtr currentBuffer = new IntPtr(buffer.ToInt64() + 
       Marshal.SizeOf(typeof(int))); 

      // Allocate an array of entries. 
      MIB_IPNETROW[] table = new MIB_IPNETROW[entries]; 

      // Cycle through the entries. 
      for (int index = 0; index < entries; index++) 
      { 
       // Call PtrToStructure, getting the structure information. 
       table[index] = (MIB_IPNETROW) Marshal.PtrToStructure(new 
        IntPtr(currentBuffer.ToInt64() + (index * 
        Marshal.SizeOf(typeof(MIB_IPNETROW)))), typeof(MIB_IPNETROW)); 
      } 

      for (int index = 0; index < entries; index++) 
      { 
       MIB_IPNETROW row = table[index]; 
       IPAddress ip=new IPAddress(BitConverter.GetBytes(row.dwAddr)); 
       Console.Write("IP:"+ip.ToString()+"\t\tMAC:"); 

       Console.Write(row.mac0.ToString("X2") + '-'); 
       Console.Write(row.mac1.ToString("X2") + '-'); 
       Console.Write(row.mac2.ToString("X2") + '-'); 
       Console.Write(row.mac3.ToString("X2") + '-'); 
       Console.Write(row.mac4.ToString("X2") + '-'); 
       Console.WriteLine(row.mac5.ToString("X2")); 

      } 
     } 
     finally 
     { 
      // Release the memory. 
      FreeMibTable(buffer); 
     } 
     } 
    } 
} 
+0

क्षमा करें, लेकिन विचार यह पता था कि वर्तमान में कौन सी मशीनें ऑनलाइन हैं। हम कनेक्ट होने के समय आईपी को नहीं जानते हैं, क्योंकि वे वायरलेस से आ सकते हैं और आईपी को डीएचसीपी के माध्यम से प्राप्त कर सकते हैं। मुझे पता है कि हम राउटर लॉक कर सकते हैं, लेकिन विचार एआरपी + से मैक पते की तलाश करना था नेटवर्क से जुड़े उपकरणों की वर्तमान सूची इकट्ठा करना। – BerggreenDK

+0

क्या आप जानते हैं कि आईएसी नंबर से मैक पता कैसे प्राप्त करें? – BerggreenDK

+0

ग्रेट सामान! यह मुझे परीक्षण करना होगा। मैंने इसे अभी जवाब के रूप में चिह्नित किया है। आपका बहुत बहुत धन्यवाद! मुझे आगे लाता है ... "एक विशाल छलांग": ओ) – BerggreenDK

2

उम्मीद है कि आप आईपी पते से मैक पते प्राप्त करने की कोशिश कर रहे हैं, न कि दूसरी तरफ।

ARP Resolver

मैं इसे करने की कोशिश नहीं की है, हमें पता है कि यह कैसे काम करता है:

यहाँ एक पुरुष के उदाहरण के लिए एक कड़ी है।

+0

में उदाहरण कोड देखें लिंक के लिए धन्यवाद, लेकिन क्या उस उदाहरण को निम्नलिखित की आवश्यकता नहीं होगी: Tamir.IPLib का उपयोग कर ; Tamir.IPLib.Packets का उपयोग कर ; Tamir.IPLib.Util का उपयोग कर ; ??? – BerggreenDK

+0

मैं यह भी पता लगाने की कोशिश कर रहा हूं कि कैसे/अगर कमांडप्रोप्ट "arp -a" का "सी #" संस्करण बनाना संभव है ... छुपे हुए कमांडप्रोप्ट को कॉल करके नहीं, लेकिन किसी भी तरह से कोड द्वारा एआरपी कमांड करके मर्दाना। जैसा कि मैंने अभी तक समझा है, एआरपी कमांड वर्तमान उपलब्ध आईपी + "इस नेटवर्क कार्ड" से देखे गए उनके मैक पते सूचीबद्ध करता है ... और यह हमारी आवश्यकताओं को पूरी तरह अनुरूप करेगा। – BerggreenDK

+1

एआरपी कमांड इसे पूरा करने के लिए एक सॉकेट पर कच्चे बाइट भेज रहा है। लिंक में कक्षा आईपी पते से एक मैक पता हल कर सकती है, क्या आपको इसकी आवश्यकता है, या आपको किसी भी तरह से "आईपी पते" खोजने की ज़रूरत है? शीर्ष पर उपयोग कथन SharpPcap.dll से आते हैं जो ऊपर पोस्ट किए गए लिंक से डाउनलोड करने योग्य और मुक्त स्रोत है। – jonathanpeppers

-4

Google "fingbox" के लिए खोज। ऐसा लगता है कि आप घुसपैठियों का पता लगाने की कोशिश कर रहे हैं?

यह एक घुसपैठ करने वाला डिटेक्टर डिवाइस है जो पूरी तरह से कानूनी है और यह जानना अच्छा है कि कौन सी बंदरगाहों पर आपके वाईफाई का उपयोग कर परिवहन कर रहा है। कभी-कभी यह मैक पता भी दिखाता है और पिंग कर सकता है। अन्य सुविधाओं के ढेर है।

+0

यह सीओडीई से परीक्षण करने के बारे में है जो ऐप या हार्डवेयर नहीं खरीद रहा है। मुझे फिंगबॉक्स के बारे में पता है, यह मेरे मूल प्रश्न से बहुत बाद में आया था। – BerggreenDK

संबंधित मुद्दे

 संबंधित मुद्दे