Welchen Befehl kann ich verwenden, um die Seriennummer des Systems von der Unix-Befehlszeile abzurufen? Da uname
einige Informationen über die Software und Hardware ausgegeben werden, möchte ich die Seriennummer aus einem Befehl abrufen, um sie in einem Skript zu verwenden.
Der Befehl system_profiler bietet eine direkte Antwort, die für Menschen leicht lesbar ist (vorausgesetzt, Sie verwenden 10.3 oder neuer), aber Sie können auch ioreg für die Aufgabe verwenden, da sie im Allgemeinen schneller abgeschlossen wird.
system_profiler SPHardwareDataType
ist der Datentyp, der die wichtigsten Hardwareinformationen enthält, und Sie können grep oder awk verwenden, um die Dinge nach Bedarf weiter zu reduzieren:
system_profiler SPHardwareDataType | awk '/Serial/ {print $4}'
oder
ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'
Beide Befehle benötigen zwischen 0,5 und 0,2 Sekunden, um auf modernen SSD-Macs ausgeführt zu werden. Wenn Sie also den Befehl optimieren und das " entfernen möchten, können Sie Ihre Antwort in etwa 0,005 Sekunden erhalten:
ioreg -c IOPlatformExpertDevice -d 2 | awk -F\" '/IOPlatformSerialNumber/{print $(NF-1)}'
Das funktioniert auch…
ioreg -l | grep IOPlatformSerialNumber
C++ Beispiel von https://developer.apple.com/library/archive/technotes/tn1103/_index.html :
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
std::string
getSerialNumber()
{
CFStringRef serial;
char buffer[32] = {0};
std::string seriaNumber;
io_service_t platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault,
IOServiceMatching("IOPlatformExpertDevice"));
if (platformExpert)
{
CFTypeRef serialNumberAsCFString = IORegistryEntryCreateCFProperty(platformExpert,
CFSTR(kIOPlatformSerialNumberKey),
kCFAllocatorDefault, 0);
if (serialNumberAsCFString) {
serial = (CFStringRef)serialNumberAsCFString;
}
if (CFStringGetCString(serial, buffer, 32, kCFStringEncodingUTF8)) {
seriaNumber = buffer;
}
IOObjectRelease(platformExpert);
}
return seriaNumber;
}
Um es leichter zu merken, verwenden Sie eine Suche ohne Berücksichtigung der Groß-/Kleinschreibung:
ioreg -l | grep -i serialnumber | grep -i platform
Dies funktioniert in Python 3.10.5 unter macOS
import json
import os
import time
# Example MySPHardwareDataType.json created and used by this script
#{
# "SPHardwareDataType" : [
# {
# "_name" : "hardware_overview",
# "activation_lock_status" : "activation_lock_disabled",
# "boot_rom_version" : "7459.121.3",
# "chip_type" : "Apple M1",
# "machine_model" : "MacBookPro17,1",
# "machine_name" : "MacBook Pro",
# "number_processors" : "proc 8:4:4",
# "os_loader_version" : "7459.121.3",
# "physical_memory" : "16 GB",
# "platform_UUID" : "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
# "provisioning_UDID" : "XXXXXXXX-XXXXXXXXXXXXXXXX",
# "serial_number" : "XXXXXXXXXXXX"
# }
# ]
#}
def export_basic_hwinfo_to_json(json_filename_and_path):
# This is the command that will execute. It will run macOS' built-in
# system_profiler binary, filtered to only show the contents of
# SPHardwareDataType, which contains the data we are looking for.
# Additionally, we have also specified that it should be exported to JSON
# format, and that standard output should be redirected to the file specified earlier
command = "system_profiler SPHardwareDataType -json >" + json_filename_and_path
# execute the command
os.popen(command)
# This loop waits for the file to exist before continuing
while os.path.exists(json_filename_and_path) is False:
time.sleep(1)
def get_serial(json_filename_and_path) -> str:
# open the .json file for processing
file_handle = open(json_filename_and_path)
# load the contents of the .json file into a Python dictionary
hw_info_dict = json.load(file_handle)
# retrieve the data we are looking for
serial = hw_info_dict['SPHardwareDataType'][0]['serial_number']
return serial
def get_machine_sku(json_filename_and_path) -> str:
# open the .json file for processing
file_handle = open(json_filename_and_path)
# load the contents of the .json file into a Python dictionary
hw_info_dict = json.load(file_handle)
# retrieve the data we are looking for
machine_sku = hw_info_dict['SPHardwareDataType'][0]['machine_model']
return machine_sku
def get_model(json_filename_and_path) -> str:
# open the .json file for processing
file_handle = open(json_filename_and_path)
# load the contents of the .json file into a Python dictionary
hw_info_dict = json.load(file_handle)
# retrieve the data we are looking for
model = hw_info_dict['SPHardwareDataType'][0]['machine_name']
return model
def main():
# We are going to store the output file in a temp directory
temp_dir: str = os.environ.get('TMPDIR')
temp_dir: str = temp_dir[:-1]
json_filename: str = "MySPHardwareDataType.json"
# Cleanly append the name of the output file to the file path
json_filename_and_path: str = os.sep.join([temp_dir, json_filename])
export_basic_hwinfo_to_json(json_filename_and_path)
serial: str = get_serial(json_filename_and_path)
machine_sku: str = get_machine_sku(json_filename_and_path)
model: str = get_model(json_filename_and_path)
print(F"serial={serial}")
print(F"machine_sku={machine_sku}")
print(F"model={model}")
if __name__ == "__main__":
main()
Wenn Sie keine Spalten zählen möchten, können Sie sich darauf verlassen, dass der Wert immer in der letzten steht und verwenden
system_profiler SPHardwareDataType | awk '/Serial/{print $NF}'
Jasonologie