0

In my snap (coded in python), I try to perform some sudo commands but it didn’t work. Here is an example of a command that didn’t work:

command = "sudo netmgr -i country_code set:" + countryCode
subprocess.run([command])

And when I run the snap in my device it won’t work and I got this error:

> Traceback (most recent call last): File
> “/snap/iotr-configuration/x17/bin/iotr-configuration”, line 11, in 
> load_entry_point(‘iotr-configure==0.0.3’, ‘console_scripts’,
> ‘iotr-configuration’)() File
> “/snap/iotr-configuration/x17/lib/python3.5/site-packages/src/app.py”,
> line 53, in main configuration_program() File
> “/snap/iotr-configuration/x17/lib/python3.5/site-packages/src/app.py”,
> line 37, in configuration_program
> confNIC.set_nic_settings(“fd05:a40b:b47d:7340::4”, “1250”) File
> “/snap/iotr-configuration/x17/lib/python3.5/site-packages/src/configureNic.py”,
> line 16, in set_nic_settings subprocess.run([command]) File
> “/snap/iotr-configuration/x17/usr/lib/python3.5/subprocess.py”, line
> 693, in run with Popen(*popenargs, **kwargs) as process: File
> “/snap/iotr-configuration/x17/usr/lib/python3.5/subprocess.py”, line
> 947, in init restore_signals, start_new_session) File
> “/snap/iotr-configuration/x17/usr/lib/python3.5/subprocess.py”, line
> 1551, in _execute_child raise child_exception_type(errno_num, err_msg)
> FileNotFoundError: [Errno 2] No such file or directory: ‘sudo netmgr
> -i country_code set:1250’

This function exist because when I type it directly in the terminal, it works…

Can you help me on this issue ?

Ben Gates
  • 762
  • 5
  • 14
Kevin
  • 9
  • 1
  • 5

1 Answers1

0

You're calling subprocess.run in the wrong way. You should either pass it a the command as a single string (like you're doing here) but then set shell=True, or break the command into several arguments, as in:

command = ["sudo", "netmgr", "-i", "country_code", "set:" + countryside]
subprocess.run(command)

See the FAQ part of the documentation:

args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.

Roy2012
  • 11,755
  • 2
  • 22
  • 35
  • Ok thank you, but now I have another issue when I use the command you told me: `cannot open /run/snapd/ns/snap.netmgr.info: Permission denied` but when I find the file, and check the permissions, it show that I have the permission – Kevin Jun 03 '20 at 09:03
  • Emm. Might have something to do with sudo. Could you run your entire script under sudo (e.g. sudo python3 ...), and see what happens? Also - do the same, but remove 'sudo' from the command. – Roy2012 Jun 03 '20 at 09:06
  • When I run directly my program, it work (even without the "sudo" before python3). And when I try to remove sudo from the command, it shows me `internal error, please report: running "netmgr" failed: open /snap/netmgr/x1/meta/snap.yaml: permission denied` – Kevin Jun 03 '20 at 09:31
  • so I don't understand. In your first comment, you wrote that you're getting a 'cannot open ...' error. But now, you're saying that when you run your program it works? Not sure I follow. – Roy2012 Jun 03 '20 at 09:32
  • When I run my program with "sudo python3 setup.py" it works well indeed. But I want to snap my programme to use it in Ubuntu Core. So I snapped it and installed install it with "sudo snap install iotr-configuration.snap". And then when I use the command "iotr-configuration" to start my program, the command doesn't work... In fact every command with sudo doesn't work inside my snap – Kevin Jun 03 '20 at 09:37
  • Have a look at this one: https://forum.snapcraft.io/t/running-snap-and-sudo-from-within-a-python-snap/17307 – Roy2012 Jun 03 '20 at 09:39
  • I tried to add the plugs but I got the message `cannot open /run/snapd/ns/snap.netmgr.info: Permission denied` again... I don't know why and I configured my snap in devmode to have access top everything... I don't know what is happening with these "sudo" commands – Kevin Jun 03 '20 at 11:49