LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   User clone script from one server to another (https://www.linuxquestions.org/questions/linux-server-73/user-clone-script-from-one-server-to-another-871608/)

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;


PrinceCruise 03-29-2011 03:46 PM

I'd love to know a brief description other than the title...*sigh!

priyophan 03-30-2011 12:21 AM

I live in a network with nearly 5000 servers passthrough with nsh or ssh , this script is designed for the nsh shell but it can also be tweaked to work with ssh .

The first part uptil the file handlers for grp.tmp and etcpswd.tmp is for taking the input for the group and users to clone and copying it from the source server to a temporary file in the grp.tmp and the etcpswd.tmp.

The users are again filtered in the input as it sometimes happens that the source server contains a lot of users and the users to clone are not all of them but a few of them. $userex

The users are put in an array @userexarr and then using a for loop the array elements are put in separate variables .

The script might appear longer as the comment field in the user field is populated with the service request no. , the people soft id and the details of the user.

Afterwards when all the variables are populated it is just passed to the nsh shell with useradd and the usual expect statements for setting the password .


If required the script can be shortened without the people soft id and the service request no. but it depends on the environment to be implemented .

Cheers.....


All times are GMT -5. The time now is 06:59 PM.