python字典脚本
import subprocess
all_msg = {}
cmd_disk = 'lsblk'
disks = {}
ret_disk = subprocess.getoutput(cmd_disk)
for line in ret_disk.splitlines():
if 'disk' in line:
dev_name = line.strip().split()[0]
dev_size = line.strip().split()[3]
disks[dev_name] = dev_size
all_msg["DISK"] = disks
cmd_mem = 'free -m'
ret_mem = subprocess.getoutput(cmd_mem)
mem = {}
for line in ret_mem.splitlines():
if line.startswith('Mem'):
total = line.split()[1]
free = line.split()[3]
mem = {"MEM_TOTAL:":total,"MEM_FREE":free}
all_msg["MEM"] = mem
cmd_cpu = "grep 'model name' /proc/cpuinfo | uniq"
cmd_core ="grep 'core id' /proc/cpuinfo | sort -u | wc -l"
ret_cpu = subprocess.getoutput(cmd_cpu)
ret_core = subprocess.getoutput(cmd_core)
cpu_core = {}
core_num = ret_core
for line in ret_cpu.splitlines():
cpu_name = ret_cpu.split(": ")[1]
cpu_core = {"CPU_NAME:":cpu_name,"CPU_CORE:":core_num}
all_msg["CPU"] = cpu_core
ips = {}
cmd_ip = 'ip a'
ret_ip = subprocess.getoutput(cmd_ip)
for line in ret_ip.splitlines():
if 'inet ' in line and '127.0.0.1/8' not in line:
_,ip,*_,name = line.split()
ips[name] = ip
all_msg["IP"] = ips
for k,v in all_msg.items():
print(k)
for a,b in v.items():
print(a,b)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210317140045965.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0FuTmFuMTk5Nw==,size_16,color_FFFFFF,t_70)