// 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和密码)
步骤:
/// <summary> /// 注册帮助类 /// </summary> public class RegisterHelper { // 机器指纹字符串 private static string m_FingerPrintString = string.Empty; /// <summary> /// Get a string Unique Identification code of a computer /// </summary> /// <returns></returns> public static string StringValue(string mac) { if (string.IsNullOrEmpty(m_FingerPrintString)) { m_FingerPrintString = "MAC >> " + mac + "\nCPU >> " + GetCpuId() + "\nBIOS >> " + GetBiosId() + "\nBASE >> " + GetBaseId() + "\nDISK >> " + GetDiskId() + "\nVIDEO >> " + GetVideoId(); } return m_FingerPrintString; } /// <summary> /// First enabled network card ID /// </summary> /// <returns></returns> public static string GetMacId() { return Identifier("Win32_NetworkAdapterConfiguration", "MACAddress", "IPEnabled"); } /// <summary> /// Get the cpuID /// </summary> /// <returns></returns> private static string GetCpuId() { //Uses first CPU identifier available in order of preference //Don't get all identifiers, as it is very time consuming string retVal = Identifier("Win32_Processor", "UniqueId"); if (string.IsNullOrEmpty(retVal)) //If no UniqueID, use ProcessorID { retVal = Identifier("Win32_Processor", "ProcessorId"); if (string.IsNullOrEmpty(retVal)) //If no ProcessorId, use Name { retVal = Identifier("Win32_Processor", "Name"); if (string.IsNullOrEmpty(retVal)) //If no Name, use Manufacturer { retVal = Identifier("Win32_Processor", "Manufacturer"); } //Add clock speed for extra security retVal += Identifier("Win32_Processor", "MaxClockSpeed"); } } return retVal; } /// <summary> /// BIOS Identifier /// </summary> /// <returns></returns> private static string GetBiosId() { return Identifier("Win32_BIOS", "Manufacturer") + " | " + Identifier("Win32_BIOS", "SMBIOSBIOSVersion") + " | " + Identifier("Win32_BIOS", "IdentificationCode") + " | " + Identifier("Win32_BIOS", "SerialNumber") + " | " + Identifier("Win32_BIOS", "ReleaseDate") + " | " + Identifier("Win32_BIOS", "Version") + " | " + Identifier("Win32_BIOS", "Name"); } /// <summary> /// Main physical hard drive ID /// </summary> /// <returns></returns> private static string GetDiskId() { return Identifier("Win32_DiskDrive", "Model") + " | " + Identifier("Win32_DiskDrive", "SerialNumber") + " | " + Identifier("Win32_DiskDrive", "Signature") + " | " + Identifier("Win32_DiskDrive", "TotalHeads"); } /// <summary> /// Motherboard ID /// </summary> /// <returns></returns> private static string GetBaseId() { return Identifier("Win32_BaseBoard", "Model") + " | " + Identifier("Win32_BaseBoard", "Manufacturer") + " | " + Identifier("Win32_BaseBoard", "Name") + " | " + Identifier("Win32_BaseBoard", "SerialNumber") + " | " + Identifier("Win32_BaseBoard", "SKU") + " | " + Identifier("Win32_BaseBoard", "Product"); } /// <summary> /// Primary video controller ID /// </summary> /// <returns></returns> private static string GetVideoId() { return Identifier("Win32_VideoController", "Name") + " | " + Identifier("Win32_VideoController", "AdapterRAM"); } /// <summary> /// Return a hardware identifier /// </summary> /// <param name="wmiClass"></param> /// <param name="wmiProperty"></param> /// <returns></returns> private static string Identifier(string wmiClass, string wmiProperty) { string result = string.Empty; System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass); System.Management.ManagementObjectCollection moc = mc.GetInstances(); foreach (System.Management.ManagementObject mo in moc) { //Only get the first one if (string.IsNullOrEmpty(result)) { try { result = mo[wmiProperty]?.ToString(); break; } catch(Exception e) { LogSingleton.CreateInstance().Error(e, "Window获取硬件信息失败"); } } } return result; } /// <summary> /// Return a hardware identifier /// </summary> /// <param name="wmiClass"></param> /// <param name="wmiProperty"></param> /// <param name="wmiMustBeTrue"></param> /// <returns></returns> private static string Identifier(string wmiClass, string wmiProperty, string wmiMustBeTrue) { string result = string.Empty; System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass); System.Management.ManagementObjectCollection moc = mc.GetInstances(); foreach (System.Management.ManagementObject mo in moc) { if (mo[wmiMustBeTrue].ToString() == "True") { //Only get the first one if (string.IsNullOrEmpty(result)) { try { result = mo[wmiProperty]?.ToString(); break; } catch(Exception e) { LogSingleton.CreateInstance().Error(e,"Window获取硬件信息失败"); } } } } return result; } }
标签:Window,dokcer,部署,获取方式,系统,主机,安装服务 来源:
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。