8

How to identify the hardware details of a Linux/Mac machine using.Net Core.

For windows machines, we can use System.Management and WMI Query.

So is there any similar way to identify the hardware details (like RAM ,Processor,Monitor ,CAM etc) of Linux and Mac machines.

For windows, I'm using:

ManagementObjectSearcher searcher = 
    new ManagementObjectSearcher("select * from Win32_Processor");
jazb
  • 5,498
  • 6
  • 37
  • 44
Pascal Jackson
  • 129
  • 2
  • 3

2 Answers2

4

This is a piece of code to write bash linux commends in .net core:

using System;
using System.Diagnostics;
    public static class ShellHelper
    {
        public static string Bash(this string cmd)
        {
            var escapedArgs = cmd.Replace("\"", "\\\"");

            var process = new Process()
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = "/bin/bash",
                    Arguments = $"-c \"{escapedArgs}\"",
                    RedirectStandardOutput = true,
                    UseShellExecute = false,
                    CreateNoWindow = true,
                }
            };
            process.Start();
            string result = process.StandardOutput.ReadToEnd();
            process.WaitForExit();
            return result;
        }
    }

This is an extension method, you use it like this:

var output = "ps aux".Bash();

As for the commends, refer the Get Linux System and Hardware Details on the Command Line article on VITUX to help you out writing the commends, it lists most of the commends to collect system information on Linux.


For MAC:

System.Management.ManagementClass mc = default(System.Management.ManagementClass);
ManagementObject mo = default(ManagementObject);
mc = new ManagementClass("Win32_NetworkAdapterConfiguration");

ManagementObjectCollection moc = mc.GetInstances();
    foreach (var mo in moc) {
        if (mo.Item("IPEnabled") == true) {
              Adapter.Items.Add("MAC " + mo.Item("MacAddress").ToString());
         }
     }
Barr J
  • 10,636
  • 1
  • 28
  • 46
4

I have done a workaround to get hardware info as per Platform. For windows I have used old way of system Management classes, for Linux i have used different Bash commands to Get Processor Id, Model,model version,machine id. Following are some linux commands i am using

1. "LinuxModel": "cat /sys/class/dmi/id/product_name"
2. "LinuxModelVersion": "cat /sys/class/dmi/id/product_version"
3. "LinuxProcessorId": "dmidecode -t processor | grep -E ID | sed 's/.*: //' | head -n 1"
4. "LinuxFirmwareVersion": "cat /sys/class/dmi/id/bios_version",
5. "LinuxMachineId": "cat /var/lib/dbus/machine-id"

Waiting for some support in the .net core framework soon

My gihub post address is https://github.com/dotnet/corefx/issues/22660

I have also used similar extension method with a bit optimized code for bash command

public static string Bash(this string cmd)
        {
            string result = String.Empty;

            try
            {
                var escapedArgs = cmd.Replace("\"", "\\\"");

                using (Process process = new Process())
                {
                    process.StartInfo = new ProcessStartInfo
                    {
                        FileName = "/bin/bash",
                        Arguments = $"-c \"{escapedArgs}\"",
                        RedirectStandardOutput = true,
                        UseShellExecute = false,
                        CreateNoWindow = true,
                    };

                    process.Start();
                    result = process.StandardOutput.ReadToEnd();
                    process.WaitForExit(1500);
                    process.Kill();
                };
            }
            catch (Exception ex)
            {
                //Logger.ErrorFormat(ex.Message, ex);
            }
            return result;
        }
Kamran Shahid
  • 3,954
  • 5
  • 48
  • 93
  • 1
    You don't need bash to read text files. You can do this instead: `File.ReadAllText("/sys/class/dmi/id/product_name")` – Soonts Nov 25 '19 at 10:13
  • Thanks For update @Soonts. Doesn't know this prior. Is this mean all command listed above other then the one started from /sys should use cat while the one starts from /sys should use File.ReadAllText(....) – Kamran Shahid Nov 25 '19 at 10:14
  • This applies to commands which read a complete file with `cat`. I would leave this command in bash: `dmidecode -t processor | grep -E ID | sed 's/.*: //' | head -n 1` It's technically possible to port that one to C# as well, but bash is simpler and good enough for the job. – Soonts Nov 25 '19 at 11:02
  • Thanks @Soonts I will love to see equivalent C# code for that command. At the moment cat commands are working so i am letting that as it is. but surely move the code to C# if i got all command convertible. – Kamran Shahid Nov 25 '19 at 11:25
  • 1
    C# equivalent of `cat` is a single method call, `File.ReadAllText`. If you want to port that `dmidecode` command, it’s more involved: start the process (the binary location is usually `/usr/sbin/dmidecode`), then instead of `StandardOutput.ReadToEnd()` read output line by line, find the first line containing `"ID"`, then replace whatever `sed` is replacing (not sure what it is, I have limited experience with Linux) and return the value. – Soonts Nov 25 '19 at 11:51
  • Thanks Kosta. Will do it – Kamran Shahid Nov 25 '19 at 12:04