Adding newusers to Unix system from mysql data (Better Approach)

Amy G amy-g-art at cox.net
Sat Dec 27 02:13:50 CET 2003


I am writing a simple Python program that will read in data from a database
(userid, password, username).  It will then store the data in a file in the
format

name:uid:gid:class:change:expire:gecos:home_dir:shell:password

or in my case

userid::20::::username::sh:password

Then I do an os.system('adduser -f <filepath>')

My problem is this...  It writes the data file without problem.  But, the
os.system... does not necessarily work to add a new user.  By 'not
necessarily' I mean that it works some of the time, but not all of the time.
If I run the program and it does not work, I can simple type 'adduser -f
<filepath>' at the command line and it works no problem.  Any ideas what
could be causing this.

CODE
******************************************************************
#!/usr/local/bin/python

import MySQLdb
import os

db=MySQLdb.connect(db="challenge")
c=db.cursor()
c.execute("""SELECT userid, password, name from users where status is
null""")
aList = []

for line in c:
    userid, password, name = line
    aList.append("%s::20::::%s::sh:%s" %(userid, name, password))

f1=open('/home/sean/bin/users.test', 'w')
for line in range(len(aList)):
    f1.write(str(aList[line]) + "\n")
f1.close

os.system('adduser -f /home/sean/bin/users.test')

****************************************************************

Thanks in advance.






More information about the Python-list mailing list