Python批量处理aida64extreme收集到的硬件信息

   

因需收集部分驻外同事电脑的硬件信息(未连公司内网,无法使用桌管软件统一采集),采用aida64extreme收集后再通过python处理写入数据库。

   

   

.创建表 createtable.py

#!/usr/bin/python3

#coding:utf-8

import pymysql

db = pymysql.connect("192.168.xxxx.xxx", "root", "password",port=xxx,db="computers")

cursor = db.cursor()

#sql ='''drop table Diskreport'''

sql ='''create table computerreport(

ID INTEGER PRIMARY KEY  AUTO_INCREMENT,

Userid char(255),

Name char(255),

Computer char(255),

Displayer char(255),

Mainboard char(255),

CPU char(255),

Mem char(255),

Disk char(255),

VGA char(255),

OS char(255))'''

   

cursor.execute(sql)    

print("CREATE TABLE OK")

   

.数据清洗,写入数据库

#!/usr/bin/python3

#coding:utf-8

import os

import os.path

import subprocess

import pymysql

 

收集到的信息保存为txt文件,并命名为"工号姓名.txt"

items = os.listdir("/data/hardware/txt/")

copylists = []

for names in items:

  if names.endswith(".txt" ):

    copylists.append(names)

print(copylists)

print("文件总数:"+str(len(copylists)))

 

 

def load_data():

  try:

    db = pymysql.connect("192.168.xxxx.xxx", "root", "password",port=xxx,db="computers")

    cur=db.cursor()

     

    for file in copylists:

      useridname=os.path.splitext(os.path.basename(file))[0].split("-",-1)     

           #windows下运行aida64生成的txt文件在linux下乱码的处理 

      os.system("cat /data/hardware/txt/%s | iconv -f GBK -t UTF-8 -c >/data/hardware/txtUTF8/%s" %(file,file) )

       

      Userid = useridname[0]

      Name =  useridname[1]

      #python打印txt文件时,会带有b' \r\n'  如何把b' rn'去掉呢?只要在后面加上.decode().strip()即可

      Computer = subprocess.check_output("cat /data/hardware/txtUTF8/%s  |grep '计算机名称' |head -1|awk '{$1=\"\";print }'" %file, shell=True).decode().strip()

      Displayer= subprocess.check_output("cat /data/hardware/txtUTF8/%s  |grep '最大可用屏幕'|awk '{$1=\"\";print }'" %file, shell=True).decode().strip()

      Mainboard= subprocess.check_output("cat /data/hardware/txtUTF8/%s  |grep '主板芯片组'|tail -1|awk '{$1=\"\";print }'" %file, shell=True).decode().strip()

      CPU = subprocess.check_output("cat /data/hardware/txtUTF8/%s  |grep '处理器名称'|awk '{$1=\"\";print }'" %file, shell=True).decode().strip()

      Mem = subprocess.check_output("cat /data/hardware/txtUTF8/%s  |grep '总数'|head -1|awk '{$1=\"\";print }'" %file, shell=True).decode().strip()

      Disk = subprocess.check_output("cat /data/hardware/txtUTF8/%s  |grep '硬盘驱动器'|egrep -v 'USB'|awk '{$1=\"\";print }'" %file, shell=True).decode().strip()

      VGA = subprocess.check_output("cat /data/hardware/txtUTF8/%s  |grep '显示适配器'|head -1|awk '{$1=\"\";print }'" %file, shell=True).decode().strip()

      OS = subprocess.check_output("cat /data/hardware/txtUTF8/%s  |grep '操作系统名称'|awk '{$1=\"\";print }'" %file, shell=True).decode().strip()

       

      sql="""insert into computers.computerreport(Userid,Name,Computer,Displayer,Mainboard,CPU,Mem,Disk,VGA,OS) values('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}')""".format(Userid,Name,Computer,Displayer,Mainboard,CPU,Mem,Disk,VGA,OS)

      cur.execute(sql)

    cur.close()

    db.commit()

    db.close()

  except Exception as e :print(e)

load_data()

   

   

windows下运行aida64生成的txt文件在linux下乱码的处理

      os.system("cat /data/hardware/txt/%s | iconv -f GBK -t UTF-8 -c >/data/hardware/txtUTF8/%s" %(file,file) )

   

  

   

结果截图  

 

 

   

   

posted @ 2023-10-19 22:07  ST运维  阅读(152)  评论(0编辑  收藏  举报