Results 1 to 9 of 9

Thread: [SOLVED] Python run terminal command with sudo

  1. #1
    Join Date
    Dec 2007
    Beans
    386
    Distro
    Ubuntu 10.04 Lucid Lynx

    [SOLVED] Python run terminal command with sudo

    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:
    Code:
    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

  2. #2
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Python run terminal command with sudo

    Try with gksudo instead of sudo, it will pop up a graphical password dialog.

  3. #3
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Python run terminal command with sudo

    You are better off running the script with sudo.

  4. #4
    Join Date
    Dec 2007
    Beans
    386
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python run terminal command with sudo

    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

  5. #5
    Join Date
    Jun 2008
    Location
    Tennessee
    Beans
    3,415

    Re: Python run terminal command with sudo

    Code:
    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:

    Code:
    os.system("xterm -c \"sudo /home/checkit/fellowsbook\"")
    Last edited by lykwydchykyn; October 10th, 2008 at 08:41 PM.

  6. #6
    Join Date
    Jan 2007
    Beans
    209

    Re: Python run terminal command with sudo

    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.

  7. #7
    Join Date
    Mar 2008
    Beans
    4,715
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Python run terminal command with sudo

    If this is your python script:
    Code:
    #!/usr/bin/env python
    import os
    os.system("/home/checkit/fellowsbook")
    Then make the launcher command
    Code:
    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.

  8. #8
    Join Date
    Aug 2006
    Location
    Pittsburgh, PA
    Beans
    424
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python run terminal command with sudo

    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:

    PHP Code:
    import ossys

    # 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...
    Last edited by themusicwave; October 10th, 2008 at 09:40 PM. Reason: Added Note

  9. #9
    Join Date
    Dec 2007
    Beans
    386
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python run terminal command with sudo

    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •