You are not logged in.

#1 2009-08-14 16:27:50

kjon
Member
From: Temuco, Chile
Registered: 2008-04-16
Posts: 398

script for automating account creation

Hi.

Well I'm writting this because the domain controller got fried yesterday. We got a very strong storm here and, while the trees were flying by the wind, the computer ceased to exist due to a lightning (I'll post pics later, there are a lot of people working here, retrieving evidence for insurance issues)

The real problem is that I need to set urgently around 200+ accounts for a temporary domain controller in order to get the network running as soon as possible. I got a list with all these people, but I don't know how to archieve this on bash. Would you guys help me?

The idea is pretty simple, I got my text file with names and I need the proper account get created. I know how to play with adduser, but I don't know how to program scripts.

The list.txt (for example)

Ariel Connor
Jack Smith
John Smith

the account's names, after using the script (first letter of the name + last name and add a number if the username exists on /etc/passwd, to avoid collisions). Again, I only need to process the names, because the groups and shells (among the other stuff) are all the same.

aconnor
jsmith
jsmith02

Any help would be greatly appretiatied.

Last edited by kjon (2009-08-14 16:30:33)


They say that if you play a Win cd backward you hear satanic messages. That's nothing! 'cause if you play it forwards, it installs windows.

Offline

#2 2009-08-15 10:04:37

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,231
Website

Re: script for automating account creation

Partially tested...

#!/bin/bash

LFILE='/tmp/list.txt'
IFS='
'

for NAME in $(cat $LFILE) ; do
   FNAME=`echo $NAME | awk '{ print $1 }'`
   LNAME=`echo $NAME | awk '{ print $2 }'`
   UNAME="${FNAME:0:1}${LNAME}"
   UNAME=`echo $UNAME | tr A-Z a-z`

   C=1
   NNAME=$UNAME
   EXISTS=`grep -E '$UNAME' /etc/passwd`
   while [ ! -z "$EXISTS" ] ; do
        echo "found existing user $NNAME"
        if [ ${#C} -eq 1 ] ; then
                NNAME="${UNAME}0${C}"
        else
                NNAME="${UNAME}${C}"
        fi
        EXISTS=`grep -E '$NNAME' /etc/passwd`
        C=$((C+1))
   done

   useradd $NNAME
done

exit 0

Offline

#3 2009-08-16 14:48:15

kjon
Member
From: Temuco, Chile
Registered: 2008-04-16
Posts: 398

Re: script for automating account creation

okay, thanks fukawi2. I'll try your approach (although I already created the users by hand tongue -- my hands are still in pain). Thanks a lot again for your help.

Last edited by kjon (2009-08-16 14:48:49)


They say that if you play a Win cd backward you hear satanic messages. That's nothing! 'cause if you play it forwards, it installs windows.

Offline

Board footer

Powered by FluxBB