获取linux系统信息的一个python脚本

简介:

其实获取系统信息,如果使用python的psutil的模块,更加的简洁好用。自己写的这个脚本算是一个python练习。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python2.7
# -*- coding:utf-8 -*-
import  os
import  subprocess
import  re
import  hashlib
#对字典取子集
def  sub_dict(form_dict, sub_keys, default = None ):
     return  dict ([(k, form_dict.get(k.strip(), default))  for  in  sub_keys.split( ',' )])
#读取cpuinfo信息
# dmidecode -t 4
def  read_cpuinfo():
     cpu_stat  =  []
     with  open ( '/proc/cpuinfo' 'r' ) as f:
         data  =  f.read()
         for  line  in  data.split( '\n\n' ):
             cpu_stat.append(line)
     return  cpu_stat[ - 2 ]
#读取fdisk信息
def  read_fdisk():
     =  subprocess.Popen( 'fdisk -l' , stdout = subprocess.PIPE, shell = True )
     out  =  p.communicate()[ 0 ]
     info  =  []
     for  in  out.split( '\n\n' ):
         for  in  i.splitlines():
             if  x:
                 info.append(x)
     return  info
#读取dmidecode信息
def  read_dmidecode():
     =  subprocess.Popen( 'dmidecode -t 1' , stdout = subprocess.PIPE, shell = True )
     return  p.communicate()[ 0 ]
#读取ifconfig信息
def  read_ifconfig():
     =  subprocess.Popen( 'ifconfig' , stdout = subprocess.PIPE, shell = True )
     return  p.communicate()[ 0 ]
#返回cpu信息:CPU型号、颗数、核数
def  get_cpuinfo(data):
     cpu_info  =  {}
     for  in  data.splitlines():
         k, v  =  [x.strip()  for  in  i.split( ':' )]
         cpu_info[k]  =  v
       
     cpu_info[ 'physical id' =  str ( int (cpu_info.get( 'physical id' ))  +  1 )
     return  sub_dict(cpu_info,  'model name,physical id,cpu cores' )
#返回每块硬盘大小
def  get_diskinfo(data):
     disk_info  =  {}
     m_disk  =  re. compile (r '^Disk\s/dev' )
               
     for  in  data:
         if  m_disk.match(i):
             =  i.split( ',' )[ 0 ]
             k, v  =  [x  for  in  i.split( ':' )]
             disk_info[k]  =  v
     return  disk_info
#返回硬件信息:品牌、型号
def  get_dmiinfo(data):
     dmi_info  =  {}
     line_in  =  False
     for  line  in  data.splitlines():
         if  line.startswith( 'System Information' ):
             line_in  =  True
             continue
         if  line.startswith( '\t' and  line_in:
             k, v  =  [i.strip()  for  in  line.split( ':' )]
             dmi_info[k]  =  v
         else :
             line_in  =  False
     return  sub_dict(dmi_info,  'Manufacturer,Product Name,Serial Number' )
#返回网卡及ip信息:网卡、IP址、MAC地址
def  get_ipinfo(data):
     data  =  (i  for  in  data.split( '\n\n' if  and  not  i.startswith( 'lo' ))
     ip_info  =  []
     ifname  =  re. compile (r '(eth[\d:]*|wlan[\d:]*)' )
     ipaddr  =  re. compile (r '(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})){3}' )
     macaddr  =  re. compile (r '[A-F0-9a-f:]{17}' )
     for  in  data:
         =  {}
         if  ifname.match(i):
             device  =  ifname.match(i).group()
             x[ 'Adapter' =  device
         if  macaddr.search(i):
             mac  =  macaddr.search(i).group()
             x[ 'MAC' =  mac
         if  ipaddr.search(i):
             ip  =  ipaddr.search(i).group()
             x[ 'IP' =  ip
         else :
             x[ 'IP' =  None
         ip_info.append(x)
     return  ip_info
#返回内存及swap大小
def  get_meminfo():
     mem_info  =  {}
     with  open ( '/proc/meminfo' 'r' ) as f:
         data  =  f.readlines()
         for  in  data:
             k, v  =  [x.strip()  for  in  i.split( ':' )]
             mem_info[k]  =  int (v.split()[ 0 ])
     return  sub_dict(mem_info,  'MemTotal,SwapTotal' )
#返回操作信息
def  get_osinfo():
     os_info  =  {}
     =  os.uname()
     os_info[ 'os_type' =  i[ 0 ]
     os_info[ 'node_name' =  i[ 1 ]
     os_info[ 'kernel' =  i[ 2 ]
     return  os_info
#唯一标识符
def  get_indentity(data):
     match_serial  =  re. compile (r "Serial Number: .*" , re.DOTALL)
     match_uuid  =  re. compile (r "UUID: .*" , re.DOTALL)
     if  match_serial.search(data):
         serial  =  match_serial.search(data).group()
     if  match_uuid.search(data):
         uuid  =  match_uuid.search(data).group()
     if  serial:
         serial_md5  =  hashlib.md5(serial).hexdigest()
         return  serial_md5
     elif  uuid:
         uuid_md5  =  hashlib.md5(uuid).hexdigest()
         return  uuid_md5
if  __name__  = =  "__main__" :
     ipinfo  =  get_ipinfo(read_ifconfig())
     dmiinfo  =  get_dmiinfo(read_dmidecode())
     cpuinfo  =  get_cpuinfo(read_cpuinfo())
     diskinfo  =  get_diskinfo(read_fdisk())
     meminfo  =  get_meminfo()
     osinfo  =  get_osinfo()
     identity  =  get_indentity(read_dmidecode())


本文转自 li_qinshan 51CTO博客,原文链接:http://blog.51cto.com/share/1269078
相关文章
|
19天前
|
Ubuntu Linux
Linux(21) Linux自定义开机启动脚本或命令
Linux(21) Linux自定义开机启动脚本或命令
33 3
|
12天前
|
存储 Shell Linux
Linux Bash 脚本中的 IFS 是什么?
【4月更文挑战第25天】
19 0
Linux Bash 脚本中的 IFS 是什么?
|
1天前
|
编解码 Ubuntu Linux
|
1天前
|
Python Perl
LabVIEW调用Perl和Python脚本
LabVIEW调用Perl和Python脚本
|
1天前
|
Oracle Java 关系型数据库
【服务器】python通过JDBC连接到位于Linux远程服务器上的Oracle数据库
【服务器】python通过JDBC连接到位于Linux远程服务器上的Oracle数据库
14 6
|
3天前
|
Linux Perl
linux如何查看系统信息
linux如何查看系统信息
|
3天前
|
缓存 人工智能 算法
编写高效的Python脚本:性能优化的策略与技巧
编写高效的Python脚本需要综合考虑多个方面,包括代码结构、数据结构和算法选择等。本文将探讨在Python编程中提高脚本性能的方法,包括优化数据结构、选择合适的算法、使用Python内置函数以及通过并行和异步编程提升效率。这些技巧旨在帮助开发者在不同应用场景中编写出高性能的Python代码。
|
5天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
23 5
|
6天前
|
分布式计算 大数据 Hadoop
【经验分享】用Linux脚本管理虚拟机下的大数据服务
【经验分享】用Linux脚本管理虚拟机下的大数据服务
15 1
|
6天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
14 3