OK - Figured it out...

Channel21 Python Team python at channel21.com
Wed May 10 22:10:46 CEST 2000


ok: (notes for code below)
1. adding user w/o password
2. popen pipe to passwd
3. write password
3.a. make sure to flush, or it won't actually take the input...
4. repeat 3,3.a
5. close
** added the sleeps because machine is too slow (it spat back conversation
errors otherwise)
this seems to be working fine and doing just what I wanted
(I am sure there must be a better way, but this works... ;>)

####################################################

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

def adduser(username, password):
    cmd = "/usr/sbin/useradd %s" %username
    os.system(cmd)
    time.sleep(2)
    p = os.popen("passwd %s" %username, 'w')    
    time.sleep(2)
    p.write(password)
    time.sleep(2)
    p.flush()
    time.sleep(2)
    p.write(password)
    time.sleep(2)
    p.flush()
    time.sleep(2)
    p.close()

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