Ubuntu Server Automatically Insert Password Using Python3

5,768

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:
5,768

Related videos on Youtube

Eden Crow
Author by

Eden Crow

Updated on September 18, 2022

Comments

  • Eden Crow
    Eden Crow over 1 year

    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!

  • Oli
    Oli about 13 years
    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.
  • Eden Crow
    Eden Crow about 13 years
    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
  • flo
    flo about 13 years
    @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.
  • Eden Crow
    Eden Crow about 13 years
    @Stefano Thanks! Going to try to implement it in my program now...
  • Eden Crow
    Eden Crow about 13 years
    @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).
  • flo
    flo about 13 years
    Huh.. You would get this error if you somehow missed the import sys at the top of the file; I've tested this solution on python versions 2.4 through 3.2, it should work as is.
  • Eden Crow
    Eden Crow about 13 years
    @Stefano Thanks (for making me feel stupid :P). I did miss import sys! I was just confused as was battling with a couple of bugs at the same time. Thanks for all your amazing helP!
  • flo
    flo about 13 years
    oh cool :-) you're very welcome