[Techtalk] script for adding users

Carla Schroder carla at bratgrrl.com
Sat May 8 17:34:01 EST 2004


On Saturday 08 May 2004 5:07 pm, wendy wrote:
> Anybody know of an easy way to add 100 users to linux?  I was thinking
> in terms of maybe a bash script calling on a spreadsheet file with
> username and password. 
> 

I'm so glad you asked! A number of our fab Techtalkers collaborated on this 
very thing, and came up with a lovely Python script to do the very thing you 
want. It was posted sometime ago on Techtalk, and here it is again. 

#!/usr/bin/env python

##
## Mass Useradd For Linux
## A Python script that extracts new user data
## from a delimited text file, and automatically generates
## new user accounts. It generates  a random password for
## each login, and exports the new logins and passwords
## to a text file. Passwords automatically expire at first login.
##
## Mass Useradd creates a "User Personal Group."
## The UID and the GID are the same. User's home directories
## are created with restrictive permissions, chmod 700.
## Mass Useradd uses standard Shadow Suite utilities.
## Values and behaviors are easily modifiable, according
## to the individual utility being called.
##
## This script was created by Aaron Malone, and modified by
## Meredydd Luff, Peter Samuelson, and Kathryn Hogg.
## Many thanks!
## Carla Schroder wrote the documentation and pestered
## the aforementioned persons to modify the original script.
## Copyright (C) 2003  Carla Schroder
## carla at bratgrrl dot com
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## http://www.fsf.org/licenses/gpl.html
##
##   Usage:
##       #  python mass-useradd.py < inputfile >> new-passwords.txt
##

from string import lowercase, uppercase
from random import choice
from os import system, popen
from sys import stdin, stdout


def make_pass():
    """creates a random eight-character password from A-Za-z0-9."""
    numbers = "".join(map(str, range(10)))
    chars = lowercase + uppercase + numbers
    
    p = ""
    for r in range(8):
        p += choice(chars)

    return p

# This causes the password to expire at first login,
# requiring the user to create a new one
olddate = "2003-01-01"

# this section reads from the input file,
# and generates the new password
# the format of the input file must be like this:
# userlogin:FirstName LastName,,,,,
# to use a comma-delimited file, substitute
# line.split(',', 1)
for line in stdin.readlines():
    line = line.strip()  # remove EOL characters
    username, realname = line.split(':')
    password = make_pass()

# this part reads /etc/passwd and /etc/group, and calculates
# the next available UID and GID.
# it starts at {id=1000}, change this to suit
    chp = popen("cat /etc/passwd /etc/group | cut -f3 -d: | sort -un | awk 
'BEGIN { id=1000 } $1 == id { id++ } $1 > id { print id; exit }'", 'r')
    next_id = int(chp.read())
    chp.close()

# Now users are added to /etc/group, /etc/passwd,
# and home directories with chmod 700 are created
# Any of the /groupadd, /useradd, and /chmod options
# can be changed to suit
    system("/usr/sbin/groupadd -g %d %s" % (next_id, username))
    system("/usr/sbin/useradd -m -c \"%s\" -g %s -u %d %s" % (realname, 
username, next_id, username))
    system("/bin/chmod 700 /home/%s" % (username))

# set the password.  We use popen instead of just echoing the
# password because we don't want it to show up on a process list
    chp = popen("/usr/sbin/chpasswd", 'w')
    chp.write("%s:%s\n" % (username, password))
    chp.close()

# finally, we expire the password by setting the 'last changed'
# date to somewhere in the past, and the expire timeout to
# ninety days
    system("/usr/bin/chage -M 90 -d %s %s" % (olddate, username))

# password is printed to stdout. direct output
# to a file to save it, see 'Usage'
    print "%s\t%s\t%d" % (username, password, next_id)


-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Carla Schroder
this message brought to you
by Libranet 2.8 and Kmail
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



More information about the Techtalk mailing list