Custom adduser function...

Jeff Bauer jbauer at rubic.com
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 
below.

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

---

#!/usr/bin/python
import os, sys

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

if __name__ == '__main__':
    usage = "usage: %s username [password]" % \
            os.path.basename(sys.argv[0])
    if len(sys.argv) < 2:
        print usage
        sys.exit(1)
    username = sys.argv[1]
    if len(sys.argv) > 2:
        password = sys.argv[2]
    else:
        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
            else:
                print "Passwords don't match."
    adduser(username, password)




More information about the Python-list mailing list