0

I am writing a python/Qt application which needs to perform disk mounting. Hence it needs root permissions. Is there any way that I could make the script acquire root permissions using a popup for password, like gparted does? I know I can detect within the script, if it has been run with sudo or not, with something like this (for example in bash):

if [[ $(id -u) -ne 0 ]];

But after this, how do I ask for the password and acquire root permissions at runtime?

Thanks.

Sujay Phadke
  • 2,145
  • 1
  • 22
  • 41

1 Answers1

1

Like this:

>>> import os
>>> os.system("gksudo software-center")

Popup sudo that then executes the command of interest (here, Ubuntu software center).

You can edit this with any subprocess or other way of calling a shell command.

EDIT: Anonymous downvoters: gksudo is the perfect response here? You want someone who needs root access who may not have run the script with root access to begin with. gksudo is just an interface to get root permissions to do a command, particularly for GUIs, exactly what is requested.

Leave a comment please.

Alex Huszagh
  • 13,272
  • 3
  • 39
  • 67
  • Thanks. In my python app, I used "gksudo whoami", which is harmless but gives the prompt pop-up. Is there a way to ask for sudo permissions without running a specific shell command though? Secondly, I'd like to add my own text to the window that pops-up. For example, gparted popups a windows saying "authentication is required.... blah blah". How do I simply check for sudo permissions to popup a custom authentication window? – Sujay Phadke Jun 27 '15 at 06:20
  • It looks like a standardized system auth popup since it has an icon on the side (used for gnome keyring) which combines a keyring image and a super-imposed image of the application's icon (in this case gparted). But I have no idea how to get this kind of popup. – Sujay Phadke Jun 27 '15 at 06:51
  • I get the interest, but I'm not sure why you'd want to reinvent the wheel? Due to the way that Unix handles scripts, you would probably need to implement a Rootwrap bound to a QMessageBox/QDialog with your message of interest, saying: if event.accepted(): do some_task. To do it in purely Python, try this: https://ask.openstack.org/en/question/60893/rootwrap-python-write-to-root-only-owned-file/ However, this is exactly WHY gksudo exists: to do a root task from a script where sudo is not available. – Alex Huszagh Jun 27 '15 at 09:22
  • thanks. The "why" is because if I do "gksudo whoami", the popup says something like "the program whoami is requesting root permissions". Whereas, the root permissions are actually required by my parent program. This would just create confusion and that's why I want to customize the message in the popup or have my own popup. – Sujay Phadke Jun 27 '15 at 09:31
  • You can just rename a script to whatever your program name actually is. Then, you don't have to worry about it. I'm still not seeing why you need special permissions. Or, you can pass the message flag to write your own message. For example, try: "gksudo whoami --message cool" – Alex Huszagh Jun 27 '15 at 10:25