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

牧云@^-^@ 提交于 2021-01-27 04:48:29

问题


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");

回答1:


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;
        }



回答2:


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());
         }
     }


来源:https://stackoverflow.com/questions/53512455/how-to-identify-the-hardware-details-of-a-linux-mac-machine-using-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!