Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm trying to create a small python3 program for my friend which will install and configure the nessarcary set-up for his Ubuntu Server.

I want him to be able to start the program, give some basic information and be able to just leave the program to run.

The thing is, to do this I need the python file to send the password to the CL when requested for sudo commands.

I tried doing this for updating the Ubuntu Server:

os.system('sudo aptitude update && sudo aptitude dist-upgrade')
os.system(password)

After having the program request the password with this:

password=input('Please Enter Your Password')

But it didn't work. The CL just sits there waiting for the user to enter the password manually. Is ther anyway to get around this?

Thanks!

share|improve this question

1 Answer 1

up vote 2 down vote accepted

There are some solutions that put in the password automatically, but as they are visible in clear text for a moment, those are dangerous and you should not use them. Some methods are discussed here.

This is the proper way to run a python program as root:

import os
import sys
import subprocess


def main():
    status, output = subprocess.getstatusoutput("apt-get update")
    return status


if __name__ == '__main__':
    if os.getuid():
        print("This program needs to be run with root privileges,")
        print("please use `sudo ./{0}` to execute it".format(*sys.argv))
        sys.exit(100)
    else:
        sys.exit(main())

And the clear text method:

This is dangerous Your password will be visible to users on the system in the clear Be advised, passwords should never be stored anywhere, ever.

password = "password1"
subprocess.getstatusoutput(
    "echo {} | sudo -S apt-get update".format(password))

On a related note, the best way to prompt a user for a password is using getpass:

import getpass
password = getpass.getpass(prompt='Password: ')

This won't print the password as on the screen while the user is typing, as you'd expect on a Linux system.


import os
import sys
import subprocess


def main():
    print("hello world, we're root!")
    return 0


if __name__ == '__main__':
    if os.getuid():
        os.system("sudo python3 " + ' '.join(sys.argv))
        sys.exit(100)
    else:
        sys.exit(main())

This solution, instead of asking the user, runs itself as root if necessary.

share|improve this answer
1  
Great answer, Stefano. As you say, forcing the user to enter their sudo password instead of hard-coding it is much, much better than using sudo-in-script. –  Oli Apr 15 '11 at 10:47
    
If I use the getpass method, would it be safe for python to then eneter it automatically, or would that still be considered dangerous? Thanks for an awesome answer - Eden Crow –  Eden Crow Apr 15 '11 at 10:54
    
@Eden no, that would still be dangerous. But, I've added yet another solution that should do exactly what you want. Instead of telling the user to run the script as root, it runs itself as root, if it isn't already. –  Stefano Palazzo Apr 15 '11 at 11:03
    
@Stefano Thanks! Going to try to implement it in my program now... –  Eden Crow Apr 15 '11 at 11:10
    
@Stefano I keep getting the error "name 'sys' is not defined" when using the last solution. What am I doing wrong. Thanks (and I won't be able to reply straight away as need to go somewhere now). –  Eden Crow Apr 15 '11 at 11:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.