Last active
February 2, 2024 09:55
-
-
Save angeloped/3febaaf71ac083bc2cd5d99d775921d0 to your computer and use it in GitHub Desktop.
Python code to get motherboard serial number. For Linux, MacOS, and Windows.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# wmic bios get serialnumber#Windows | |
# hal-get-property --udi /org/freedesktop/Hal/devices/computer --key system.hardware.uuid#Linux | |
# ioreg -l | grep IOPlatformSerialNumber#Mac OS X | |
import os, sys | |
def getMachine_addr(): | |
os_type = sys.platform.lower() | |
if "win" in os_type: | |
command = "wmic bios get serialnumber" | |
elif "linux" in os_type: | |
command = "hal-get-property --udi /org/freedesktop/Hal/devices/computer --key system.hardware.uuid" | |
elif "darwin" in os_type: | |
command = "ioreg -l | grep IOPlatformSerialNumber" | |
return os.popen(command).read().replace("\n","").replace(" ","").replace(" ","") | |
#output machine serial code: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX | |
print(getMachine_addr()) |
mport os
import sysdef getMachine_addr():
""" Find OS and run appropriate read mobo serial num command"""
os_type = sys.platform.lower()if "darwin" in os_type: command = "ioreg -l | grep IOPlatformSerialNumber" elif "win" in os_type: command = "wmic bios get serialnumber" elif "linux" in os_type: command = "dmidecode -s baseboard-serial-number" return os.popen(command).read().replace("\n", "").replace(" ", "").replace(" ", "")
print("Your motherboard Serial No.", getMachine_addr())
The best solution
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'd recommend the str
startswith
-method instead of usingin
as others mentioned already thatin
isn't safe.