Nothing To Lose

If you don’t have it, how can you lose it!
Subscribe

Adding Multiple Users With Random Password in Linux

March 19, 2011 By: Dexter Category: BASH, Linux, Linux Commands, Shell Scripting

Some or the other time you will come across a need to add multiple users to your Linux system, if it is 5 to 10 its ok, but what if you need to add a list of 100 or 200 users at a time.

What you need is a text file containing your username (Login ID) and random password separated by , (comma) stored in a file. I am calling this file uidpass.csv.

Loginid,Password
2001,k8jr9yic
2002,agiidmcc
2003,mp9cxmzj
2004,e3mjyxcb
2005,vlsvc3yc
2006,a9kelmhy
……………..
……………
2299,54wgh67b

Save this file as whatever name you want. Now what we need a is command to add users to the system. The useradd command in Linux lets you do that, intrestingly it also allows you to add the password with the option -p at the same time no need to use the passwd command. The only problem with the -p option is that it need the password to be provided in encrypted format.
Here the utility mkpasswd comes in handy. The mkpasswd command can take a string can return encrypted value for it that is required by the password option of useradd.

Here is the script which will read the file containing the userid,password

#!/bin/bash
for lines in `cat uidpass.csv`
do
userid=`echo $lines |cut -d’,’ -f1`
userpw=`echo $lines |cut -d’,’ -f2`
useradd -mc”New User $userid” -s /bin/bash  -p `mkpasswd $userpw`  $userid
done

So whats happening here lets see line by line

for lines in `cat uidpass.csv`
start the for loop read every line in uidpass.csv and put ever line one by one in lines variable for every iteration.

userid=`echo $lines |cut -d’,’ -f1`

cut the line in variable lines on the delimiter comma and store first field in variable userid

userpw=`echo $lines |cut -d’,’ -f2`

cut the line in variable lines on the delimiter comma and store second field in variable userpw

useradd -mc”New User $userid” -s /bin/bash  -p `mkpasswd $userpw`  $userid

Run the user add command for the username in userid and password in userpw, in the line itself the mkpasswd will encrypt the userpw and then give it to the option password option.

The loop will run for all the lines in your file.

hope this helps.

Catching email id’s from file(s) using grep and other utils

April 18, 2009 By: Dexter Category: BASH, Linux Commands, Regular Expressions, Shell Scripting, Tutorial

Here is a simple mechanism that you can use to collect all the email id(s) from a file(s) into a single file. To do this we will be using the following command cat , grep, sort and uniq.

This one liner should do the work

cat file | grep -io ‘\<[^-.][0-9A-Za-z\.\-\_]\+@[0-9A-Za-z.]\+\>‘ | sort | uniq

If you want all the id’s in some file then redirect the above command to a file.

cat file | grep -io ‘\<[^-.][0-9A-Za-z\.\-\_]\+@[0-9A-Za-z.]\+\>‘ | sort | uniq > mailid.txt

Now lets convert this into a shell script where we shall accept a directory name from the user. This directory will be the one containing the files having the email ids
You can download the script from here Script to retrieve Email ids form files in a directory
I have noticed the copy paste of the code below is not working because of formatting characters.

#!/bin/bash
clear
echo -n “Enter the name of a DIRECTORY from where you want to pick up email id’s: “;
read dirname;

# check if the entered name is a directory

if [ -d $dirname ];then

cd $dirname; #  if it exists change to the directory

else

echo “+============================+”

echo “| Check your directory name! |”

echo “+============================+”

exit 1;

fi

# Loop through all file  in the given directory

for files in *

do

if [ ! -d $files ];then

# process all files and store them in a temporary file in users home dir

echo “Processing file $files”;

cat $files | egrep -io ‘\<[^-.][0-9A-Za-z\.\-\_]+@[0-9A-Za-z.]+\>‘ >> ~/$$;

echo “Processed”;

fi

done

cd -  # get back to previous working dir, i am assuming it was home

# sort the emails ids in the file, remove duplicates and store in a final file.

sort ~/$$ | uniq >> emailids.$$

# remove the temporary file

rm ~/$$

# tell the user where the mail ids are stored

echo

echo “+=================================================+”

echo ” Your email ids are available in ~/emailids.$$ ”

echo “+=================================================+”

exit 0;

Well I should warn you, the regular expression will catch anything that looks like an email id, so you might end up having lots of things that looks like an email id.
[end]