2012-10-09 17 views
7

में स्थानीय नेटवर्क पर एक मशीन मैक पता प्राप्त करें मैं एक ऐसा फ़ंक्शन लिखने की कोशिश कर रहा हूं जो एक पैरामीटर के रूप में एक IP address लेता है और यह पूछता है कि MAC address के लिए मेरे स्थानीय नेटवर्क पर मशीन।सी #

मैंने कई उदाहरण देखे हैं जो स्थानीय मशीन के अपने MAC address प्राप्त करते हैं, हालांकि कोई भी (मुझे नहीं मिला) जो इसके लिए स्थानीय नेटवर्क मशीन से पूछताछ करता है।

मुझे पता है कि इस तरह का एक कार्य प्राप्त करने योग्य है क्योंकि Wake on LAN scanner सॉफ़्टवेयर स्थानीय आईपी रेंज स्कैन करता है और मशीनों पर सभी का मैक पता/होस्टनाम देता है।

क्या कोई मुझे बता सकता है कि मैं सी # में इसे प्राप्त करने के लिए एक समारोह लिखने की कोशिश करना शुरू कर दूंगा? किसी भी सहायता की सराहना की जाएगी। धन्यवाद

संपादित करें:

प्रति नीचे मार्को सांसद की टिप्पणी के रूप में, एआरपी तालिकाओं का इस्तेमाल किया है। arp class

+0

यह काम करता है नहीं जानते कि क्या है, लेकिन एक त्वरित गूगल खोज के साथ मैं इस पुस्तकालय जो चाल करना चाहिए मिली: [http: // www। tamirgal.com/blog/post/ARP-Resolver-C-Class.aspx ](http://www.tamirgal.com/blog/post/ARP-Resolver-C-Class.aspx) –

+0

धन्यवाद, मुझे विश्वास है कि मैं एआरपी टेबल को असंगत होने के लिए पढ़ा है और यह सोच रहा था कि मैक पते के लिए 'पिंग' का कोई तरीका है या नहीं। –

+1

मैं ** ** सोचता हूं कि यदि आप एक नियमित पिंग (या अन्यथा संपर्क करने का प्रयास करते हैं) आईपी एड्रेस करते हैं तो यह एआरपी टेबल रीफ्रेश करने का कारण बनता है (अन्यथा नेटवर्क स्टैक मशीन को पहले स्थान से संपर्क करने में सक्षम नहीं होगा); निश्चित रूप से यह वांछित मशीन ऑनलाइन होने पर ही (अगर बिलकुल) काम करेगा। मुझे नहीं लगता कि आप ऑफ़लाइन आईपी पते के लिए विश्वसनीय परिणाम प्राप्त कर सकते हैं, विशेष रूप से यदि आपके पास गतिशील रूप से असाइन किए गए आईपी हैं। हालांकि मैं नेटवर्क विशेषज्ञ नहीं हूं, इसलिए मैं गलत हो सकता हूं (समस्या पर आपके साथ सोचने की कोशिश कर रहा हूं)। –

उत्तर

0

मार्को एमपी की टिप्पणी के अनुसार, एआरपी टेबल का उपयोग किया है। arp class

+0

1. आपका प्रश्न "आईपी प्राप्त करने और मैक पता ढूंढने" के लिए पूछता है और यह आरएआरपी के बजाय एआरपी की मांग करता है (मैक प्राप्त करें और आईपी/होस्ट लौटाएं जो आप वर्तमान में उपयोग कर रहे हैं)। आप यहां समाप्त हो गए हैं? 2. जिस साइट पर आप रेफर कर रहे हैं वह गलत प्रक्रिया का उपयोग कर रहा है (आईएपी से आईएपी एआरपी के बजाय आरएआरपी है) ... – Khan

13
public string GetMacAddress(string ipAddress) 
{ 
    string macAddress = string.Empty; 
    System.Diagnostics.Process pProcess = new System.Diagnostics.Process(); 
    pProcess.StartInfo.FileName = "arp"; 
    pProcess.StartInfo.Arguments = "-a " + ipAddress; 
    pProcess.StartInfo.UseShellExecute = false; 
    pProcess.StartInfo.RedirectStandardOutput = true; 
     pProcess.StartInfo.CreateNoWindow = true; 
    pProcess.Start(); 
    string strOutput = pProcess.StandardOutput.ReadToEnd(); 
    string[] substrings = strOutput.Split('-'); 
    if (substrings.Length >= 8) 
    { 
     macAddress = substrings[3].Substring(Math.Max(0, substrings[3].Length - 2)) 
       + "-" + substrings[4] + "-" + substrings[5] + "-" + substrings[6] 
       + "-" + substrings[7] + "-" 
       + substrings[8].Substring(0, 2); 
     return macAddress; 
    } 

    else 
    { 
     return "not found"; 
    } 
} 

बहुत देर से संपादित करें: खुला souce परियोजना में iSpy (https://github.com/ispysoftware/iSpy) वे इस कोड है, जो और भी अधिक एक छोटे से अच्छे है

public static void RefreshARP() 
     { 
      _arpList = new Dictionary<string, string>(); 
      _arpList.Clear(); 
      try 
      { 
       var arpStream = ExecuteCommandLine("arp", "-a"); 
       // Consume first three lines 
       for (int i = 0; i < 3; i++) 
       { 
        arpStream.ReadLine(); 
       } 
       // Read entries 
       while (!arpStream.EndOfStream) 
       { 
        var line = arpStream.ReadLine(); 
        if (line != null) 
        { 
         line = line.Trim(); 
         while (line.Contains(" ")) 
         { 
          line = line.Replace(" ", " "); 
         } 
         var parts = line.Trim().Split(' '); 

         if (parts.Length == 3) 
         { 
          string ip = parts[0]; 
          string mac = parts[1]; 
          if (!_arpList.ContainsKey(ip)) 
           _arpList.Add(ip, mac); 
         } 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       Logger.LogExceptionToFile(ex, "ARP Table"); 
      } 
      if (_arpList.Count > 0) 
      { 
       foreach (var nd in List) 
       { 
        string mac; 
        ARPList.TryGetValue(nd.IPAddress.ToString(), out mac); 
        nd.MAC = mac;  
       } 
      } 
     } 

https://github.com/ispysoftware/iSpy/blob/master/Server/NetworkDeviceList.cs

अद्यतन 2 का उपयोग देर से, लेकिन मुझे लगता है कि यह सबसे अच्छा है क्योंकि यह रेगेक्स का उपयोग करता है जो सटीक मैचों के लिए बेहतर जांचता है।

public string getMacByIp(string ip) 
{ 
    var macIpPairs = GetAllMacAddressesAndIppairs(); 
    int index = macIpPairs.FindIndex(x => x.IpAddress == ip); 
    if (index >= 0) 
    { 
     return macIpPairs[index].MacAddress.ToUpper(); 
    } 
    else 
    { 
     return null; 
    } 
} 

public List<MacIpPair> GetAllMacAddressesAndIppairs() 
{ 
    List<MacIpPair> mip = new List<MacIpPair>(); 
    System.Diagnostics.Process pProcess = new System.Diagnostics.Process(); 
    pProcess.StartInfo.FileName = "arp"; 
    pProcess.StartInfo.Arguments = "-a "; 
    pProcess.StartInfo.UseShellExecute = false; 
    pProcess.StartInfo.RedirectStandardOutput = true; 
    pProcess.StartInfo.CreateNoWindow = true; 
    pProcess.Start(); 
    string cmdOutput = pProcess.StandardOutput.ReadToEnd(); 
    string pattern = @"(?<ip>([0-9]{1,3}\.?){4})\s*(?<mac>([a-f0-9]{2}-?){6})"; 

    foreach (Match m in Regex.Matches(cmdOutput, pattern, RegexOptions.IgnoreCase)) 
    { 
     mip.Add(new MacIpPair() 
     { 
      MacAddress = m.Groups["mac"].Value, 
      IpAddress = m.Groups["ip"].Value 
     }); 
    } 

    return mip; 
} 
public struct MacIpPair 
{ 
    public string MacAddress; 
    public string IpAddress; 
} 
+2

यह सही जवाब है। @ मैक्रो एमपी उत्तर के बजाय पोस्ट के लिए ..... क्यों क्या यह उत्तर के रूप में नहीं चुना गया है? – Khan

+0

यह अपने पीसी मैक क्यों नहीं दिखा रहा है लेकिन अन्य सभी को पाता है? – Khan

+1

@ खान शायद इसलिए क्योंकि एआरपी तालिका में अपना स्वयं का आईपी स्टोर करने की आवश्यकता नहीं है क्योंकि आप पहले से ही अपने आईपी और मैक –

0
using System.Net; 
using System.Runtime.InteropServices; 

[DllImport("iphlpapi.dll", ExactSpelling = true)] 
public static extern int SendARP(int DestIP, int SrcIP, [Out] byte[] pMacAddr, ref int PhyAddrLen); 

try 
{ 
    IPAddress hostIPAddress = IPAddress.Parse("XXX.XXX.XXX.XX"); 
    byte[] ab = new byte[6]; 
    int len = ab.Length, 
     r = SendARP((int)hostIPAddress.Address, 0, ab, ref len); 
    Console.WriteLine(BitConverter.ToString(ab, 0, 6)); 
} 
catch (Exception ex) { } 

या के साथ पीसी का नाम

try 
     { 
      Tempaddr = System.Net.Dns.GetHostEntry("DESKTOP-xxxxxx"); 
     } 
     catch (Exception ex) { } 
     byte[] ab = new byte[6]; 
     int len = ab.Length, r = SendARP((int)Tempaddr.AddressList[1].Address, 0, ab, ref len); 
     Console.WriteLine(BitConverter.ToString(ab, 0, 6)); 
+0

आपका कोड बहुत गन्दा है। मैंने कभी ऐसा कुछ नहीं देखा है! मुझे लगता है कि आपको कुछ प्रतिष्ठा कमाने के लिए अपने कोड को स्पष्ट और साफ करना होगा। –