c# linux环境下获取硬件信息 MAC地址 硬盘序列号 CPU序列号

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

public class LinuxHelper 
{
    public string GetCpu()
    {
        var result = ExecuteCommand("sudo dmidecode -t Processor | grep -m1 'ID:' | awk -F': ' '{print $2}' | grep -v '^$'");
        var lines = result.Split(Environment.NewLine);

        //返回第一行
        foreach (var line in lines)
        {
            if (string.IsNullOrWhiteSpace(line.Trim()))
            {
                continue;
            }
            return line.Trim();
        }

        throw new Exception("cpu not found");
    }

    public string GetDiskSN()
    {
        return GetDiskSNList().First();
    }

    public List<string> GetDiskSNList()
    {
        //lsblk -do name,model,serial
        //-d:仅显示物理设备(不显示分区)
        //-n:不输出表头信息
        //- o SERIAL:仅显示序列号列
        var netCard = ExecuteCommand("lsblk -dno SERIAL | grep -v '^$'");
        var lines = netCard.Split(Environment.NewLine);

        var result = new List<string>();
        foreach (var line in lines)
        {
            //移除空字符串、移除serial头
            if (string.IsNullOrWhiteSpace(line.Trim()))
            {
                continue;
            }
            result.Add(line.Trim());
        }

        if(result.Count == 0)
        {
            throw new Exception("disk sn not found");
        }

        return result;
    }

    public string GetMac()
    {
        var netCard = GetNetCard();
        if (netCard == null || netCard.Count == 0)
        {
            throw new Exception("net card not found");
        }

        return GetMacByNetCard(netCard.First());
    }

    public List<string> GetMacList()
    {
        var result = new List<string>();

        var netCard = GetNetCard();
        if (netCard == null || netCard.Count == 0)
        {
            throw new Exception("net card not found");
        }

        foreach (var item in netCard)
        {
            var mac = GetMacByNetCard(item);
            result.Add(mac);
        }

        if (result.Count == 0)
        {
            throw new Exception("mac not found");
        }

        return result;
    }


    public string GetMacByNetCard(string netCard)
    {
        var result = ExecuteCommand($"cat /sys/class/net/{netCard}/address  | grep -v '^$'");
        return result.Trim();
    }

    /// <summary>
    /// 获取网卡名称
    /// </summary>
    public List<string> GetNetCard()
    {
        var result = new List<string>();

        var netCard = ExecuteCommand("ls /sys/class/net/ | grep ^eth | sort -un  | grep -v '^$'");

        if (string.IsNullOrWhiteSpace(netCard))
        {
            netCard = ExecuteCommand("ls /sys/class/net/ | grep ^en | sort -un  | grep -v '^$'");
        }

        if (string.IsNullOrWhiteSpace(netCard))
        {
            return result;
        }

        var lines = netCard.Split(Environment.NewLine);
        foreach (var line in lines)
        {
            if (string.IsNullOrWhiteSpace(line.Trim()))
            {
                continue;
            }
            result.Add(line.Trim());
        }

        if (result.Count == 0)
        {
            throw new Exception("net card not found");
        }

        return result;
    }

    private static string ExecuteCommand(string command)
    {
        var escapedArgs = command.Replace("\"", "\\\"");
        var process = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "/bin/bash", // 使用 bash
                                        // FileName = "/bin/sh", // 使用 sh
                Arguments = $"-c \"{escapedArgs}\"",
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true
            }
        };

        process.Start();
        process.WaitForExit();

        var message = process.StandardOutput.ReadToEnd();

        return message;
    }
}
posted @ 2025-07-11 10:16  Hey,Coder!  阅读(43)  评论(0)    收藏  举报