Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to copy files from one location to many 1

Status
Not open for further replies.

North323

Technical User
Jan 13, 2009
966
US
I have a situation where I have many linux boxes that I would like to copy the contents of a folder to all the other boxes. I am trying to write a perl script to do this, here is what I have so far. is there a better way of doing versus entering each destination server? so there is one source server that will be going to 10 destination servers, same files.
Code:
#!/usr/bin/perl
use File::Copy;

print "content-type: text/html \n\n"; #Header
$PATH1 = "\\firewall_name\home\folder"; #source folder of firewall files
$PATH2 = "\\firewall_name\home\folder"; #destination folder of firewall files
$file_to_copy = "firewall_file";
$file_to_firewall = "firewall_file";
copy("$PATH1/$file_to_copy", "PATH2/$file_to_firewall") or die "File cannot be copied.";
 
How are you communicating between the different Linux boxes?
RCP or SCP might be an option for trusted hosts.
 
i would use scp...does that change the script?
 
If I understand your question correctly, the script would go something like this:

$srcDir = "dirPath";
$tgtDir = "dirPath";
my @cList = qw (ip's or names go here);
foreach $cList (@cList) {
system("scp $srcDir/* $cList:$tgtDir/");
}
 
are those IP's seperated by a comman (,) or semi colon?
 
if you use qw then spaces otherwise, which ever way you will declare an array is how the contents will be defined.
 
then this would be the entire script:

$srcDir = "/linuxbox/home/folder";
$tgtDir = "/home/folder1";
my @cList = qw (192.168.1.1 192.168.1.3 192.168.1.5);
foreach $cList (@cList) {
system("scp $srcDir/* $cList:$tgtDir/");
}

so this script is run on the source box and will copy everything from sourcebox/linuxbox/home/folder to the IP's listed /home/folder1

correct?

 
Code:
$srcDir = "/linuxbox/home/folder";
$tgtDir = "/home/folder1";
my @cList = qw (192.168.1.1 192.168.1.3 192.168.1.5);
foreach $cList (@cList) {
        system("scp $srcDir/* $cList:$tgtDir/");
}

That would work, but if $srcDir contains additional directories under it, these won't be copied over, only the regular files will. You'll need to use the -r (recursive) option with scp to copy directories too:

system("scp -r $srcDir/* $cList:$tgtDir/");

Also, scp in a default configuration will prompt you to enter the password for the remote server. So each scp command will wait for you to provide the password before it can continue, and Perl can't easily automate this process. The best solution is to set up a key-based authentication system.

You'll need to generate an RSA key pair by running the command ssh-keygen -t rsa, this will create ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub (where "~" means your home directory)... scp the id_rsa.pub file to the remote host, and then add it to the authorized_keys file on the remote host:

Code:
cat id_rsa.pub >> ~/.ssh/authorized_keys

Make sure the authorized_keys file is chmodded 0600.

Test it by trying to ssh to the server again, if you get logged in without a password prompt then it works. In a default ssh configuration on pretty much every Linux system I've worked with (all redhat-based ones and ubuntu), this should work fine. Certain configurations in the ssh server may prevent key-based authentication though.

Kirsle.net | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Sorry I'm a bit late on this thread, but isn't this what rsync is for?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top