Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

UserAdd script 2

Status
Not open for further replies.

stu78

Programmer
May 29, 2002
121
0
0
GB
Hi,

I want to automate a build, for this I would like to automate the user ( login shell ), GID, and group creations...

Has anyone got a script which does this?

Stu.

 
You probably don't need a script for this, provided your data is nicely columned in a file. eg:
<username> <uid> <gid> <otherGID,otherGID...> <homedir> <shell> <comment>

Then use the following:
Code:
cat <file> | while read user uid gid GIDS dir shell comment
do
  useradd -u $uid -g $gid -G $GIDS -d $dir -m -s $shell -c "$comment" $user
done
Note -m creates the home directory, and <comment> can contain spaces as the last variable in a read picks up the rest of the line. Also all fields must have the correct data as no error checking is done.

Just an idea. I hope it helps.

Mike
 
Hi Mike042

This is my script.

# vi addingusers
cat /etc/passwd | while read user passwd uid gid comment dir shell
do
useradd -p $passwd -u $uid -g $gid -c "$comment" -d $dir -s $shell
done
~
This is the display of /etc/passwd file.

# cat /etc/passwd
root:x:0:1:Super-User:/:/usr/bin/ksh
daemon:x:1:1::/:
bin:x:2:2::/usr/bin:
sys:x:3:3::/:
adm:x:4:4:Admin:/var/adm:
lp:x:71:8:Line Printer Admin:/usr/spool/lp:
uucp:x:5:5:uucp Admin:/usr/lib/uucp:
nuucp:x:9:9:uucp Admin:/var/spool/uucppublic:/usr/lib/uucp/uucico
smmsp:x:25:25:SendMail Message Submission Program:/:
listen:x:37:4:Network Admin:/usr/net/nls:
nobody:x:60001:60001:Nobody:/:
noaccess:x:60002:60002:No Access User:/:
nobody4:x:65534:65534:SunOS 4.x Nobody:/:
rao_sr:x:501:14:Srinivasa Rao Cherukuri:/export/home/rao_sr:/usr/bin/ksh

When executing the script I am getting errors.
# ./Useradd
UX: useradd: ERROR: invalid syntax.
usage: useradd [-u uid [-o] | -g group | -G group[[,group]...] | -d dir |
-s shell | -c comment | -m [-k skel_dir] | -f inactive |
-e expire | -A authorization [, authorization ...] |
-P profile [, profile ...] | -R role [, role ...]]
-p project [, project ...] login
useradd -D [-g group | -b base_dir | -f inactive | -e expire
-A authorization [, authorization ...] |
-P profile [, profile ...] | -R role [, role ...]] |
-p project
--------------------------------
------------------------------------
It goes like that

Where is the problem?

Thanks in advance
 
I'll take it that "# ./Useradd" is a typo as you called your script "addingusers".

If you put "set -x" at the top of your script and run your script again, it will show you how and with what your variables are replaced.

I think you need to replace the colons (":") with spaces in the passwd file first before you start your script, as the while expects a space as seperator.

Test it first, but I think it should look like this (added the sed command and changed the cat)

sed s/:/" "/g /etc/passwd > ./passwd.tmp
cat ./passwd.tmp | while read user passwd uid gid comment dir shell
do
useradd -p $passwd -u $uid -g $gid -c "$comment" -d $dir -s $shell
done
 
You are right It was a typo.

Pl have a look at the script called au meant for automating useradd process.

# vi au

sed s/:/" "/g /etc/passwd > ./passwd.tmp
cat ./passwd.tmp | while read user passwd uid gid comment dir shell
do
useradd -p $passwd -u $uid -g $gid -c "$comment" -d $dir -s $shell
done

Also have a look at passwd.tmp file

# cat /passwd.tmp
root x 0 1 Super-User / /usr/bin/ksh
daemon x 1 1 /
bin x 2 2 /usr/bin
sys x 3 3 /
adm x 4 4 Admin /var/adm
lp x 71 8 Line Printer Admin /usr/spool/lp
uucp x 5 5 uucp Admin /usr/lib/uucp
nuucp x 9 9 uucp Admin /var/spool/uucppublic /usr/lib/uucp/uucico
smmsp x 25 25 SendMail Message Submission Program /
listen x 37 4 Network Admin /usr/net/nls
nobody x 60001 60001 Nobody /
noaccess x 60002 60002 No Access User /
nobody4 x 65534 65534 SunOS 4.x Nobody /
rao_sr x 501 14 Srinivasa Rao Cherukuri /export/home/rao_sr /usr/bin/ksh
jdoherty x 502 14 James Doherty /export/home/jdoherty /sbin/sh

But /etc/passwd file is still with semicolons like

nobody4:x:65534:65534:SunOS 4.x Nobody:/:
rao_sr:x:501:14:Srinivasa Rao Cherukuri:/export/home/rao_sr:/usr/bin/ksh
jdoherty:x:502:14:James Doherty:/export/home/jdoherty:/sbin/sh

After executing ...

# ./au
UX: useradd: ERROR: invalid syntax.
usage: useradd [-u uid [-o] | -g group | -G group[[,group]...] | -d dir |
-s shell | -c comment | -m [-k skel_dir] | -f inactive |
-e expire | -A authorization [, authorization ...] |
-P profile [, profile ...] | -R role [, role ...]]
-p project [, project ...] login



Same error.

Is there any condition that all the entries in /etc/passwd and /etc/passwd.tmp file should be similar which is not in my case.

Thanks Please.



 
Is there any condition that all the entries in /etc/passwd and /etc/passwd.tmp file should be similar which is not in my case."

In the sense formatwise with similar entries like username,password etc. In the display I have just shown few lines from /etc/passwd file. Otherwise both the files are same with equal number of entries and of course with semicolons(/etc/passwd) and spaces (/etc/passwd.tmp).

Thanks
 
You only use the /etc/passwd file to create the correct input. The correct input is then stored in the passwd.tmp file which is then used in your script. Do NOT change anything in your /etc/passwd file!!!

Like I sugested, just put the line "set -x" at the top of you script and check its output. It will show you how the useradd command will be executed. You then need to check if this is the correct syntax.
Check if spaces are listed correctly. If the quotes are at the correct location.
The set -x should realy show you this!!

BTW. You have to be carefull just executing this script because before you know it you have created all kinds of users with the wrong info.
 
Hi shraddha3,

I don't understand why you would want to use the /etc/passwd file as input to this code (to add new users). One of the reasons for the errors is that -p is not for password in useradd, it is used for project (see man useradd), and another is that there is no loginame (username) in the posting of your script.

From stu78's initial enquiry, I thought he wanted to add (not modify) a list of new user accounts, so I suggested a file, eg:

rao_sr 501 14 1 /export/home/rao_sr /usr/bin/ksh Srinivasa Rao Cherukuri
jdoherty 502 14 1 /export/home/jdoherty /sbin/sh James Doherty

to automate the process. This was for new users, not to add existing users which get created during a Solaris installation - from root (uid 0) to nobody4 (uid 65534).

I hope this helps to clarify the situation.

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top