import subprocess
import re
# info = subprocess.Popen("systeminfo",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).stdout.read().decode("gbk")
#
# with open(r"D:/info.txt",'w') as f:
# f.write(info)
with open(r"D:/info.txt",'r') as f:
info = f.read()
info2 = re.sub("\s","",info)
# print(info2)
#主机名:PC201808110908
hostname = r"(?P<hostname>主机名\W\w{2}\d+)"
#产品ID:00426-OEM-8992662-00006
cp_id = r"(?P<cp_id>产品ID\W\d+-\w+-\d+-\d+)"
#初始安装日期:2018/8/11,9:15:36 \d/\d\W\d:\d:\d)
cz_date = r"(?P<cz_date>初始安装日期\W\d+\W\d+\W\d+\W\d+:\d+:\d+)"
#系统类型:x64-based
sys_type = r"(?P<sys_type>系统类型\W\w+\d+-[a-z]+)"
#处理器:安装了1个处理器。[01]:Intel64Family6Model58Stepping9GenuineIntel~2601Mhz
cpu_type = r"(?P<Intel>Intel\d+)(?P<Family>Family\d+)(?P<Model>Model\d+)(?P<Stepping>Stepping\d+)(?P<GenuineIntel>GenuineIntel\W+\d+\w{3})"
#物理内存总量:8,142MB
wl_mer = r"(?P<wl_mer>物理内存总量\W\d+\W\d+\w{2})"
#IP地址,MAC地址:IP地址[01]:192.168.1.3[02]:fe80::557c:e951:f509:995b
ip_add = r"(?P<ip_add>IP地址\[\d+\]\W\d+\W\d+\W\d+\W\d+)(?P<mac>\[\d+\]\W(?:\w+|\S\W){9})"
data_list = [hostname,cp_id,cz_date,sys_type,cpu_type,wl_mer,ip_add]
c = []
for i in data_list:
b = re.finditer(i,info2).__next__().groupdict()
c.append(b)
with open('./PC_MSG.txt','w') as f2:
f2.write(str(c))