获取MAC OS苹果电脑配置信息,发送到企业微信机器人自动提醒

import subprocess
import requests
import time
import json


class MacInfo:
    def __init__(self):
        self.strSerial_Number = ""  # SN序列号
        # self.strModel_Identifier = ""  # CPU信息
        self.strChip = ""  # CPU信息
        self.strProcessor_Name = ""  # 处理器信息
        self.strProcessor_Speed = ""  # 处理器速度
        self.valueNumber_of_Processors = ""  # 处理器数量
        self.valueTotal_Number_of_Cores = ""  # 核心总数
        self.strMemory = ""  # 内存大小
        self.strHardware_UUID = ""  # 硬盘UUID

    # 电脑型号
    def getModel_Identifier(self):
        cmd = "system_profiler SPHardwareDataType | grep 'Model Identifier'| awk '{print $3}'"
        result = subprocess.run(cmd, stdout=subprocess.PIPE, shell=True, check=True)
        model_identifier = result.stdout.strip()
        s = str(model_identifier, 'utf-8')
        return s

    '''
    def getProcessor_Nmae(self):
        cmd = "system_profiler SPHardwareDataType | grep 'Processor Name' | awk '{print $3$4$5$6}'"
        result = subprocess.run(cmd, stdout=subprocess.PIPE, shell=True, check=True)
        processor_nmae = result.stdout.strip()
        return processor_nmae'''

    # 获取电脑CUP型号
    def getChip(self):
        cmd = "system_profiler SPHardwareDataType | grep 'Chip' | awk '{print $2$3$4$5$6}'"
        result = subprocess.run(cmd, stdout=subprocess.PIPE, shell=True, check=True)
        chip = result.stdout.strip()
        return chip

    def getProcessor_Speed(self):
        cmd = "system_profiler SPHardwareDataType | grep 'Processor Speed' | awk '{print $3$4}'"
        result = subprocess.run(cmd, stdout=subprocess.PIPE, shell=True, check=True)
        processor_speed = result.stdout.strip()
        return processor_speed

    def getNumber_of_Processors(self):
        cmd = "system_profiler SPHardwareDataType | grep 'Number of Processors' | awk '{print $4}'"
        result = subprocess.run(cmd, stdout=subprocess.PIPE, shell=True, check=True)
        number_of_processors = result.stdout.strip()
        return number_of_processors

    def getTotal_Number_of_Cores(self):
        cmd = "system_profiler SPHardwareDataType | grep 'Total Number of Cores' | awk '{print $5}'"
        result = subprocess.run(cmd, stdout=subprocess.PIPE, shell=True, check=True)
        total_number_of_cores = result.stdout.strip()
        return total_number_of_cores

        # 获取电脑信内存

    def getMemory(self):
        cmd = "system_profiler SPHardwareDataType | grep 'Memory' | awk '{print $2$3}'"
        result = subprocess.run(cmd, stdout=subprocess.PIPE, shell=True, check=True)
        memory = result.stdout.strip()
        s = str(memory, 'utf-8')
        return s

        # 获取电脑SN序列号

    def getSerial_Number(self):
        cmd = "system_profiler SPHardwareDataType | grep 'Serial Number' | awk '{print $4}'"
        result = subprocess.run(cmd, stdout=subprocess.PIPE, shell=True, check=True)
        serial_number = result.stdout.strip()
        s = str(serial_number, 'utf-8')
        return s

    # def getHardware_UUID(self):
    #     cmd = "system_profiler SPHardwareDataType | grep 'Hardware UUID' | awk '{print $3}'"
    #     result = subprocess.run(cmd, stdout=subprocess.PIPE, shell=True, check=True)
    #     hardware_uuid = result.stdout.strip()
    #     s = str(hardware_uuid, 'utf-8')
    #     return s

    # 获取CUP
    def get_CUP(self):
        cmd = 'sysctl -n machdep.cpu.brand_string'
        result = subprocess.run(cmd, stdout=subprocess.PIPE, shell=True, check=True)
        data = result.stdout.split()
        m = []
        for i in data:
            s = str(i, 'utf-8')
            m.append(s)
        CUP = "".join(m)
        return CUP

        # 获取MAC地址

    def get_MAC(self):
        cmd = 'ifconfig en0'
        result = subprocess.run(cmd, stdout=subprocess.PIPE, shell=True, check=True)
        ipMAC = result.stdout.split()[6]
        s = str(ipMAC, 'utf-8')
        return s

        # 获取IP地址

    def get_IP(self):
        cmd = 'ifconfig en0'
        result = subprocess.run(cmd, stdout=subprocess.PIPE, shell=True, check=True)
        IP = result.stdout.split()[15]
        s = str(IP, 'UTF-8')
        return s

        # 获取inter硬盘大小
    def get_disk(self):
        cmd = 'diskutil list'
        result = subprocess.run(cmd, stdout=subprocess.PIPE, shell=True, check=True)
        disk = result.stdout.split()[10]
        s = str(disk, 'utf-8').replace("*", '') + " GB"
        if self.getModel_Identifier().startswith('Intel'):
            return s
             # 获取M1硬盘
        else:
            cmd1 = 'diskutil list'
            result = subprocess.run(cmd1, stdout=subprocess.PIPE, shell=True, check=True)
            disk = result.stdout.split()[9]
            s1 = str(disk, "utf-8")
            return s1

       

        # 传送到企业微信机器人
        #如果不需要发送企业微信这部省略即可
    def Webhook(self):
        url = "这里写自己企业微信创建的机器人的webhook地址"
        headers = {'Content-Type': 'application/json;charset=utf-8'}
        data_text = {
            "msgtype": "markdown",
            "markdown": {
                "content": f"MAC电脑配置信息请相关同事注意。\n>"
                           f"Model Identifier:{self.getModel_Identifier()}"
                           f"\n> Serial Number:"
                           f"{self.getSerial_Number()}\n> Chip:{self.get_CUP()}"
                           f"\n Memory:{self.getMemory()}"
                           f"\n disk:{self.get_disk()}"
                           f"\n ip:{self.get_IP()}"
                           f"\n mac:{self.get_MAC()}"
            }
        }
        r = requests.post(url, data=json.dumps(data_text), headers=headers)


if __name__ == '__main__':
    objHardware = MacInfo()
    print("Model Identifier:{}".format(objHardware.getModel_Identifier()))
    print("Serial Number:{}".format(objHardware.getSerial_Number()))
    print('Chip:{}'.format(objHardware.get_CUP()))
    print("Memory:{}".format(objHardware.getMemory()))
    print('disk:{}'.format(objHardware.get_disk()))
    print('ip:{}'.format(objHardware.get_IP()))
    print('mac:{}'.format(objHardware.get_MAC()))
    objHardware.Webhook()
    time.sleep(200)
    input('')
	
    # print("Processor Name:{}".format(objHardware.getChip()))
    # print("Processor Speed:{}".format(objHardware.getProcessor_Speed()))
    # print("Number of Processors:{}".format(objHardware.getNumber_of_Processors()))
    # print("Total Number of Cores:{}".format(objHardware.getTotal_Number_of_Cores()))
    # print("Hardware UUID:{}".format(objHardware.getHardware_UUID()))

接下来看效果
获取MAC OS苹果电脑配置信息,发送到企业微信机器人自动提醒_第1张图片

你可能感兴趣的:(机器人,python,开发语言)