Pages

Saturday, January 30, 2010

Generate randum password using a shell script

Some times when creating bulk users, admins are forced to create complex passwords.. but how to do it.. how many you can set manually? Let me put it in this way.. for one or two users we can think for a randum password and assigne to them.. but if its 100 users? Naaa.. i can not think for 100 passwds..
So leave this job to Linux to create randum passwords..
you use below command to generate randum passwords
< /dev/urandom tr -dc A-Na-n1-9_ | head -c8
This will just generate randum 8 lenght password
for example
see just typing the above command will generate a randum password
root@ps6061:~#<  /dev/urandom tr -dc A-Na-n1-9_ | head -c8
57fg67gKroot@ps6061:~#<  /dev/urandom tr -dc A-Na-n1-9_ | head -c8

FN8ahe8broot@ps6061:~#<  /dev/urandom tr -dc A-Na-n1-9_ | head -c8

eLhLLNCGroot@ps6061:~# < /dev/urandom tr -dc A-Na-n1-9_ | head -c8

FK3CC9GNroot@ps6061:~#< /dev/urandom tr -dc A-Na-n1-9_ | head -c8
cA8H24Mlroot@ps6061:~#<  /dev/urandom tr -dc A-Na-n1-9_ | head -c8
9eDL39I_root@ps6061:~#  
Here is a script written by me to automate entire 20 user account creation..
#!/bin/bash
#Author:Surendra Kumar Anne
#Purpose:To automate user creation
#Date/Time:29-01-2010.19:10
mkdir -p /home/admin/useraccounts
for (( i=0; i<=20; i++ ))
do
useradd user$i
< /dev/urandom tr -dc A-Na-n1-9_ | head -c8 > /tmp/passwd.txt
cat /tmp/passwd.txt | passwd --stdin user$i
echo -e "Username:user$i" > /home/admin/useraccounts/user$i
echo -e "password:" >> /home/admin/useraccounts/user$i
cat /tmp/passwd.txt >> /home/admin/useraccounts/user$i
done
rm -rf /tmp/passwd.txt

Note:When password generating i just created a password which will not contain o(small ooo), O(capital OO), 0(zero). This is to remove confusions for users.. some times user will get confusion when using these characters. If you want all the characters from a to z, A to Z, and 0-9 just use below code..

< /dev/urandom tr -dc A-Za-z0-9_ | head -c8

0 comments:

Post a Comment