LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 03-28-2011, 11:30 PM   #1
priyophan
Member
 
Registered: Apr 2009
Posts: 36

Rep: Reputation: 0
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;

Last edited by jeremy; 03-30-2011 at 01:59 PM. Reason: added code tags
 
Old 03-29-2011, 03:46 PM   #2
PrinceCruise
Member
 
Registered: Aug 2009
Location: /Universe/Earth/India/Pune
Distribution: Slackware64 -Current
Posts: 890

Rep: Reputation: 186Reputation: 186
I'd love to know a brief description other than the title...*sigh!
 
Old 03-30-2011, 12:21 AM   #3
priyophan
Member
 
Registered: Apr 2009
Posts: 36

Original Poster
Rep: Reputation: 0
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.....
 
  


Reply

Tags
[perl]



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to clone a user account from one server to other server ZAMO Linux - Enterprise 5 03-19-2010 01:40 AM
Small bash script to notify server user makowka Linux - Server 2 02-15-2010 02:17 AM
Execute script on local server as normal user to run commands on remote server ALInux Linux - Software 1 01-01-2010 06:30 AM
I need to Clone a Red Hat drive and install clone in HP server drummer54 Linux - Newbie 14 03-07-2009 04:15 PM
shell script to automatically kill user on a server vathsan AIX 3 01-06-2009 03:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 01:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration