473,473 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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:sh ell: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.
Jul 18 '05 #1
3 1207
"Amy G" <am*******@cox.net> writes:
[...]
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
Jul 18 '05 #2
"Amy G" <am*******@cox.net> wrote...
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))

f1.close


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

f1.close()
David.
Jul 18 '05 #3
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" <dw***********@botanicus.net> wrote in message
news:99**************************@posting.google.c om...
"Amy G" <am*******@cox.net> wrote...
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))

f1.close


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

f1.close()
David.

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Sims | last post by:
Hi, I have a field in my DB that saves the date/time as a integer. I get the time on my server using the time() function. now, moving away from php and looking at (My)SQL only. If I use...
8
by: Sandy Norton | last post by:
Hi folks, I have been mulling over an idea for a very simple python-based personal document management system. The source of this possible solution is the following typical problem: I...
33
by: Joshua D. Drake | last post by:
Hello, I think the below just about says it all: http://www.commandprompt.com/images/mammoth_versus_dolphin_500.jpg Sincerely, Joshua Drake
9
by: Jan Wieck | last post by:
Dear community, for some reason the post I sent yesterday night still did not show up on the mailing lists. I have set up some links on the developers side under...
3
by: job | last post by:
REQUEST FOR PROPOSAL Overview Our company is a mom & pop company seeking to capitalize its marketing to our several thousand past customer base, as well as expand. We currently sell mainly...
8
by: John Baker | last post by:
Hi: I am URGENTLY in need of some book or web site OR tool that will help me integrate a relatively simple access application into a web page or pages. This is a time recording system (by...
5
by: strawberry | last post by:
In the function below, I'd like to extend the scope of the $table variable such that, once assigned it would become available to other parts of the function. I thought 'global $table;' would solve...
6
by: bill | last post by:
I am about to start on a module that will accept a location from a user, use Google geolocation services to get the lat/lon and then compute the distance from the site visitor to about 100 kennels...
12
by: Bob Jones | last post by:
I have an odd business requirement and I think that the implementation is not correct in the terms of OOP development. Any help on the concepts would be very appreciated! We currently have a...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.