Operating System - HP-UX
1827452 Members
4643 Online
109965 Solutions
New Discussion

Re: How do you add users from a file ?

 
SOLVED
Go to solution
Stuart Abramson_2
Honored Contributor

How do you add users from a file ?

I have a list of users from server "A" that I want to add to server "B". I have them from /etc/passwd:

xxx:N/ySsbm7JksgM:108:20:Nick smith,LOC MIS,,:/home/xxx:/usr/bin/ksh
yyy:3TZhn7hgulQyM:113:108:Tom jones,PTD ,,:/home/yyy:/usr/bin/ksh
zzz:pxLMqyNjqx1Wk:114:105:Mike greeb,WSD MIS,,:/home/zzz:/usr/bin/ksh
...
more...

What's the quickest way to add them to server "B"? Isn't there a command to add users (I normally use SAM for one user at a time.)
5 REPLIES 5
Stefan Farrelly
Honored Contributor

Re: How do you add users from a file ?

/usr/sbin/useradd
Im from Palmerston North, New Zealand, but somehow ended up in London...
Robert-Jan Goossens
Honored Contributor
Solution

Re: How do you add users from a file ?

Re: How do you add users from a file ?

/usr/bin/cat /etc/passwd |egrep "1003 (Group Id) " | cut -d : -f 1 >/home/xyz/users
for i in `cat /home/xyz/users`

do
remsh $(Remote host) "/usr/sbin/useradd -g "groupname" -d /home/"$i" -m "$i""
done
Paul Cross_1
Respected Contributor

Re: How do you add users from a file ?

NIS... or LDAP. or # cat file1 >> file2
Sridhar Bhaskarla
Honored Contributor

Re: How do you add users from a file ?

Hi Stuart,

The following approach may be safer as it basically uses user*|group* commands. Take backup of your /etc/passwd and /etc/group files before you attempt to do anything. A make_tape_recovery may help you in case if you get into trouble.

I am putting some scripting here but that is not tested completely. It's only to give you an idea.

Basically this is looking like a passwd file. So, you can grab all the fields and use them with useradd command. Set the password using usermod.sam command. This approach will work whether this system is trusted or not. However, there are couple of things to be done before you can actually use useradd command.

1. Find out all the groups that may be new to the system.

//
for GRP in $(awk '{FS=":";print $4}' your_file|sort |uniq)
do
grep -q ":${GRP}:" /etc/group
if [ $? = 0 ]
then
echo "$GRP" >> group.exists
else
echo "$GRP" >> group.create
fi
done
//

group.create file contains all the groups that have to exist before you can add the users. Modify the file to look like this

108:group1
105:group2

etc., and run

//

while read LINE
do
GID=$(echo $LINE|awk '{FS=":";print $1}')
GROUP=$(echo $LINE|awk '{FS=":";print $2}')
groupadd -g $GID $GROUP
done < group.create

//

2. Once the groups are created, then you run the following script to add the users. The errors and output of the script will be logged into useradd.log. You can view it and see the errors and fix them. Since you already have the encrypted passwords, you can usermod.sam to add them into password registry.

//
while read LINE
do
USER=$(echo $LINE |awk '{FS=":";print $1}')
PASS=$(echo $LINE |awk '{FS=":";print $2}')
UID=$(echo $LINE |awk '{FS=":";print $3}')
GID=$(echo $LINE |awk '{FS=":";print $4}')
GECOS=$(echo $LINE |awk '{FS=":";print $5}')
HOUSE=$(echo $LINE |awk '{FS=":";print $6}')
SHELL=$(echo $LINE |awk '{FS=":";print $7}')
printf "%-30.30s" "Working on $USER "
useradd -u $UID -g $GID -d $HOUSE -s $SHELL -c "$GECOS" -m -k /etc/skel $USER >> u
seradd.log 2>&1
RESP=$?
if [ $RESP = 0 ]
then
/usr/sam/lbin/usermod.sam -p "$PASS" $USER
STATUS="-DONE"
else
echo "Addtion of $USER failed with return code $RESP" >> useradd.log
STATUS="-FAILED"
fi
printf "${STATUS}\n"
done < your_file
//

-Sri

PS: Use it at your risk.
You may be disappointed if you fail, but you are doomed if you don't try