系统相关
首页 > 系统相关> > Docker容器获取宿主机信息

Docker容器获取宿主机信息

作者:互联网

最近在做产品授权的东西,开始宿主机为Window,程序获取机器硬件信息相对简单些,后来部署时发现各种各样的的环境问题,所有后来改用dokcer部署,docker方式获取宿主机信息时花了些时间,特此记录一下

docker 获取宿主机的信息

     

    // 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 获取宿主机信息大体可以通过三种方式

一:通过环境变量由外部传入容器内

 大体思路是docker 支持通过-e来传递参数到容器内部程序,就像安装docker-mysql那样密码可以通过参数传递一样

 

二:使用挂载宿主机目录方式

         确保宿主机能执行dmidecode命令(必须)

        将宿主机的如下两个目录挂载到容器中

       

  // dmidecode程序的目录,如果不挂载那么容器中识别不了dmidecode命令

  /usr/sbin/dmidecode或者/sbin/dmidecode

  // dmidecode调用时会使用到mem这个文件,如果不挂载会找不到文件

   /dev/mem

   在容器启动时增加 --privileged = true参数,让容器获得近似于宿主机root的权限

三:在容器中使用ssh连接到主机

思路:在docker容器内安装ssh,sshpass服务,通过ssh连接到宿主机执行命令,获 取宿主机信息(必须知道宿主机Ip和密码)

步骤:

Window 获取设备信息帮助类

 

  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     }

标签:Window,dokcer,部署,获取方式,系统,主机,安装服务
来源: