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!

transfering files using Perl

Status
Not open for further replies.

ddanj

IS-IT--Management
Jul 19, 2005
9
US
So I'm trying to write a Perl script to transfer authorized_keys to all the computers on the network from a master computer, but I am having trouble. I'm trying to use scp to copy the whole directory like so:

scp -r ./keys/ $some_machine:/root/.ssh

the thing is, the first time I run this script, the .ssh directory does not exist on the network machines, so the 'keys' directory gets renamed as .ssh and transfered, which is fine. But if I want to add another computer to the network and use the scrip again, instead of overwriting the .ssh directory with the correct files(on machines that i pushed the file to previously), it 'moves' the whole 'keys' directory to the already existing .ssh directory. So now, the .ssh directory will contain a 'keys' directory, instead of the contents of the 'keys' directory. I have no idea how to get around this, any help would be much appreciated!

-ddanj
 
That's an scp problem rather than a perl problem but, rather than calling scp from within perl, you should
Code:
use Net::SCP qw(scp iscp);
scp($source, $destination);
to avoid shell interactions.

The underlying problem is that scp's destination argument can be interpreted as a file or a directory and, the way you are using it, it flips between the two depending on the existance of the target directory. If you explicitly specify the filename you want to create, rather than just the directory in which to put it, that problem goes away.

Try
Code:
use Net::SCP;
my $scp = new Net::SCP 'somehost', $rem_user;
my $ak = '.ssh/authorized_keys';
$scp->scp( "$home/$ak", "$target_home/$ak" );
if ( $scp->{errstr} =~ 
    /cp: cannot create regular file .*: No such file or directory/ ) {
  $scp->mkdir( "$target_home/.ssh" );
  # maybe set permissions?
  $scp->scp( "$home/$ak", "$target_home/$ak" );
}

Yours,

fish



"As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilkes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top