Hi,
Im trying to create regular user accounts from a text file. I have a text file with a list of users that need to be created. Can I use a script to do this? Thanks
Printable View
Hi,
Im trying to create regular user accounts from a text file. I have a text file with a list of users that need to be created. Can I use a script to do this? Thanks
OK...lets say your list looks like this and are called users.txt and are located in /tmp do like this
File format:
The program below creating the accounts...Code:nisse1
user1
user2
user3
user4
user5
user6
Code:#!/bin/sh
#
# Creator:andutt@linuxforums.org
#
# Creates users from a file
#
FILE=/tmp/users.txt
if [ ! -f $FILE ]
then
echo "ERROR $FILE doesnt exist.."
exit 1
else
echo "Creating user accounts from $FILE"
fi
USERS=`cat $FILE`
for i in $USERS
do
echo "Creating user:$i"
useradd -g users -m -c "Autocreated account $i" $i
done
echo
echo "Usercreation is completed"
echo
You still end up manually setting the passwords. That's the worst part.
That can be rather easily remedied, though.
Use an input file of this format instead:
Then use the following script.Code:user1
pass1
user2
pass2
...
As you can see, this script also supports generation of 8-character random passwords by using the -r switch. In that case, the input file format is similar to what andutt suggested and it outputs the passwords to a file called "passwords". It also supports multiple input files and usage of stdin. For example, the following usage might be convenient:Code:#!/bin/sh
if [ $# -lt 1 ]; then
echo "Usage: $0 [ -g grpname ] [ -r ] filename ..." >&2
exit 1
fi
correct=
group=users
getpwd=y
while [ $# -ge 0 ]; do
o="$1"
shift
case "$o" in
-g)
group="$1"
shift
;;
-r)
if [ -z "$getpwd" ]; then
getpwd=y
else
getpwd=
fi
;;
*)
correct=y
if [ "$o" != - ]; then
exec 3<"$o"
else
exec 3<&0
fi
while :; do
read username <&3
if [ -n "$getpwd" ]; then
read password <&3
else
password="$(dd bs=6 count=1 </dev/urandom 2>/dev/null | mimencode)"
echo "$username $password" >>passwords
fi
if [ -z "$username" ]; then break; fi
useradd -g "$group" -m "$username"
passwd --stdin "$username" <<<"$password"
done
exec 3<&-
;;
esac
done
if [ -z "$correct" ]; then
echo "No users were created" >&2
fi
Note, however, that I haven't actually tried this script; I just wrote it down now. Therefore, check it through for typos before using it in a real environment.Code:awk '{if($1 == "thisschool") print $2;}' | ./genuser -g teachers teachers.in -g students -r -
Thanks a lot for your help, the scripts worked great.