0

I'm trying to deploy the project from VSCode to Azure function app that has Linux operating system. I run the project locally on Windows using processor id by using following command:

os.popen("wmic cpu get processorid").read().replace("\n", "").replace("  ", "").replace(" ", "")[11:]

I need the analogue of the aforementioned command to get processor id for Linux system. I tried below command on Ubuntu terminal and it worked:

sudo dmidecode --type processor

enter image description here

I get errors when I write it in VSCode and deploy it to Azure function app using following code (from [here]):

import platform, subprocess, logging

def get_processor_info():
    if platform.system() == "Windows":
        return platform.processor()
    elif platform.system() == "Darwin":
        return subprocess.check_output(['/usr/sbin/sysctl', "-n", "machdep.cpu.brand_string"]).strip()
    elif platform.system() == "Linux":
        command = "sudo dmidecode --type processor"
        logging.info("My command: %s.", str(subprocess.check_output(command, shell=True).strip()))
    return ""
    
get_processor_info()

Output of the error is: " Result: Failure Exception: CalledProcessError: Command '['sudo', 'dmidecode', '--type', 'processor']' returned non-zero exit status 1. Stack: File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/dispatcher.py", line 405, in _handle__invocation_request invocation_id, fi_context, fi.func, args) File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run result = self.fn(*self.args, **self.kwargs) File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/dispatcher.py", line 612, in _run_sync_func func)(params) File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/extension.py", line 215, in _raw_invocation_wrapper result = function(**args) File "/home/site/wwwroot/timer_trig_05/init.py", line 79, in main logging.info("My command: %s.", str(get_processor_info())) File "/home/site/wwwroot/timer_trig_05/init.py", line 76, in get_processor_info return subprocess.check_output(cmd_lst) File "/usr/local/lib/python3.7/subprocess.py", line 411, in check_output **kwargs).stdout File "/usr/local/lib/python3.7/subprocess.py", line 512, in run output=stdout, stderr=stderr)."

What am I doing wrong? Or maybe there're any other ways to extract the processor id?

8
  • Why are you posting a photo of the screen instead of copying and pasting text? Commented Apr 10, 2022 at 20:08
  • More to the point, do you have passwordless sudo privileges for noninteractive processes on the VM in question? If not, you'll need to edit /etc/sudoers (hopefully scoped to only allow dmidecode) Commented Apr 10, 2022 at 20:09
  • 1
    In general, Linux intentionally restricts CPU ID information to protect user privacy. Have you considered using /etc/machine-id instead? How about the ethernet MAC address? Commented Apr 10, 2022 at 20:48
  • 1
    Do also see Best way to get machine ID on Linux. Because you're running on an emulated processor, you don't have access to a real hardware serial number either way. Commented Apr 10, 2022 at 20:50
  • 1
    @CharlesDuffy thanks a lot. I used the command from here: stackoverflow.com/a/63148464/9951611 and manged to get machine id. I think machine id will be enough for me Commented Apr 10, 2022 at 21:13

1 Answer 1

0

I guess it's a typo but the command you pass to the subprocess.checkoutput is sudo vim and not sudo dmidecode --type processor. Besides, just like in the Darwin case, arguments should be passed as a list of string. I changed the return value to match the other cases:

import platform, subprocess, logging

def get_processor_info():
    if platform.system() == "Windows":
        return platform.processor()
    elif platform.system() == "Darwin":
        return subprocess.check_output(['/usr/sbin/sysctl', "-n", "machdep.cpu.brand_string"]).strip()
    elif platform.system() == "Linux":
        command = "sudo dmidecode --type processor"
        cmd_lst = command.split(' ')
        return subprocess.check_output(cmd_lst)
    return ""
    
print(get_processor_info())

Edit: like @CharlesDuffy rightfully pointed out, you can match the other cases by writing it this way:

def get_processor_info():
    if platform.system() == "Windows":
        return platform.processor()
    elif platform.system() == "Darwin":
        return subprocess.check_output(['/usr/sbin/sysctl', "-n", "machdep.cpu.brand_string"]).strip()
    elif platform.system() == "Linux":
        return subprocess.check_output(['sudo', 'dmidecode', '--type', 'processor'])
    return ""
Sign up to request clarification or add additional context in comments.

3 Comments

I've already changed the command here - I made the mistake when i pasted the code here. But i ran the code with your code and got the same error as in my post.
Better to use command = ['sudo', 'dmidecode', '--type', 'processor'] -- there's no real advantage to command.split(' '), and several disadvantages to making a habit of it (if you need to parameterize filenames, there are bugs one runs into when they contain spaces when going that route)
@CharlesDuffy you're right. I just wanted to make clear the changes to OP's code. If we want to match the other cases, the right way to write it would even be return subprocess.check_output(['sudo', 'dmidecode', '--type', 'processor']) on a single line

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.