Wednesday, October 13, 2010

Useradd shell script for Linux

Useradd shell script for Linux. You need to save usernames in /tmp/ulist. Username and password will be the same.

#!/bin/bash
#Purpose : Add multiple users with password
#Author   : Ranjith Kumar R
#Date      : 14th Oct 2010

if [ $(id -u) -eq 0 ]; then
for i in `cat /tmp/ulist`
do
egrep "^$i" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
                echo "$i exists!"
else
pass=$(perl -e 'print crypt($ARGV[0], "password")' $i)
useradd -p $pass $i
fi
done
else
echo "Only root may add a user to the system"
        exit 2
fi