Solved

trying to add multiple users in a script either using user add or manually

Posted on 2004-10-06
9
304 Views
Last Modified: 2013-12-26
I'm trying to write a script that when called with 3 parameters as followed a letter,lower number id,max id will create generic user accounts.All acounts need to have passwords set to st3T63

example call   /home/john/acctgenerator s 120 140

would use s as the startig letter and create accounts s120,s121,.....s140

I'm thinking to try using useradd in the script however everything would go smoothly except how to set the password for each account. I'm not sure if this would be possible experts would i be better off opening up /etc/passwd doing everything manually?

this is what i'm thinking the script will look like in it's infancy.............


while [ $1 > $2 ]  <=== i think this grabs the first two parameters of the call...however are they strings i need 120 to 140 to int's here because thats how many accounts i'll need
do

echo useradd -u $0+$1 -c$0 -g11 -s/bin/ksh "$0+$1"
echo passwd st3T63

$1 = `expr $1 + 1` <== increment 120 to 121,.140
done

hows this logic look ?

any tips and hits would be appreciated
0
Comment
Question by:tyweed420
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 20

Expert Comment

by:dsacker
ID: 12241957
This example merely echoes the results, so you'll need to code your account setup where the echo is, but the following should work:

letter="$1"
start="$2"
end="$3"

while [ $start -le $end ]; do
    echo $letter$start
    start=`expr $start + 1`
done
0
 

Author Comment

by:tyweed420
ID: 12242406
how about useradd or manually write to /etc/passwd which would be easier because i need to insert a password anmd i don't think useradd will allow me to do this.
0
 
LVL 21

Expert Comment

by:tfewster
ID: 12242412
See http://www.experts-exchange.com/Operating_Systems/Linux/Q_20476614.html for how to do the password bit; It's not a trivial task (You daren't modify the password file manually in case it gets corrupted), but that link shows how to do it safely via a script
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Assisted Solution

by:dsacker
dsacker earned 25 total points
ID: 12242487
If you can pass those parameters via my example (instead of the echo $letter$start), you might be able to get there.
0
 

Author Comment

by:tyweed420
ID: 12273085
I need some help this is not working i get these following errors.

1. while [ -le ]  :Command not found
2. syntax error near unexpected token  'done'
3. 'done'


any ideas why??

i'm still trying to get this code to create users any help would be great
0
 

Author Comment

by:tyweed420
ID: 12273096
ok

while[ 1 -le 5 ]  is what i got when i used the parameters 1 5 but are they already integers that can be compared because it is giving me errors
0
 

Author Comment

by:tyweed420
ID: 12281234
I want to try to do this user addidn manually how does this look below. I'm having difficulty figuring out how to set the password

letter="$1"
start="$2"
end="$3"

while [ $start -le $end ]; do
vi /etc/passwd
echo "$letter$start:rattler:$start:10:"$letter$start":/export/home/$letter$start:/bin/ksh" >> /etc/passwd
pwconv
mkdir /export/home/$letter$start
cp /etc/skel/local.cshrc /export/home/$letter$start/.cshrc
cp /etc/skel/local.login /export/home/$letter$start/.login
cp /etc/skel/local.profile /export/home/$letter$start/.profile
chown -R $letter$start:10 /export/home/$letter$start
passwd $letter$start
type rattler somehow
start=`expr $start + 1`
done
0
 
LVL 21

Accepted Solution

by:
tfewster earned 25 total points
ID: 12281932
I think you're taking a step backwards by creating the user manually - And losing the safeguards that useradd gives you, e.g. passwd file locking & checking for duplicate userids;

#!/bin/ksh
letter="$1"
start="$2"
end="$3"

while [ $start -le $end ]; do
  useradd -u ${start} -g 10 -c "${letter}${start}" -m -d /export/home/${letter}${start} -s /bin/ksh ${letter}${start}
  # And other options, e.g. expiry

  chpass  ${letter}${start} rattler # This is yuzhs "expect" script from
                                                # http://www.experts-exchange.com/Operating_Systems/Linux/Q_20476614.html

  start=`expr $start + 1`
done
0
 
LVL 21

Expert Comment

by:tfewster
ID: 12281981
I'm guessing you're using Solaris - You can get expect (+ its dependencies, tcl, tk and libgcc)  from http://www.sunfreeware.com/

If you're using another version of Unix, let us know what it is and we'll find the "expect" binaries for you. Unless, of course, you prefer to compile them yourself...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
repeateFront java challenge 31 95
sumNumber challenge 16 140
format the code in java 6 109
no14 challenge 14 73
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question