PDA

View Full Version : [SOLVED] Python run terminal command with sudo



MaxVK
October 10th, 2008, 07:06 PM
Hi there.

I am very new to Python, so new in fact that this is the first thing I have considered doing with it, so be gentle! Anyway:

I would like to write a script that, when executed, would be able to open a terminal window for me to type my password. I want to be able to create a desktop launcher that will point to the correct script (python <script name>)

I need to run the following command:
sudo /home/checkit/fellowsbook

Of course I can do this:

import os
os.system("sudo /home/checkit/fellowsbook")

but no terminal window opens, so I cant type my password and the script does nothing.

Is there a way around this?

Many thanks

Max

geirha
October 10th, 2008, 07:11 PM
Try with gksudo instead of sudo, it will pop up a graphical password dialog.

LaRoza
October 10th, 2008, 07:27 PM
You are better off running the script with sudo.

MaxVK
October 10th, 2008, 07:35 PM
Thanks for the replies:

geirha:
Surely gksudo is for graphical apps, and this one would normally be started from a terminal with sudo. Is it okay to use gksudo like that? I always thought that sudo and gksudo should be used in different circumstances.

LaRoza:
Do you mean that I should include sudo in the launcher command? Remembering that I want to run this script from an icon on the desktop.

regards

Max

lykwydchykyn
October 10th, 2008, 08:36 PM
import os
os.popen("sudo -S somecommand", 'w').write("mypassword")


That'd obviate the need for asking a password, but it's not secure. I just re-read your post, you want a terminal to pop up here. So why not:



os.system("xterm -c \"sudo /home/checkit/fellowsbook\"")

Rich78
October 10th, 2008, 08:38 PM
As Laroza said use sudo for the launcher command.

This will prompt for the password prior to running the script, once the password has entered, the content of the script will run under sudo.

unutbu
October 10th, 2008, 09:11 PM
If this is your python script:


#!/usr/bin/env python
import os
os.system("/home/checkit/fellowsbook")
Then make the launcher command


gksu python_script.py

If you use sudo, the launcher will not open a terminal for you to ask for your password, so you have to use gksu.

themusicwave
October 10th, 2008, 09:37 PM
It is also nice to check if the user is root when your script first starts to run. If they aren't tell them to run it as root and exit. Like so:


import os, sys

# if not root...kick out
if not os.geteuid()==0:
sys.exit("\nYou must be root to run this application, please use sudo and try again.\n")


Please note the getuid function only works on Unix. I tried running that snippet on my Windows machine at work and it failed. Then I read the docs...

MaxVK
October 11th, 2008, 08:28 AM
Aha! This is great, many thanks to you all - I now have the answer to another question (check to see if user is root) as well as the original!

Many thanks - Ill get onto this as soon as I'm not working! ;)

Regards

Max