实验目的: 1.通过Python实现对底层硬件信息的获取 (包括但不限于 CPU架构、CPU核数、内存信息、磁盘信息、 网络信息) 实验内容: 1.查看-CPU架构 2.查看-CPU核数 3.查看-内存信息 4.查看-磁盘信息 5.查看-网络信息
1)查看-CPU架构
#Python中,platform模块给提供了很多方法去获取操作系统的信息。
#encoding:utf-8
import platform
print(platform.platform()) #获取操作系统名称及版本号
print(platform.version()) #获取操作系统版本号
print(platform.architecture()) #获取操作系统的位数
print(platform.machine()) #计算机的网络名称
print(platform.processor()) #计算机处理器信息
print(platform.uname()) #包含上面所有的信息汇总
示例如下: kali Linux: ![在这里插入图片描述](https://img-blog.csdnimg.cn/6bc0b9495e9d423d8ca85e9bd09e1cf9.png) CentOS 7: ![在这里插入图片描述](https://img-blog.csdnimg.cn/5abaa96b731d4fff91b2c3efb86ea8b2.png) 输出结果: ![在这里插入图片描述](https://img-blog.csdnimg.cn/9ee4b6b0882d4d2883fb45200a37aeff.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/089117ef0c4b40d9a9794e7ffd708487.png)
2)查看-CPU核数
#encoding:utf-8
import multiprocessing
print(multiprocessing.cpu_count())
示例如下: kali Linux: ![在这里插入图片描述](https://img-blog.csdnimg.cn/5a575d18232d44e4a24ea5034a070845.png) CentOS 7: ![在这里插入图片描述](https://img-blog.csdnimg.cn/b46a14a9bb5647a393e99b53aecc0c10.png) 输出结果: ![在这里插入图片描述](https://img-blog.csdnimg.cn/2d7880a34e2247e68d31fe6fcd03fe5b.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/b4623ebdff694e2c8d4f8b869f332c80.png)
3)查看-内存信息
#encoding:utf-8
mem_info = ""
f_mem_info =open("/proc/meminfo")
try:
for line in f_mem_info:
if(line.find("MenTotal")==0):
men_info += line.strip()+","
elif(line.find("SwapTotal") == 0 ):
mem_info +=line.strip()
break
print("mem_info----{:s}".format(mem_info))
finally:
f_mem_info.close()
示例如下: kali Linux: ![在这里插入图片描述](https://img-blog.csdnimg.cn/d9870c1bfb5c467f8f11ab5a4be0529d.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAWC5JTw==,size_18,color_FFFFFF,t_70,g_se,x_16) CentOS 7: ![在这里插入图片描述](https://img-blog.csdnimg.cn/0df6af8ca3894508aacea9ee342ef400.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAWC5JTw==,size_20,color_FFFFFF,t_70,g_se,x_16) 输出结果: ![在这里插入图片描述](https://img-blog.csdnimg.cn/3f5d28d7c7d84c0ab17216d35cb4bfb4.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/6809c7e01a194ad18fcdd2a8a3b3bbd5.png)
4)查看-磁盘信息
#磁盘信息可以通过shell命令df -h查询
#encoding:utf-8
import subprocess
pipe = subprocess.Popen("df -h",stdout = subprocess.PIPE, shell=True)
disc_info = pipe.stdout.read()
print(disc_info)
示例如下: kali Linux: ![在这里插入图片描述](https://img-blog.csdnimg.cn/6765db2b0a6a4bceaefaf011e7282f73.png) CentOS 7: ![在这里插入图片描述](https://img-blog.csdnimg.cn/6c614a5a0a4744a6bbb1819c4ea1b319.png) 输出结果: ![在这里插入图片描述](https://img-blog.csdnimg.cn/0a15f1e27fb64ad9aa02608f4f167a53.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/57cd0c5ffc5743d9b3f0a74203e088e5.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAWC5JTw==,size_20,color_FFFFFF,t_70,g_se,x_16)
5)查看-网络信息
需要提前安装以下netifaces第三方库。
#使用netifaces模块可以获取本机IP、网关等信息。
#encoding:utf-8
def GetNetworkIP(): #获取本地网卡IP地址
import netifaces
#routingGateway = netifaces.gateways()['default'][netifaces.AF_INET][0] #网关
routingNicName = netifaces.gateways()['default'][netifaces.AF_INET][1] #网络适配器信息
for interface in netifaces.interfaces():
if interface == routingNicName:
#print(netifaces.ifaddresses(interface))
try:
routingIPAddr = netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['addr'] #获取IP
except KeyError:
pass
#print("Routing IP Address:%s"% routingIPAddr)
return routingIPAddr
if __name__ =="__main__":
try:
print("Routing IP Address:",GetNetworkIP())
except:
print("Unable to get the address,there may not be installed netifaces module! command: pip install netifaces")
示例如下: kali Linux : ![在这里插入图片描述](https://img-blog.csdnimg.cn/ee194b6241e04d7a97d0d97d902b6867.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAWC5JTw==,size_20,color_FFFFFF,t_70,g_se,x_16)
输出结果: ![在这里插入图片描述](https://img-blog.csdnimg.cn/91d24305e93c4df58b82c8594123e82a.png)
|