Python は MacBook Pro デバイスのハードウェア情報を取得し、Webhook を通じてグループ チャットに送信します。

1 はじめに

前置きは不要です。効果を見てください。

2.効果

ここに画像の説明を挿入

3. コード

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.get_CUP().startswith('Intel'):
            return s
        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") + " GB"
            return s1

        # 获取M1硬盘

        # 传送到企业微信机器人

    def Webhook(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/*************" # 企微webhook机器人地址
        headers = {
    
    'Content-Type': 'application/json;charset=utf-8'}
        data_text = {
    
    
            "msgtype": "markdown",
            "markdown": {
    
    
                "content": f"MAC电脑配置信息<font color=\"warning\"></font>请相关同事注意。\n>"
                           f"Model Identifier:<font color=\"comment\">{
      
      self.getModel_Identifier()}"
                           f"</font>\n> Serial Number:<font color=\"comment\">"
                           f"{
      
      self.getSerial_Number()}</font>\n> Chip:<font color=\"comment\">{
      
      self.get_CUP()}"
                           f"</font>\n Memory:<font color=\"comment\">{
      
      self.getMemory()}"
                           f"</font>\n disk:<font color=\"comment\">{
      
      self.get_disk()}"
                           f"</font>\n ip:<font color=\"comment\">{
      
      self.get_IP()}"
                           f"</font>\n mac:<font color=\"comment\">{
      
      self.get_MAC()}</font>"
            }
        }
        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)

    # 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()))

おすすめ

転載: blog.csdn.net/qq_45194089/article/details/125826758