Custom adduser function...

Jeff Bauer jbauer at
Wed May 10 13:59:23 EDT 2000

Channel21 Python Team wrote:
> ok - RedHat 6.2
> need to have a function w/ 2 args (username,password) 
> which will create a system account w/ the username 
> and passwd passed in... any ideas?

The easiest way is to probably pass the arguments
to /usr/sbin/useradd via os.command().  Sample code 

I would probably add keyword arguments to support
the other account options (i.e. uid, group, shell,
home, comment, expire, etc.)

Best regards,

Jeff Bauer
Rubicon Research


import os, sys

def adduser(username, password):
    cmd = "/usr/sbin/useradd -p %s %s" % (password, username)

if __name__ == '__main__':
    usage = "usage: %s username [password]" % \
    if len(sys.argv) < 2:
        print usage
    username = sys.argv[1]
    if len(sys.argv) > 2:
        password = sys.argv[2]
        from getpass import getpass
        password = None
        while password is None:
            p1 = getpass("Password for user '%s':" % username)
            p2 = getpass("Re-enter user '%s' password:" % username)
            if p1 == p2:
                password = p1
                print "Passwords don't match."
    adduser(username, password)

More information about the Python-list mailing list