priyophan |
03-28-2011 11:30 PM |
User clone script from one server to another
Code:
#!/usr/bin/perl
use Term::ANSIColor;
####GROUP AND PASSWORD DETAILS EXTRACTION ######
print colored ( "Please enter the name of the SOURCE SERVER\n", ' bold green on_blue' );
$sserver = ;
chomp ($sserver);
print colored ( "Please enter the name of the DESTINATION SERVER\n" , ' bold green on_blue' );
$destserver = ;
chomp ($destserver);
print colored ( "Please enter the name of the GROUP TO CLONE\n", ' bold green on_blue' );
$group = ;
chomp ($group);
print colored ( "Please enter CA TICKET FOR CLONING USERS\n", ' bold green on_blue' );
$caticketno = ;
chomp ($caticketno);
$caticketno = "CA" . "$caticketno";
print colored ( "Please enter PASSWORD TO SET FOR USERS\n", ' bold green on_blue' );
$pswrd = ;
chomp ($pswrd);
$y=`/usr/nsh/bin/nexec $sserver grep $group /etc/group ` ;
chomp ($y);
$z = $y . ",";
`touch grp.tmp`;
`touch etcpswd.tmp`;
open (GRPTMP, ">grp.tmp")
or die "Can't open tmp file : $!";
print GRPTMP "$z" ;
close (GRPTMP)
or die "Can't close GRPTMP file :$! ";
open (PSWDTMP, ">etcpswd.tmp")
or die "Can't open tmp file : $!";
print PSWDTMP `/usr/nsh/bin/nexec $sserver cat /etc/passwd `;
close (PSWDTMP)
or die "Can't close PSWDTMP file :$! ";
if ($group)
{
open (FILEGRP, "./grp.tmp")
or die "Can't open group file : $!";
while ()
{
if ("$_" =~ m/^$group\b/)
{print "$group has users listed below: \n";
$groupcontent=$_;
}
}
close (FILEGRP)
or die "Can't close group file :$! ";
@groupcon = split(':',$groupcontent);
$userdetails = ("%s\n",$groupcon[3]);
printf ("%s\n",$userdetails);
print colored ( "Enter USERS TO EXCLUDE FROM CLONING IN COMMA SEPARATED VALUES \n", ' bold green on_blue' );
$userex = ;
chomp ($userex);
$userex = $userex . ",";
if ($userex)
{
@userexarr = split(',',$userex);
$userexarrc = @userexarr;
for ($count=0; $count < $userexarrc; $count++ )
{
$userexa = ("%s\n",$userexarr[$count]);
$userdetails =~ s/$userexa,//g;
$userdetails = $userdetails;
}
print "VIEW THE REVISED LIST OF USERS \n";
printf ("%s\n",$userdetails);
&userdetailext;
&options;
}
else
{
&userdetailext;
&options;
}
{
sub userdetailext
{
@arr1 = split(',',$userdetails);
$c = @arr1;
for ($count=0; $count < $c; $count++ )
{
$user = ("%s\n",$arr1[$count]);
chomp ($user);
open (FILEETCPSWD, "etcpswd.tmp")
or die "Can't open passwd file : $!";
while ()
{
if ("$_" =~ m/^$user\b/)
{
@details = split(':',$_);
$userid = ("%s\n",$details[0]);
$usercomment = ("%s\n",$details[4]);
@usercombreak = split(';',$usercomment);
$psid = ("%s\n",$usercombreak[0]);
$username = ("%s\n",$usercombreak[1]);
$usercomment = "$psid;" . "$username;" . "$caticketno" ;
print colored ( "$usercomment\n", ' bold blue on_yellow' );
}
}
close (FILEETCPSWD)
or die "Can't close group file :$! ";
}
}
}
{
sub options
{
print colored ( "IF YOU WOULD LIKE TO CONTINUE ,PLEASE ENTER YES\n", ' bold green on_blue' );
$userinput = ;
chomp ($userinput);
if ( $userinput =~ m/y|yes/i )
{
&useraddition;
}
else
{
exit;
}
}
}
{
sub usercommentdisp
{
print colored ( "$usercomment\n", ' bold blue on_yellow' );
}
}
{
sub useraddition
{
@arr1 = split(',',$userdetails);
$c = @arr1;
for ($count=0; $count < $c; $count++ )
{
$user = ("%s\n",$arr1[$count]);
chomp ($user);
open (FILEETCPSWD, "etcpswd.tmp")
or die "Can't open passwd file : $!";
while ()
{
if ("$_" =~ m/^$user\b/)
{
@details = split(':',$_);
$userid = ("%s\n",$details[0]);
$usercomment = ("%s\n",$details[4]);
@usercombreak = split(';',$usercomment);
$psid = ("%s\n",$usercombreak[0]);
$username = ("%s\n",$usercombreak[1]);
$usercomment = "$psid;" . "$username;" . "$caticketno" ;
print colored ( "CREATING USER $userid\n", ' bold red ' );
print color 'reset';
`/usr/nsh/bin/nexec $destserver useradd -c "$usercomment" -g $group $userid`;
&password_change;
}
}
close (FILEETCPSWD)
or die "Can't close group file :$! ";
}
}
}
###CLEARING TMP FILES####
`rm grp.tmp`;
`rm etcpswd.tmp`;
#########################
{
sub password_change
{
use Expect;
my $exp=Expect->spawn("/usr/nsh/bin/nexec $destserver passwd $userid");
my $spawn_ok;
$exp->expect(30,
[
qr'ssword:',
sub {
$spawn_ok=1;
$exp->stty('-echo');
print $exp ("$pswrd\n");
$exp->stty('echo');
exp_continue;
}
],
[
qr'ssword:',
sub {
$spawn_ok=1;
$exp->stty('-echo');
print $exp ("$pswrd\n");
$exp->stty('echo');
exp_continue;
}
],
);
$exp->soft_close();
return(0);
}
}
}
else
{print "No input provided in GROUP TO CLONE input\n";}
exit;
|