Hey guys!

Finally, I found time to update this lovely blog of mine. And luckily, I have one super cool simple script to share. This script will help a lot of system administrators in adding user accounts, very useful if you have about a hundred servers to login to and execute useradd and passwd again and again. This is a bash script, so no need to install packages and easy to understand.

Here goes the code.


#!/bin/bash

for USER in $(cat /tmp/users.txt) do
$(useradd $USER)
###Remove the space between 8 and )
PASSWORD=$(openssl rand -base64 8 )
###passwd [double dash] stdin
echo $PASSWORD | $(passwd –stdin $USER)
echo “$USER $PASSWORD” | mail -s “Your Account Info” $USER@company.com
sleep 3
done

Here is a quick summary of the code per line:

for USER in $(cat /tmp/users.txt) do
This line begins the for loop and /tmp/users.txt should be a text file containing the list of usernames to be added one username per line. The value of $USER

$(useradd $USER)
This line will begin by executing the command useradd. If the script fails at this point because the username already exists, it will proceed with changing the password of the username.

###Remove the space between 8 and )
###passwd [double dash] stdin
PASSWORD=$(openssl rand -base64 8 )
echo $PASSWORD | $(passwd --stdin $USER)

This line generates an 8-byte random password using openssl and pipes it to command passwd using –stdin option so the generated password can be passed.

echo “$USER $PASSWORD” | mail -s “Your Account Info” $USER@company.com
This line sends an email to the user containing the username and password, assuming that the email address is the same as the username.

This script can be further enhanced (imagination is limitless!) and feel free to share your script here.

Popularity: 11% [?]

Share and Enjoy:
  • E-mail this story to a friend!
  • StumbleUpon
  • Digg
  • Technorati
  • del.icio.us
  • Reddit
  • Facebook
  • Google
  • Slashdot
  • Blogosphere News
  • TwitThis
  • NewsVine
  • Propeller
  • Furl
  • Simpy
  • Spurl

Related Posts