-
-
Save angeloped/3febaaf71ac083bc2cd5d99d775921d0 to your computer and use it in GitHub Desktop.
# 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()) |
To fix error change:
if "windows" in os_type:
to:
if "win" in os_type:
To fix error change:
if "windows" in os_type:to:
if "win" in os_type:
thanks for fixing the error! I just edited it.
I hope you don't mind that I used your code in my snippets collection on my blog I credited this page of course:
https://stevepython.wordpress.com/2019/10/22/python-code-snippets-vol-36
DarWIN includes WIN. Do it the other way around:
(why is the code not importing?...)
'import os
import sys
def 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 = "hal-get-property --udi /org/freedesktop/" \
"Hal/devices/computer --key system.hardware.uuid"
return os.popen(command).read().replace("\n", "").replace(" ", "").replace(" ", "")
print("Your motherboard", getMachine_addr())'
We can do it the other way around by using dmidecode -s baseboard-serial-number
for newer Linux variants:
import os
import sys
def 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())
Getting following errors (OS Linux Mint 19.3) :-
/sys/firmware/dmi/tables/smbios_entry_point: Permission denied
/dev/mem: Permission denied
However when script is run as sudo , it works as desired and spits out the mb serial number.
Wondering if there is any other method that facilitates above without sudo ?
For the original code Im getting this error:
/bin/sh: 1: hal-get-property: not found
For the fix from @shuq007 its running perfect but with sudo, any idea how to run it without sudo?
PC: Ubuntu 20.04
I'd recommend the str startswith
-method instead of using in
as others mentioned already that in
isn't safe.
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
Traceback (most recent call last):
File "ex3.py", line 16, in
print(getMachine_addr())
File "ex3.py", line 13, in getMachine_addr
return os.popen(command).read().replace("\n","").replace(" ","").replace(" ","")
UnboundLocalError: local variable 'command' referenced before assignment