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

A

Amy G

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.
 
J

John J. Lee

Amy G said:
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. [...]
os.system('adduser -f /home/sean/bin/users.test')
[...]

You're not checking the exit status. Read the docs for os.system and
os.wait, and look at the commands module. Then add code to your
program to log details of any failures (the logging module in 2.3 is
useful here), so you can figure out what goes wrong.


John
 
D

David M. Wilson

Amy G said:
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.

Have you tested adduser seperately? I can't see any problem with your
code, suggesting it may be adduser.

for line in c:
userid, password, name = line

You can do inline tuple unpack:

for userid, password, name in c:

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")

Just in case you hadn't noticed the shortcut here:

for userid, password, name in c:
f1.write("%s::20::::%s::sh:%s\n" %(userid, name, password))


Ahah! Where are the parenthesis? :) Your file is probably not getting
flushed to disk:

f1.close()


David.
 
A

Amy G

Thank you very much... it was indeed f1.close() that needed fixing. Program
works flawlessly.

And thanks for the shortcut, that is a good one.


David M. Wilson said:
Amy G said:
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.

Have you tested adduser seperately? I can't see any problem with your
code, suggesting it may be adduser.

for line in c:
userid, password, name = line

You can do inline tuple unpack:

for userid, password, name in c:

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")

Just in case you hadn't noticed the shortcut here:

for userid, password, name in c:
f1.write("%s::20::::%s::sh:%s\n" %(userid, name, password))


Ahah! Where are the parenthesis? :) Your file is probably not getting
flushed to disk:

f1.close()


David.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top