// dmidecode -t 4 | grep ID | tail -1 // CPUID // 系統 // dmidecode -s system-serial-number // 檢視系統序列號 // dmidecode -s system-uuid // 檢視系統UUID // dmidecode -s system-product-name //檢視伺服器系統型號 // dmidecode -s processor-manufacturer | tail -1 // 處理器廠家 // 主機板 // dmidecode -s baseboard-product-name // 主機板型號 // dmidecode -s baseboard-serial-number // 主機板序列號 // dmidecode -s baseboard-manufacturer // 主機板廠家
實際專案當中,我獲取了CPUID、系統序列號、系統UUID、系統型號、處理器廠家,之所有獲取這麼多資訊標識機器,是考慮到有些資訊在某些系統可能為空,而且CPUID也不唯一了,所以就多獲取些。
使用掛載宿主機目錄方式
大體思路是docker 支援通過-e來傳遞引數到容器內部程式,就像安裝docker-mysql那樣密碼可以通過引數傳遞一樣
確保宿主機能執行dmidecode命令(必須)
將宿主機的如下兩個目錄掛載到容器中
// dmidecode程式的目錄,如果不掛載那麼容器中識別不了dmidecode命令 /usr/sbin/dmidecode或者/sbin/dmidecode // dmidecode呼叫時會使用到mem這個檔案,如果不掛載會找不到檔案 /dev/mem
在容器啟動時增加 --privileged = true引數,讓容器獲得近似於宿主機root的許可權
思路:在docker容器內安裝ssh,sshpass服務,通過ssh連線到宿主機執行命令,獲 取宿主機資訊(必須知道宿主機Ip和密碼)
步驟:
1 /// <summary> 2 /// 註冊幫助類 3 /// </summary> 4 public class RegisterHelper 5 { 6 // 機器指紋字串 7 private static string m_FingerPrintString = string.Empty; 8 9 /// <summary> 10 /// Get a string Unique Identification code of a computer 11 /// </summary> 12 /// <returns></returns> 13 public static string StringValue(string mac) 14 { 15 if (string.IsNullOrEmpty(m_FingerPrintString)) 16 { 17 m_FingerPrintString = "MAC >> " + mac + "\nCPU >> " + GetCpuId() + "\nBIOS >> " + GetBiosId() + "\nBASE >> " + GetBaseId() 18 + "\nDISK >> " + GetDiskId() + "\nVIDEO >> " + GetVideoId(); 19 } 20 return m_FingerPrintString; 21 } 22 23 /// <summary> 24 /// First enabled network card ID 25 /// </summary> 26 /// <returns></returns> 27 public static string GetMacId() 28 { 29 return Identifier("Win32_NetworkAdapterConfiguration", "MACAddress", "IPEnabled"); 30 } 31 32 /// <summary> 33 /// Get the cpuID 34 /// </summary> 35 /// <returns></returns> 36 private static string GetCpuId() 37 { 38 //Uses first CPU identifier available in order of preference 39 //Don't get all identifiers, as it is very time consuming 40 string retVal = Identifier("Win32_Processor", "UniqueId"); 41 if (string.IsNullOrEmpty(retVal)) //If no UniqueID, use ProcessorID 42 { 43 retVal = Identifier("Win32_Processor", "ProcessorId"); 44 if (string.IsNullOrEmpty(retVal)) //If no ProcessorId, use Name 45 { 46 retVal = Identifier("Win32_Processor", "Name"); 47 if (string.IsNullOrEmpty(retVal)) //If no Name, use Manufacturer 48 { 49 retVal = Identifier("Win32_Processor", "Manufacturer"); 50 } 51 //Add clock speed for extra security 52 retVal += Identifier("Win32_Processor", "MaxClockSpeed"); 53 } 54 } 55 return retVal; 56 } 57 58 /// <summary> 59 /// BIOS Identifier 60 /// </summary> 61 /// <returns></returns> 62 private static string GetBiosId() 63 { 64 return Identifier("Win32_BIOS", "Manufacturer") + " | " + Identifier("Win32_BIOS", "SMBIOSBIOSVersion") 65 + " | " + Identifier("Win32_BIOS", "IdentificationCode") + " | " + Identifier("Win32_BIOS", "SerialNumber") 66 + " | " + Identifier("Win32_BIOS", "ReleaseDate") + " | " + Identifier("Win32_BIOS", "Version") 67 + " | " + Identifier("Win32_BIOS", "Name"); 68 } 69 70 /// <summary> 71 /// Main physical hard drive ID 72 /// </summary> 73 /// <returns></returns> 74 private static string GetDiskId() 75 { 76 return Identifier("Win32_DiskDrive", "Model") + " | " + Identifier("Win32_DiskDrive", "SerialNumber") 77 + " | " + Identifier("Win32_DiskDrive", "Signature") + " | " + Identifier("Win32_DiskDrive", "TotalHeads"); 78 } 79 80 /// <summary> 81 /// Motherboard ID 82 /// </summary> 83 /// <returns></returns> 84 private static string GetBaseId() 85 { 86 return Identifier("Win32_BaseBoard", "Model") + " | " + Identifier("Win32_BaseBoard", "Manufacturer") 87 + " | " + Identifier("Win32_BaseBoard", "Name") + " | " + Identifier("Win32_BaseBoard", "SerialNumber") 88 + " | " + Identifier("Win32_BaseBoard", "SKU") + " | " + Identifier("Win32_BaseBoard", "Product"); 89 } 90 91 /// <summary> 92 /// Primary video controller ID 93 /// </summary> 94 /// <returns></returns> 95 private static string GetVideoId() 96 { 97 return Identifier("Win32_VideoController", "Name") + " | " + Identifier("Win32_VideoController", "AdapterRAM"); 98 } 99 100 /// <summary> 101 /// Return a hardware identifier 102 /// </summary> 103 /// <param name="wmiClass"></param> 104 /// <param name="wmiProperty"></param> 105 /// <returns></returns> 106 private static string Identifier(string wmiClass, string wmiProperty) 107 { 108 string result = string.Empty; 109 System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass); 110 System.Management.ManagementObjectCollection moc = mc.GetInstances(); 111 foreach (System.Management.ManagementObject mo in moc) 112 { 113 //Only get the first one 114 if (string.IsNullOrEmpty(result)) 115 { 116 try 117 { 118 result = mo[wmiProperty]?.ToString(); 119 break; 120 } 121 catch(Exception e) 122 { 123 LogSingleton.CreateInstance().Error(e, "Window獲取硬體資訊失敗"); 124 } 125 } 126 } 127 return result; 128 } 129 130 /// <summary> 131 /// Return a hardware identifier 132 /// </summary> 133 /// <param name="wmiClass"></param> 134 /// <param name="wmiProperty"></param> 135 /// <param name="wmiMustBeTrue"></param> 136 /// <returns></returns> 137 private static string Identifier(string wmiClass, string wmiProperty, string wmiMustBeTrue) 138 { 139 string result = string.Empty; 140 System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass); 141 System.Management.ManagementObjectCollection moc = mc.GetInstances(); 142 foreach (System.Management.ManagementObject mo in moc) 143 { 144 if (mo[wmiMustBeTrue].ToString() == "True") 145 { 146 //Only get the first one 147 if (string.IsNullOrEmpty(result)) 148 { 149 try 150 { 151 result = mo[wmiProperty]?.ToString(); 152 break; 153 } 154 catch(Exception e) 155 { 156 LogSingleton.CreateInstance().Error(e,"Window獲取硬體資訊失敗"); 157 } 158 } 159 } 160 } 161 return result; 162 } 163 }
1 public class LinuxHelper 2 { 3 4 // sudo dmidecode -t 4 | grep ID | tail -1 // CPUID 5 6 // 系統 7 // sudo dmidecode -s system-serial-number // 檢視系統序列號 8 // sudo dmidecode -s system-uuid // 檢視系統UUID 9 // sudo dmidecode -s system-product-name // 檢視伺服器系統型號 10 // sudo dmidecode -s processor-manufacturer | tail -1 // 處理器廠家 11 12 // 主機板 13 // sudo dmidecode -s baseboard-product-name // 主機板型號 14 // sudo dmidecode -s baseboard-serial-number // 主機板序列號 15 // sudo dmidecode -s baseboard-manufacturer // 主機板廠家 16 17 /// <summary> 18 /// Get a string Unique Identification code of a computer 19 /// </summary> 20 /// <returns></returns> 21 public static string StringValue() 22 { 23 string cpuID = GetCpuId(); 24 string serialNumber = GetSerialNumber(); 25 string productName = GetProductName(); 26 string processorManufacturer = GetProcessorManufacturer(); 27 if (string.IsNullOrWhiteSpace(cpuID) && string.IsNullOrWhiteSpace(serialNumber) && string.IsNullOrWhiteSpace(productName) && string.IsNullOrWhiteSpace(processorManufacturer)) 28 { 29 return string.Empty; 30 } 31 return "CPU >> " + cpuID + "\nSerialNumber >> " + serialNumber + "\nProductName >> " + productName + "\nProcessorManufacturer >> " + processorManufacturer; 32 } 33 34 35 /// <summary> 36 /// Get the cpuID 37 /// </summary> 38 /// <returns></returns> 39 private static string GetCpuId() 40 { 41 return ProcessShell("dmidecode -t 4 | grep ID | tail -1"); 42 } 43 44 /// <summary> 45 /// SerialNumber 46 /// </summary> 47 /// <returns></returns> 48 private static string GetSerialNumber() 49 { 50 return ProcessShell("dmidecode -s system-serial-number"); 51 } 52 53 /// <summary> 54 /// product-name 55 /// </summary> 56 /// <returns></returns> 57 private static string GetProductName() 58 { 59 return ProcessShell("dmidecode -s system-product-name"); 60 } 61 62 /// <summary> 63 /// ProcessorManufacturer 64 /// </summary> 65 /// <returns></returns> 66 private static string GetProcessorManufacturer() 67 { 68 return ProcessShell("dmidecode -s processor-manufacturer | tail -1"); 69 } 70 71 /// <summary> 72 /// 執行Shell命令 73 /// </summary> 74 /// <param name="shellCmd"></param> 75 /// <returns></returns> 76 private static string ProcessShell(string shellCmd) 77 { 78 string result = string.Empty; 79 try 80 { 81 using Process process = new Process 82 { 83 StartInfo = new ProcessStartInfo("/bin/bash", "") 84 }; 85 process.StartInfo.RedirectStandardInput = true; 86 process.StartInfo.RedirectStandardOutput = true; 87 process.StartInfo.UseShellExecute = false; 88 process.Start(); 89 process.StandardInput.WriteLine(shellCmd); 90 process.StandardInput.Close(); 91 result = process.StandardOutput.ReadToEnd(); 92 process.WaitForExit(); 93 } 94 catch (Exception e) 95 { 96 LogSingleton.CreateInstance().Error(e, "Linux獲取硬體資訊失敗"); 97 } 98 return result; 99 }
參考資訊 :https://blog.csdn.net/qq_42910468/article/details/121387411
https://blog.csdn.net/weixin_42173451/article/details/123085460