Tuesday, September 30, 2008

Creating multiple users through scripts

Creating a user account may seem fairly simple. However, if you consider creating multiple user accounts, it will be tedious and time-consuming to do them one by one. Thus, the need to find a script that will make it possible to create multiple user accounts in a batch. This is very helpful in organizations such as a university or a large corporation.

Using Google, I typed certain keywords like "create multiple users" +"shell scripts." Among the results generated, I chose these two methods mentioned below.

The first method is applicable to Linux, and the article can be found here.

Using the chmod command:

# touch /root/batch-user-add.txt
# chmod 0600 /root/batch-user-add.txt


After creating a user list, open the file:
# vi /root/batch-user-add.txt

Appending the user name and password, type the following commands:
user1:password:1001:513:Student Account:/home/user1:/bin/bash
user2:password:1002:513:Sales user:/home/user2:/bin/bash
user100:password:1100:513:Sales user:/home/user100:/bin/bash
tom:password:1110:501:Guest Account:/home/guest:/bin/menu
jerry:password:1120:501:Guest Account:/home/guest:/bin/menu


Finally, create the users in batch:
# newusers /root/batch-user-add.txt


(I think it's a rather straightforward and simple way of creating multiple user accounts, but I'm not entirely sure because I don't have Linux to test it with. For those of you who have Linux, please try and let me know if it works by leaving a comment on this blog post. Thanks!)

The other method, which uses shell scripts, was posted in a forum found here. From what little programming knowledge I have, what I understand from this script is that while it satisfies the condition set in the given parameters, then it will do the commands that are written following the "do" command.

Okay, so maybe showing you the script is easier to understand, so here it goes:
#!/bin/sh

ALPHA=$1
count=$2
NUM2=$3
UID=1002

while [ $count -le $NUM2 ]
do
echo "$1$count:x:$UID:1:User $1$count:/export/home/$1$count:/bin/sh" >> /etc/passwd
       echo "$1$count:WafjZ5pUd5j7U:::::::" >> /etc/shadow
mkdir /export/home/$1$count
cp /etc/skel/local.login /export/home/$1$count/.login
cp /etc/skel/local.profile /export/home/$1$count/.profile
cp /etc/skel/local.cshrc /export/home/$1$count/.cshrc
chown -R $1$count:1 /export/home/$1$count
count=`expr $count + 1`
UID=`expr $UID + 1`
done

Personally, I find this second method more difficult just because I am unfamiliar to the "pound bang slash bin" characters (#!/bin). But you know, whatever works is fine by me.... (c",)

No comments: