获取Linux主机的CPU、内存、主板、BIOS的信息(Centos)
#!/usr/bin/env python
#coding:utf-8
import subprocess
import re def Cmd_Exec(cmd):
'''
执行获取信息命令
:param cmd:
:return:
'''
result = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
return result.stdout.read()
Cmd ={
'cpu':'cat /proc/cpuinfo',
'mem':'cat /proc/meminfo',
'bios':'dmidecode -t 0',
'motherbord':'dmidecode -t 1'
} host_info ={} #主机信息表 for k,v in Cmd.items():
if k == 'cpu':
rst_cpu = Cmd_Exec(v)
cpu_info = {
'CPU生产商':re.search('vendor_id\s*:.*', rst_cpu).group().split(':')[1], # 提供者
'CPU类型': re.search('model name\s*:.*', rst_cpu).group().split(':')[1], # 型号
'CPU主频': re.search('cpu MHz\s*:.*', rst_cpu).group().split(':')[1], # 主频
'CPU缓存': re.search('cache size\s*:.*', rst_cpu).group().split(':')[1], # cpu的缓存
'物理CPU个数': re.search('physical id\s*:.*', rst_cpu).group().split(':')[1], # cpu物理个数
'CPU盒数': re.search('cpu cores\s*:.*', rst_cpu).group().split(':')[1] # cpu盒数
}
host_info[k] = cpu_info
elif k == 'mem':
rst_mem = Cmd_Exec(v)
mem_info = {
'MemTotal': re.search('MemTotal:\s*.*', rst_mem).group().split(':')[1].strip(), # 机器总内存大小
'MemFree': re.search('MemFree:\s*.*', rst_mem).group().split(':')[1].strip(), # 机器当前空闲物理内存
#'MemAvailable': re.search('MemAvailable:\s*.*', rst_mem).group().split(':')[1].strip(), # 机器可用内存逻辑内存
'Buffers': re.search('Buffers:\s*.*', rst_mem).group().split(':')[1].strip(), # 内存当前的buffer值
'Cached': re.search('Cached:\s*.*', rst_mem).group().split(':')[1].strip(), # 内存当前的Cache值
'SwapTotal': re.search('SwapTotal:\s*.*', rst_mem).group().split(':')[1].strip(), # 交换分区的大小
'SwapFree': re.search('SwapFree:\s*.*', rst_mem).group().split(':')[1].strip(), # 空闲交换分区大小
}
host_info[k] = mem_info elif k == 'bios':
rst_bios = Cmd_Exec(v)
bios_info = {
re.search('\s*Vendor:.*', rst_bios).group(0).strip().split(':')[0]:
re.search('\s*Vendor:.*', rst_bios).group(0).strip().split(':')[0].strip(),
re.search('\s*Version:.*', rst_bios).group(0).strip().split(':')[0]:
re.search('\s*Version:.*', rst_bios).group(0).strip().split(':')[1].strip(),
re.search('\s*Release Date:.*', rst_bios).group(0).strip().split(':')[0]:
re.search('\s*Release Date:.*', rst_bios).group(0).strip().split(':')[1].strip()
}
host_info[k] = bios_info elif k == 'motherbord': rst_mb= Cmd_Exec(v)
motherboard_info = {
re.search('\s*Product Name:.*', rst_mb).group(0).strip().split(':')[0]:
re.search('\s*Product Name:.*', rst_mb).group(0).strip().split(':')[1].strip(),
re.search('\s*Version:.*', rst_mb).group(0).strip().split(':')[0]:
re.search('\s*Version:.*', rst_mb).group(0).strip().split(':')[1].strip(),
re.search('\s*Serial Number:.*', rst_mb).group(0).strip().split(':')[0]:
re.search('\s*Serial Number:.*', rst_mb).group(0).strip().split(':')[1].strip()
}
host_info[k] = motherboard_info
else:
print '不匹配' for k,v in host_info.items():
print '============='+k+'============='
for k1,v1 in v.items():
print k1 +": "+v1
执行结果如下:
=============mem=============
MemTotal: 1876632 kB
Cached: 455472 kB
SwapFree: 2047996 kB
SwapTotal: 2047996 kB
MemFree: 776972 kB
Buffers: 876 kB
=============bios=============
Version: 6.00
Vendor: Vendor
Release Date: 07/31/2013
=============motherboard=============
Serial Number: VMware-56 4d fc a3 c4 42 1b 81-67 f8 f1 58 53 8a 0d b3
Version: None
Product Name: VMware Virtual Platform
=============cpu=============
CPU缓存: 8192 KB
CPU类型: Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
CPU主频: 3408.000
CPU盒数: 2
CPU物理个数: 0
CPU生产商: GenuineIntel
获取Linux主机的CPU、内存、主板、BIOS的信息(Centos)的更多相关文章
- 用python实现批量获取Linux主机简要信息并保存到Excel中 unstable 1.1
#!/usr/bin/env python3 # -*- coding: utf-8 -*- #filename get_linux_info.py #获取Linux主机的信息 # titles=[' ...
- linux Java项目CPU内存占用高故障排查
linux Java项目CPU内存占用高故障排查 top -Hp 进程号 显示进程中每个线程信息,配合jstack定位java线程运行情况 # 线程详情 jstack 线程PID # 查看堆内存中的对 ...
- AIX/Linux/HP-UX查看CPU/内存/磁盘/存储命令
1.1 硬件环境验证方式 硬件环境主要包括CPU.内存.磁盘/存储.网络设备(如F5等).系统特有设备(如密押设备等)等,其中网络设备和系统特有设备由网络管理员或项目组提供为准,本节主要关注CP ...
- linux查看系统CPU,内存,硬盘使用情况
top查看CPU,内存使用情况 free查看硬盘使用情况
- PHP 获取linux服务器性能CPU、内存、硬盘、进程等使用率
数据库配置文件: conn.php <?php define("MONITORED_IP", "172.16.0.191"); //被监控的服务器IP地址 ...
- delphi 获取硬盘序列号、cpu号、bios号、网卡号
delphi 获取硬盘 序列号 function GetIdeNum: String; type TSrbIoControl = packed record HeaderLength : ULONG; ...
- python 在linux下通过top,和dh命令获得cpu,内存,以及硬盘信息
主要是通过os.popen读取命令输出实现的,os.popen启动新的进程,且将外部命令的输出作为文件类型对象返回.不能获得外部命令的返回值.既然是文件对象就可以直接用for in 来读取,代码如下: ...
- c#获取电脑运行状态(cpu,内存,网络,系统运行时间)
public class DeviceMonitor { static readonly PerformanceCounter cpuCounter = new PerformanceCounter( ...
- [bash] 获取linux主机名,检视内中是否有特定字符串
代码: #!/bin/bash hostname=$(hostname) #调用hostname命令获取主机名放入变量hostname中 #echo $hostname if [ `echo ${ho ...
随机推荐
- 在php中使用strace、gdb、tcpdump调试工具
[转] http://www.syyong.com/php/Using-strace-GDB-and-tcpdump-debugging-tools-in-PHP.html 在php中我们最常使用调试 ...
- 使用命令行+ideal 工具实现本地代码项目提交
在 OSChina 上建立一个私用的项目 mkdir test cd test git init touch README.md git add README.md git commit -m &qu ...
- System.getProperty()方法大全
System.out.println("当前程序所在目录:" + System.getProperty("user.dir")); // 当前程序所在目录 Sy ...
- shell中${ } 的一些特异功能
假设我们定义了一个变量为: file=/dir1/dir2/dir3/my.file.txt 我们可以用 ${ } 分别替换获得不同的值: ${file#*/}:拿掉第一条 / 及其左边的字符串:di ...
- 通过LDAP管理VSFTP帐户
yum install -y openldap openldap-servers openldap-clients pam_ldap nss-pam-ldapd vsftpd slappasswd # ...
- javascript keycode大全
keycode 8 = BackSpace BackSpacekeycode 9 = Tab Tabkeycode 12 = Clearkeycode 13 = Enterkeyc ...
- 新版startssl 免费SSL证书申请 (实测 笔记 https http2 必要条件)
简单说明: 目前多个大型网站都实现全站HTTPS,而SSL证书是实现HTTPS的必要条件之一. StartSSL是StartCom公司旗下的.提供免费SSL证书服务并且被主流浏览器支持的免费SSL.包 ...
- ajax post提交form表单 报400错误 解决方法
昨天晚上做项目遇到了一个奇怪的问题,我用ajax提交一个form表单,后台Java方法用的是一个实体接,但是他根本不进方法体中,直接给我一个400的错误,一开始我以为是我路径的问题(尴尬),结果直接访 ...
- Xcode 调试技巧
一 NSLog调试 官方文档:Logs an error message to the Apple System Log facility. 即NSLog不是作为普通的debug log的,而是err ...
- iOS单例详解
单例:整个程序只创建一次,全局共用. 单例的创建 // SharedPerson.h 文件中 + (instancetype)share; // SharedPerson.m 文件中 static S ...