Sandeep Singh Cheema rearranged electrons thusly:

> i want to set predetermined passwords for some 150 users on my server. how
> can i automate the task? i heard expect can make interective scripts, but cd
> not get useful information on its useage.
 
 expect waits for user input - but I suggest you try something like this below
 (change example.com to your own domain's name).  Then have a separate file
 with usernames and passwords - and use foreach to feed the contents of the
 textfile to this script.
 
 usage - adduser <username> <passwd> [domainname] - if domainname is not given,
 the default domain (example.com) will be used.
 
        -suresh

#!/usr/bin/perl

if ($#ARGV < 1)
{
   print "Usage : adduser <USER> <PASSWORD> [<DOMAIN>]" . "\n";
   exit 999;
}

my $DOMAIN = "example.com";

if (@ARGV[2] ne "")
{
   $DOMAIN = @ARGV[2];
}
my $username = @ARGV[0];
my $password = @ARGV[1];
$passdir = "/etc/passwd";

open(PASSFILE,"$passdir") || die "Cannot Open File $passdir";
@lines = <PASSFILE>;
close(PASSFILE);

$found = 0;
        
open(PASSFILE,">$passdir");
foreach $line(@lines){
        if($line =~ /^$username/g){
                $newpass = crypt($password, substr($username,0,1) . substr($username, 
length($username) - 1,1));
                print PASSFILE "$username:$newpass\n";
                $found = 1;

        }
        else{
                print PASSFILE "$line"; 
        }
}
if ($found == 0) {
        $newpass = crypt($password, substr($username,0,1) . substr($username, 
length($username) - 1,1));

my $userID="501:501::";

        print PASSFILE "$username:$newpass:$userID\n";
}
close(PASSFILE);
exit;

-- 
Suresh Ramasubramanian + [EMAIL PROTECTED]
Linux is like a Teepee - No Windows, No Gates, Apache Inside

----------------------------------------------
The mailing list archives are available at
http://lists.linux-india.org/cgi-bin/wilma/LIH

Reply via email to