LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Adding users via a script (https://www.linuxquestions.org/questions/linux-server-73/adding-users-via-a-script-752124/)

kchakrak 09-02-2009 10:49 AM

Adding users via a script
 
Is it possible to design a matrix (such as a csv) that the users can fill in with all their appropriate details and then can be directly imported? just to make adding endless numbers of users easy.

If this is not possible then is there any way of making adding large numbers of users a quicker task?

Using Ubuntu 8.04 server.

forrestt 09-02-2009 11:07 AM

Try

man useradd

I don't think it will read users from a file, but it shouldn't be too hard to do what you want w/ a simple script and that command.

HTH

Forrest

MS3FGX 09-02-2009 11:08 AM

I don't know if there is an existing script or program to do what you want, but you could certainly create a Bash script that would feed values from a .csv file into "useradd".

I checked on a Google a bit and found that this is not an uncommon request. It seems there are some scripts out there already that do what you want.

Disillusionist 09-02-2009 11:45 AM

Perl script from the following LQ post: http://www.linuxquestions.org/questi...g-ftp-740675/?

The example script was designed to take a space delimited text file.

kchakrak 09-03-2009 03:27 AM

Wow, thanks guys. Much appreciated. I'll get to trying various methods out and finding out what suits me best.

Thanks again,

netrabhatta 09-03-2009 05:49 AM

Users can be created by script. First u need to create file containing the list of users with password, uid, gid, home directory, login shell.
Then command newusers is used to read that file and take action as u need.

1. Create File
USERNAME:PASSWORD:UID:GID:COMMENT:HOMEDIR:LOGINSHELL

# vim userlists
netra:redhat:1052:100:I m netra:/home/netra:/bin/bash

# newusers userlists

I think this would help.

GaHillBilly 10-26-2009 08:48 PM

More ways to create batches of Linux & Samba users by script. . . .
 
1. The Webmin system management tool allows you to import batches of users, either to create users or to modify existing users. Fields are colon separated. Webmin also has some sort of tool for syncing Linux and Samba users, but I haven't tried it. (In years past, Webmin has hosed my Samba config. It may be fine now, but I'm not inclined to try it.)

2. Also, I have a Bash script, that I've cobbled together from bits and pieces I found, which 'works for me' to simultaneously create Unix and Samba. It creates MD5 encrypted passwords which seems to allow long passwords, rather than the 8 character passwords truncated by "crypt". There may be mistakes or other problems, so I would appreciate feedback from you shell wizards out there. I know there are shorter ways to do it, but it seems to work OK and I can read it, so I'm not real interested in more 'elegant' solutions. But, if there are problems, I'd very much like to correct them.

GaHillBilly

==============================================================
#!/bin/sh

# Script to add users to Linux and Samba
echo " "
echo "**************************************"
echo " This script takes a list of usernames, groups, and plaintext passwords from"
echo " a file named 'userlist' and located in the same directory as the script, and"
echo " from this file creates Linux and Samba users."
echo ""
echo " It can ONLY be run by root, and will fail if another user attempts to run it."
echo ""
echo " Values in 'userlist' must be separated by a single space character; also, the"
echo " last record (line) in that file must be empty. If it's not, the last user in"
echo " the list will not be added. Note that the script prints only the 1st 4 characters of"
echo " the user plaintext password, to avoid leaving the full password in Bash history. "
echo " For more security, simply comment that line out of the script."
echo ""
echo " The REQUIRED data order is: name group password UIN directory shell samba "
echo " Three lines of SAMPLE data: "
echo "******************************************"
echo "user1 smbgrp user1pass 501 /home/smbusers/user1 /bin/sh 1"
echo "user2 lnxgrp user2pass 521 /home/lnxusers/user2 /bin/sh 0"
echo "user3 lnxgrp user3pass 522 /home/lnxusers/user3 /bin/false 0"
echo "" # empty line!
echo "******************************************"
echo " where samba is '0' or '1'. If 'samba' is '1', then a Samba user and"
echo " password will also be created. "
echo ""
echo "**************************************"
echo ""

# Give ROOT user a chance to read info and make sure he/she wants to run script.
# Comment the line below, and uncomment the next line when you get tired of the pause.

read -p "yes or no?" response
# response="yes"

if echo $response | grep "y" >/dev/null
then
echo ""
echo "Will continue"
echo ""
else
echo ""
echo "Bailing out!"
echo ""
exit 0
fi

# Check user -- must be root!

if [ $(id -u) -ne 0 ]; then
echo "**************************************"
echo "Only root may add a user to the system"
echo "**************************************"
exit 2
fi

# Users to be created.
new_users="userlist"
echo Reading users from file $new_users
echo ""

# *****************************************************************
# Loop through user data
echo "*************************************"
cat ${new_users} | \
while read username group plainpass uin dir ushell samba; do
echo "**************************************"
echo ""
echo "User: $username Group: $group UID: $uin **"
echo "Pass: $plainpass " | head -c10
echo ""
# make random salt for openssl
rand=$(cat /dev/urandom | tr -dc A-Za-z0-9 | head -c8)
# encrypt via openssl using MD5 -- allows long passphrases, but no spaces in this script.
encpass=$(openssl passwd -1 -salt $rand $plainpass)
echo "Home: $dir Shell: $ushell Encrypted password: $encpass Samba?: $samba ***"
echo " "
# *****************************************************************
# Add groups as needed.
#
egrep "^$group" /etc/group >/dev/null
if [ $? -eq 0 ]; then
echo "Group $group exists, and does not need to be created!"
else
groupadd -g $uin $group
fi

# *****************************************************************
# Add matching Samba users.

useradd -m -p $encpass -u $uin -g $group -s $ushell -d $dir $username
[ $? -eq 0 ] && echo "User $username has been added to the system!" \
|| echo "Failed to add user $username!" \


if [ $samba -eq 1 ] ; then
(echo $plainpass; echo $plainpass) | smbpasswd -as $username
echo "User $username added to Samba"
else
echo "Not adding user $username to Samba"
fi
echo ""
echo "**************************************"

done


All times are GMT -5. The time now is 07:00 AM.