ping أو أخبر ما إذا كان الجهاز موجودًا على الشبكة بواسطة Mac في C#

StackOverflow https://stackoverflow.com/questions/2567107

  •  24-09-2019
  •  | 
  •  

سؤال

أنا أقوم بتطوير تطبيق أمن المنزل. شيء واحد أود القيام به هو إيقاف تشغيله تلقائيًا واستنادًا إلى ما إذا كنت في المنزل أم لا. لدي هاتف مع WiFi يتصل تلقائيًا بشبكتي عندما أكون في المنزل.

يتصل الهاتف ويحصل على عنوانه عبر DHCP. بينما يمكنني تكوينه لاستخدام IP ثابت ، لا أفضل ذلك. هل هناك أي نوع من "ping" أو ما يعادله في C# / .NET يمكنه أخذ عنوان MAC لجهاز ويخبرني ما إذا كان نشطًا حاليًا على الشبكة أم لا؟

تعديل: للتوضيح ، أقوم بتشغيل برنامج على جهاز كمبيوتر أرغب في اكتشاف الهاتف على نفس الشبكة المحلية.

تعديل: إليكم الكود الذي توصلت إليه ، وذلك بفضل مساعدة سبولسون. يكتشف بشكل موثوق ما إذا كان أي من الهواتف التي أهتم بها في المنزل أم لا.

private bool PhonesInHouse()
{

    Ping p = new Ping();
    // My home network is 10.0.23.x, and the DHCP 
    // pool runs from 10.0.23.2 through 10.0.23.127.

    int baseaddr = 10;
    baseaddr <<= 8;
    baseaddr += 0;
    baseaddr <<= 8;
    baseaddr += 23;
    baseaddr <<= 8;

    // baseaddr is now the equivalent of 10.0.23.0 as an int.

    for(int i = 2; i<128; i++) {
        // ping every address in the DHCP pool, in a separate thread so 
        // that we can do them all at once
        IPAddress ip = new IPAddress(IPAddress.HostToNetworkOrder(baseaddr + i));
        Thread t = new Thread(() => 
             { try { Ping p = new Ping(); p.Send(ip, 1000); } catch { } });
        t.Start();
    }

    // Give all of the ping threads time to exit

    Thread.Sleep(1000);

    // We're going to parse the output of arp.exe to find out 
    // if any of the MACs we're interested in are online

    ProcessStartInfo psi = new ProcessStartInfo();
    psi.Arguments = "-a";
    psi.FileName = "arp.exe";
    psi.RedirectStandardOutput = true;
    psi.UseShellExecute = false;
    psi.CreateNoWindow = true;

    bool foundone = false;
    using (Process pro = Process.Start(psi))
    {
        using (StreamReader sr = pro.StandardOutput)
        {
            string s = sr.ReadLine();

            while (s != null)
            {
                if (s.Contains("Interface") || 
                    s.Trim() == "" || 
                    s.Contains("Address"))
                {
                    s = sr.ReadLine();
                    continue;
                }
                string[] parts = s.Split(new char[] { ' ' }, 
                    StringSplitOptions.RemoveEmptyEntries);

                // config.Phones is an array of strings, each with a MAC 
                // address in it.
                // It's up to you to format them in the same format as 
                // arp.exe
                foreach (string mac in config.Phones)
                {
                    if (mac.ToLower().Trim() == parts[1].Trim().ToLower())
                    {
                        try
                        {
                            Ping ping = new Ping();
                            PingReply pingrep = ping.Send(parts[0].Trim());
                            if (pingrep.Status == IPStatus.Success)
                            {
                                foundone = true;
                            }
                        }
                        catch { }
                        break;
                    }
                }
                s = sr.ReadLine();
            }
        }
    }

    return foundone;
}
هل كانت مفيدة؟

المحلول

نهج مختلف هو الاستخدام ping و arp أدوات. نظرًا لأن حزم ARP لا يمكنها البقاء إلا في مجال البث نفسه ، يمكنك Ping Ping عنوان البث الخاص بشبكتك وسيقوم كل عميل بالرد باستخدام استجابة ARP. يتم تخزين كل من هذه الردود في جدول ARP الخاص بك ، والذي يمكنك عرضه مع الأمر arp -a. لذلك المتهدمة:

rem Clear ARP cache
netsh interface ip delete arpcache

rem Ping broadcast addr for network 192.168.1.0
ping -n 1 192.168.1.255

rem View ARP cache to see if the MAC addr is listed
arp -a

يمكن القيام ببعض هذه في التعليمات البرمجية المدارة ، كما في System.Net.NetworkInformation مساحة الاسم.

ملاحظة: قد يكون لتطهير ذاكرة التخزين المؤقت ARP تأثير هامشي على أداء الشبكة عن طريق مسح الإدخالات المخبأة للخوادم المحلية الأخرى. ومع ذلك ، عادة ما يتم مسح ذاكرة التخزين المؤقت كل 20 دقيقة أو أقل على أي حال.

نصائح أخرى

يمكنك استخدام بروتوكول RARP لبث طلب من يملك عنوان MAC. سوف يستجيب المالك بعنوان IP الخاص به. لا أعرف أداة أو مكتبة RARP التي أوصي بها ، لذلك يمكنك التفكير في كتابة طبقة الشبكة الخاصة بك لإرسال/تلقي هذه الحزم.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top