So we’re going to add multiple users and it’s boring to write useradd -p <password> -c <name> <username> on every user we want to add. why don’t we try to make a script that add users listed in a file?

Lets take a look on how it’s possible to add users from a file with a little shell script.

First we make the file with users, in the file we type username password and name separated with , and newlines

ex:

Username1,pass1,full name1
Username2,pass2,full name2
username3,pass3,full name3
username4,pass4,full name4
username5,pass5,full name5

Save the information in a file called users.txt, then create a new file called userscript.sh and open it in your editor.

Since it’s a shell script we need to tell the interpreter that it should interpret this file as a /bin/sh script, this can be done by writing:

#!/bin/sh

Then we’re all set to start writing the script, first of we need to think of what the script should do. Should it be possible to delete users from file ? In my example I will give the administrator a choice to add or delete users from file. Since we dont want to make 2 separate files with useradd script and userdel script we need to make functions and process correct function by options.

The first function we write is the add function:

1
2
3
4
5
6
7
8
9
10
11
add()
{
	while read line
	do
		username=`echo $line | cut -d',' -f1`
		password=`echo $line | cut -d',' -f2 | mkpasswd -H md5 -s`
		name=`echo $line | cut -d',' -f3`
		sudo /usr/sbin/useradd -c '$name' -p $password $username
	done &lt; $1
	exit 1
}

Okay, so what does this function actually do ? (described by line numbers)

  1. Define the function
  2. Starts the function
  3. Starts a loop which will loop through the file given as the first parameter line by line
  4. Do the following on every line in the file
  5. Create a variable named username, the variable should contain the info from line start to the first “,”
  6. Create a variable named password, the variable should contain info from the first “,” to the second “,” and encrypt it to md5
  7. Create a variable named name, the variable should contain info from the second “,” to the third “,”.
  8. Force the /usr/sbin/useradd command to be run with root privileges
  9. End while loop when every line given as the first parameter is processed
  10. Exit the shell script with no errors
  11. End function

As you can see we use different shell commands in order to extract and process the information we want. The cut command cuts a string on a given delimiter (-d option), and -f says which cut i want (f1 first cut, f2 second cut etc).

Then we need to start with the delete function

del()
{
	while read line
	do
		username=`echo $line | cut -d',' -f1`
		sudo /usr/sbin/userdel $username
	done &lt; $1
	exit 1
}

I’m not going to explain this function as thorough as i did with the add function. The only difference is that we don’t need to get the password or name in order to delete, then we run the userdel command instead of the useradd command.

We also need to write a little help function in order to show the user how to use this little script, this is a simple functions which echo how to use the program.

help()
{
	echo "usage: -a -d -h <file>"
	echo "    -a add users listed in file"
	echo "    -d delete users listed in file"
	echo "    -h print help"
	exit 1
}

finally we only have to write some code to get the users options and process the right function.

while getopts adh opt
do
	case ${opt} in
		a ) add ${!#};;
		d ) del ${!#};;
		h ) help;;
		\? ) help;;
	esac
done
 
help

This code loops through the arguments the user gave and run the appropriate function, if the user specify a option which doesn’t exist the help menu will be displayed, this also happens if the user doesn’t type a option at all.

You can use this script by opening a terminal and navigate to the correct folder and write “sh userscript.sh -a users.txt”, note that you have do be logged in as a user who have sudo privileges or as root in order to create the users.
It would be smart to add a echo command saying which or how many users that where added/deleted.

Full source can be viewed/downloaded here:
Users.txt
Userscript.sh

One Comment

  1. Hubert says:

    A great secret of success is to go through life as a man who never gets used up.

Leave a Reply