【问题标题】:Using Python to find Mac UUID/Serial Number使用 Python 查找 Mac UUID/序列号
【发布时间】:2018-06-05 20:21:26
【问题描述】:

基本上,我打算将计算机 UUID/序列号绑定到运行它的密钥上,在 Windows 上,我发现获取 UUID 很容易,但是我很难为 Mac 获取任何东西。 有什么解决办法吗?

【问题讨论】:

    标签: python macos uuid


    【解决方案1】:

    MacOS 有一个用于访问这些信息的内置程序,您可以使用它来获取它

    system_profiler SPHardwareDataType | awk '/Serial Number/ {print $4}'
    

    如果您在 python 中明确需要此字符串(并且如果您使用的是 3.5+),则可以使用 subprocess 模块

    import subprocess
    cmd = "system_profiler SPHardwareDataType | awk '/Serial Number/ {print $4}'"
    result = subprocess.run(cmd, stdout=subprocess.PIPE, shell=True, check=True)
    serial_number = result.stdout.strip()
    

    【讨论】:

      【解决方案2】:

      这是我通过 Python 获取 Mac 连续剧的方式:

      import subprocess
      
      task = subprocess.Popen(
          ['system_profiler', 'SPHardwareDataType'],
          stdout=subprocess.PIPE,
          stderr=subprocess.PIPE
      )
      
      out, err = task.communicate()
      
      for l in out.split('\n'):
          if 'Serial Number (system)' in l:
              serial_line = l.strip()
              break
      
      serial = serial_line.split(' ')[-1]
      
      print(serial)
      

      编辑:我发现了一种更短的方法,而且我更喜欢线分割循环。

      import json
      import subprocess
      system_profile_data = subprocess.Popen(
          ['system_profiler', '-json', 'SPHardwareDataType'], stdout=subprocess.PIPE)
      data = json.loads(system_profile_data.stdout.read())
      serial = data.get('SPHardwareDataType', {})[0].get('serial_number')
      print(serial)
      

      pyobjc方式可以在这里找到:https://gist.github.com/pudquick/c7dd1262bd81a32663f0

      【讨论】:

        猜你喜欢
        • 2013-03-05
        • 2019-03-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-25
        • 2014-08-21
        • 1970-01-01
        • 2021-04-04
        相关资源
        最近更新 更多