Link to home
Start Free TrialLog in
Avatar of leotron
leotron

asked on

useradd automation script

I have a solaris 10 server and need to add 30 users, I have tried to go through the smc and have it creat the users for me in batch form, but for some reason that is not working. I would like to have the users created though a perl script, please help on this. Thank you
Avatar of Tintin
Tintin

Say you have the 30 users listed in a file, then you can do something like:

#!/bin/sh

for user in `cat /tmp/user.list`
do
  useradd -m $user
done

Now you'll probably want to adjust the above script to add various options to useradd for groups, expiry etc.

Avatar of leotron

ASKER

i do not want to use a list though I would like the script to ask for a starting string to use and then simply append a number to that starting sting and from there create 30 users.
#!/bin/ksh
let seq=1
echo "Enter starting string: \c"
read start

while [ $seq -le 30 ]
do
  useradd -m $start$seq
  let seq="seq + 1"
done
Avatar of leotron

ASKER

ok here is a sample of my script.

[BEGINNING OF CODE]

#!/usr/bin/perl -w

print "How many users would you like to add? [1 - 50]";
chomp($numofUsers = <>);
print $numofUsers;

print "What group would do you want to put the user in? [nobody]";
chomp($GROUP = <>);
print $GROUP;
                        
print "Where should the Home directory be created? [\/opt\/SUNWbb\/root\/home\/]";
chomp($HOME = <>);
print $HOME;
            
print "What userid do you want to start at [150000]?";
chomp($UID = <>);
print $UID;

print "What would you like the password to be?";
chomp($PASSWD = <>);
print $PASSWD;

print "What prefix do you want to use? [gc]";
chomp($PRE = <>);
print $PRE;

print "Check that the following is correct.\n";
print "------------------------------------\n\n";
print "Number of user to create: \t$numofUsers\n";
print "Home directory is: \t\t$HOME\n";
print "The starting userid is: \t$UID\n";
print "The password is: \t\t$PASSWD\n";
print "The group is:\t\t\t $GROUP\n\n";
print "Is this correct?";
chomp($ans = <>);

if ($ans == "y")
      {
            for ($num = 0; $num <= $numofUsers; $num++)
                  {
                        $name = $PRE . $num;
                        print "$name\n";
                        print "$UID\n";
                        print "$HOME\n";
                        print "$PASSWD\n";
                        print "$GROUP\n\n";
                        useradd -u $UID -g $GROUP -d $HOME -p $PASSWD $name
                        $UID++;
                  }
      }

[END OF CODE]

and here is the output that I am getting.

[START OF OUTPUT]

Scalar found where operator expected at useradd.pl line 68, near "$PASSWD $name"
      (Missing operator before $name?)
Scalar found where operator expected at useradd.pl line 69, near "$name
                        $UID"
      (Missing operator before $UID?)
syntax error at useradd.pl line 68, near "useradd -u "
useradd.pl had compilation errors.
Exit code: 9 , 0009h
2 error(s), 0 warning(s)

[END OF OUTPUT]

what am i doing wrong?
ASKER CERTIFIED SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of leotron

ASKER

Perl is the only thing I know how to use currently, I am still new at perl and unix in general now what I did not sure if it was correct thoug
is this

`useradd -u $UID -g $GROUP -d $HOME -p $PASSWD $name`;

and this created the users, but the password does not work is there a reason for this?
Generally shell scripts are better suited to these tasks as you aren't really making use of any Perl features, but if Perl is what you're familiar with, there's no reason why you shouldn't use it.

Using backticks is not the correct way of running an external command.  backticks are for capturing output.

You should be using system (as I've shown).

The -p option to useradd is to set the project name not the password.

To set the password, you're going to need to run an Expect script.  You can either do this with TCL/Expect, or there is a pure Perl version of Expect.

http://search.cpan.org/~rgiersig/Expect-1.20/Expect.pod

A couple of points with your Perl script

if ($ans == "y")

should be

if ($ans eq "y")

== is for numeric comparisons.

for ($num = 0; $num <= $numofUsers; $num++)

is better written as

for $num (0..$numofUsers)

Also, you should get into the habit of starting all your perl scripts with:

#!/usr/bin/perl
use warnings;
use strict;

In particular

use strict;

will force you  to do declare all your variables, eg:

my $num=0;

which will save you a lot of headaches when you try debugging scripts that have silly typos in them.



Avatar of leotron

ASKER

now could you explain the differance in my $num and simply $num? thank you for the info you have been very helpful
You can use script to create user, set password and login ENV in one go, please have a look at the answer in http:Q_21513242.html

and
also the script in:
http:Q_20867316.html
When you have

use strict;

in place, you need to declare all your variables.

So instead of doing

$num=0;

you do

my $num=0;

Using strict mode, will catch silly typos like the following example, which can be a pain to debug sometimes (particularly in a large script)

#!/usr/bin/perl
$num=0;

$num++;

print "Total is now $nun\n";

without strictures on, the above code is syntactically correct, but obviously $nun should be $num.

With

use strict;

in place, you'll get an error alerting you to the fact that $nun hasn't been declared.
Avatar of leotron

ASKER

Thank you or the advise I appreciate this a lot. I have read one book on Perl programming which is what got me started, but I would like to learn more and be more profecieant at this, also I would like to learn shell programming, do you have any suggestions?