0

I have currently wrote a code which scans the local network with a function called "bool ping()". I want to add a capability to provide basic device information when the function returns 'true'. I've found 'ManagementObjectSearcher'. At first it looked perfect but when It gets used for a non-windows device it crash. Therefore I suppose that this method cannot be used for non-windows devices.

As I want to add the below code ( or login , after enough polishing ), to an android app that scans the local network and returns the A) IP address and B) (one of the following )

  1. the device type ( desktop , laptop , smartphone) and/or
  2. the OS type ( android , windows , linux , tvOS )

Is there a valid way I can do what I am looking for? I believe I have experienced apps that do stuff like that, though I don't know what language they were based on.

namespace LanConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            string host = "192.168.1.10"; // android smartphone IP
            string temp = null;
            
            // arguments I found 
            string[] _searchClass = { "Win32_ComputerSystem", "Win32_OperatingSystem", "Win32_BaseBoard", "Win32_BIOS" };
            string[] param = { "UserName", "Caption", "Product", "Description" };

            bool v = ping(host, 10, 900); // bool function send ping to host 
            if (v == true)
            {   
                Console.WriteLine("true");
                for (int i = 0; i <= _searchClass.Length - 1; i++)
                {
                    ManagementObjectSearcher searcher = new ManagementObjectSearcher("\\\\" + host + "\\root\\CIMV2", "SELECT *FROM " + _searchClass[i]);
                    foreach (ManagementObject obj in searcher.Get())
                    {
                        temp += obj.GetPropertyValue(param[i]).ToString() + "\n";
                        if (i == _searchClass.Length - 1)
                        {
                            Console.WriteLine(temp, "Hostinfo: " + host);
                            break;
                        }
                    }
                    Console.WriteLine("");
                }
            }
            else
                Console.WriteLine("false");
        }

        public static bool ping(string host, int attempts, int timeout)
        {
            System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();

            System.Net.NetworkInformation.PingReply pingReply;

            for (int i = 0; i < attempts; i++)
            {
                try
                {
                    pingReply = ping.Send(host, timeout);
                    // If there is a successful ping, return true.
                    if (pingReply != null &&
                        pingReply.Status == System.Net.NetworkInformation.IPStatus.Success)
                    {
                        return true;
                    }
                }
                catch
                {
                    // supressing errors
                }
            }
            // Return false if ping fales "attempts" times
            return false;
        }
    }
}
ehem
  • 70
  • 7
  • The best you can do is use the MAC which is returned in the ping which contains the manufacturer, model, serial number of the Ethernet interface. – jdweng Nov 16 '20 at 01:14
  • I've found that : "A ping travels the Network Layer (layer 3) where the MAC address is part of the Data Link Layer (layer 2), making it not possible for a ping to tell you anything about the MAC address of the responding device. You might want to consider using WMI to get the MAC address." withc WMI uses System.Management which seems not to work with non-windows devices. In addition I've found a almost working method calling the ARP through command line. But I (think) can't call a command line to call ARP in android application – ehem Nov 16 '20 at 13:39
  • A Ping is a ARP and ARP Response. Which give the MAC. To see an example of the ARP data from cmd.exe >ARP -a The MAC is the Physical Address. – jdweng Nov 16 '20 at 13:42
  • thank you very much for your answer. I edited my response second before your reply. If you can check it out and reply back. Much appreciated – ehem Nov 16 '20 at 13:43
  • What do you men by Universal Device? Do you mean USB? You code is using an Ethernet address so your Ethernet data may be on the USB interface. – jdweng Nov 16 '20 at 13:45
  • Sorry for the misleading. I mean that the android app ( i am trying to build) it can get the MAC address ( you suggested ) from any device ( android , windows , linux , tv ) – ehem Nov 16 '20 at 13:46
  • MAC and ManagementObjectSearcher results are different. ManagementObjectSearcher you can get from a remote machine to get the data from Device Manager on a remote machine. See : https://learn.microsoft.com/en-us/windows/win32/wmisdk/connecting-to-wmi-remotely-with-c- – jdweng Nov 16 '20 at 13:51
  • I cant use the ManagementObjectSearcher because It won't work if the target device is a non-windows device. I am taking your suggestion and I am trying to use the MAC address to identify the target machine for information. But I can't find a way to get the MAC address form a PING request from and android phone to a windows or linux or (another) android device – ehem Nov 16 '20 at 13:55
  • unless I am mistaken and the ManagementObjectSearcher can gather information from a non-windows device – ehem Nov 16 '20 at 13:57
  • The Ping Response (ARP response) the MAC goes into the machine ARP table. You can see the ARP table from cmd.exe >ARP -A. So to get the MAC you can send a PING which will add the AMC to the ARP table. Then read the ARP table. A machine automatically sends an ARP when turned on and then periodically (usually every 1/2 hour). But the ARP message only stays in the local subnet and is not forwarded by a router. So if device is in local subnet you can simply read the ARP table. if device is outside subnet you must send a PING to get the MAC. – jdweng Nov 16 '20 at 14:12
  • I used the ARP -a and it didn't showed my mobile device. I made a ping to my smartphone IP. Issued the ARP -a command again and then it showed in the ARP table – ehem Nov 16 '20 at 15:18
  • That is what I said. The device is not on the local subnet. You can read ARP table in c# using following : https://stackoverflow.com/questions/1148778/how-do-i-access-arp-protocol-information-through-net – jdweng Nov 16 '20 at 15:21

0 Answers0