Linux.com

Home Linux Community Community Blogs General Linux Bash Script (with Perl) to Bulk useradd to server

Bash Script (with Perl) to Bulk useradd to server

The script below will read through file usersfile.txt and then add users to a linux system with perl installed on it.

the script reads line by line through usersfile.txt which is space separated like so

User1 passwd1 group1

User2 passwd2 groupx, and so on.

You need to supply passwords in plaintext in this file, so I only suggest keeping the file on a server for the duration of the script run.

The plaintext password then uses the perl crypt library alongside the standard useradd script.

 

#!/bin/bash
if [ $(id -u) -eq 0 ]; then
NEW_USERS="/usersfile.txt"
HOME_BASE="/home/"
cat ${NEW_USERS} | while read USER PASSWORD GROUP
do
pass=$(perl -e 'print crypt($ARGV[0], "password")' $PASSWORD)
egrep "^$USER[0]" /etc/passwd > /dev/null
if [ $? -eq 0 ]; then
echo "User $USER exists!"
exit 1
else
useradd -g ${GROUP} -p ${pass} -m -d ${HOME_BASE}${USER} ${USER}
[ $? -eq 0 ] && echo "User $USER has been added to the system" || echo "Failed adding user $USER!"
fi
done
fi

 

Comments

Subscribe to Comments Feed

Upcoming Linux Foundation Courses

  1. LFD331 Developing Linux Device Drivers
    25 Aug » 29 Aug - Virtual
    Details
  2. LFS422 High Availability Linux Architecture
    08 Sep » 11 Sep - Raleigh, NC
    Details
  3. LFS426 Linux Performance Tuning
    08 Sep » 11 Sep - New York
    Details

View All Upcoming Courses


Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board