Search code examples
bashaccountcreation

Bulk account creation from text file


I have the following script, it reads from users.txt the first and second fields and uses them to generate the username and password and creates the accounts for each line. the problem is that the script is only creating accounts for the first 2 lines and not for the rest

enter image description here

#!/bin/bash
FILE=/home/knoppix/users.txt
USERSH=/bin/bash


while IFS=":" read GECOS USRGRP ; do

groupadd -f $USRGRP



USERNM="u$(cat /dev/urandom| tr -dc '0-9' | fold -w 6| head -n 1)"
USERPW=$(cat /dev/urandom| tr -dc 'a-zA-Z0-9' | fold -w 6| head -n 1)


useradd $USERNM -p $USERPW -g $USRGRP -c "$GECOS,$USRGRP" -d $HOME/$USERNM -s $USERSH -m

ACCNT=$(grep $USRNM /etc/passwd)
echo "${tgrn}Account creation successful!${tr}"
echo "$ACCNT"
echo "Credentials"
echo "${tred}Username:${tr} $USERNM ${tred}Password:${tr} $USERPW"
echo
done < $FILE

Solution

  • #!/bin/bash
    
    while IFS=: read GECOS USRGRP; do
    
        # your groupadd and useradd commands here
    
    done < /home/knoppix/users.txt