Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am using perl 5.8.8 and using perl foreign sftp to send a directory of files from one linux server to another. The script will send a list of files to replace files on the target server. The source server has some of the files in the target server but not all. When sending the files over, the target files are being wiped out with a 0 bytes but not replaced with the source file. the target file has had a chmod 777 adjusted to it to allow manipulation. I also don't see an error

#!/usr/bin/perl -I/episys/Perl use lib qw(/episys/Perl/); use Cwd qw(abs_path); use strict; #use warnings; use Net::SFTP::Foreign; use File::Basename; use AppConfig; # read config file my $cfg = new AppConfig; my $pathName = dirname(abs_path($0)); my $configNameWithPath = $pathName . "/res_test_servers.cfg"; $cfg->read($configNameWithPath) or die "Couldn't read config file: $!" +; # Get attributes from config file my $s_list = $cfg->get('server.res_target_servers'); my $inputDir = $cfg->get('source_directory.res_source_directory'); my $outputDir = $cfg->get('target_directory.res_target_directory'); # Read files from Staging directory into Files array opendir my($dh), $inputDir or die "Couldn't open dir '$inputDir': $!"; my @files = readdir $dh; closedir $dh; # get rid of the links to the directories above current directory my $index = 0; $index++ until $files[$index] eq '.'; splice(@files, $index, 1); $index = 0; $index++ until $files[$index] eq '..'; splice(@files, $index, 1); # get server list my @server_list = split(/\,/ , $s_list); # process for each server foreach my $server_list (@server_list) { # connect to server print "ftping file to server: " . $server_list . "\n"; my $sftp = Net::SFTP::Foreign->new($server_list); $sftp->error and die "Unable to stablish SFTP connection: " . $sft +p->error; # process each file to each server foreach my $file (@files) { my $source_ftp_file = $inputDir . "/". $file; my $destination_ftp_file = $outputDir . "/" . $file; print "\n" . $source_ftp_file . "\n"; print $destination_ftp_file . "\n"; # put file to server $sftp->rput($source_ftp_file, $destination_ftp_file, overwrite + => 1) or die "could not put file \n" . $sftp->error; # change files to allow modification by anyone #$sftp->chmod($destination_ftp_file, 0777) or die "count not c +hmod file \n" . $sftp->error; } $sftp->disconnect;

Replies are listed 'Best First'.
Re: rput on foreign sftp in perl
by kcott (Archbishop) on Sep 17, 2013 at 02:16 UTC

    See the Net::SFTP::Foreign documentation.

    rput() is for recursively transferring directory contents. put() is for transferring individual files.

    See also: setcwd() for changing the remote directory and chdir for changing the local directory.

    Unrelated to your problem, but readdir has an example using grep which could greatly improve your code populating @files (in particular, you could get rid of the two until loops and the splice operations).

    -- Ken

      Thank you so much for the information. I need to use the RPUT command since I am transferring a series of directories and files from one linux server to another and the RPUT is working, but I need to have the target server receive the files with permissions set to CHMOD 777. Is there a way using perl SFTP to do a recurrsive chmod command on the directories I just transferred over?

        "I need to use the RPUT command since I am transferring a series of directories ..."

        The only rput() in your code is:

        $sftp->rput($source_ftp_file, $destination_ftp_file, ...

        If $source_ftp_file and $destination_ftp_file are supposed to be directories, then they are poorly named: consider $source_ftp_dir and $destination_ftp_dir.

        "... and the RPUT is working, ..."

        Your original post said: "When sending the files over, the target files are being wiped out with a 0 bytes but not replaced with the source file.". That doesn't sound like it's working.

        "... but I need to have the target server receive the files with permissions set to CHMOD 777."

        Both put() and rput() have various options relating to permissions. I've already supplied a link to the documentation.

        -- Ken

Re: rput on foreign sftp in perl
by salva (Canon) on Sep 18, 2013 at 09:39 UTC

      Thanks for your help - here is the output of the debug statement: # rput handling /test/staging/TestMap # rpath: /test/perf/staging/Apps/TestMap # _set_err code: 42, str: Remote directory '/test/perf/staging/Apps/TestMap' already exists # rput handling /test/staging/TestMap/cls # rpath: /test/perf/staging/Apps/TestMap/cls # _set_err code: 42, str: Remote directory '/test/perf/staging/Apps/TestMap/cls' already exists # rput handling /test/staging/TestMap/src # rpath: /test/perf/staging/Apps/TestMap/src # _set_err code: 42, str: Remote directory '/test/perf/staging/Apps/TestMap/src' already exists # rput handling /test/staging/TestMap/cls/com # rpath: /test/perf/staging/Apps/TestMap/cls/com # _set_err code: 42, str: Remote directory '/test/perf/staging/Apps/TestMap/cls/com' already exists # rput handling /test/staging/TestMap/src/com # rpath: /test/perf/staging/Apps/TestMap/src/com # _set_err code: 42, str: Remote directory '/test/perf/staging/Apps/TestMap/src/com' already exists # rput handling /test/staging/TestMap/cls/com/test # rpath: /test/perf/staging/Apps/TestMap/cls/com/test # _set_err code: 42, str: Remote directory '/test/perf/staging/Apps/TestMap/cls/com/test' already exists # rput handling /test/staging/TestMap/src/com/test # rpath: /test/perf/staging/Apps/TestMap/src/com/test # _set_err code: 42, str: Remote directory '/test/perf/staging/Apps/TestMap/src/com/test' already exists # rput handling /test/staging/TestMap/cls/com/test/trigger # rpath: /test/perf/staging/Apps/TestMap/cls/com/test/trigger # _set_err code: 42, str: Remote directory '/test/perf/staging/Apps/TestMap/cls/com/test/trigger' already exists # rput handling /test/staging/TestMap/cls/com/test/trigger/TestMap.class # rput handling /test/staging/TestMap/cls/com/test/data # rpath: /test/perf/staging/Apps/TestMap/cls/com/test/data # _set_err code: 42, str: Remote directory '/test/perf/staging/Apps/TestMap/cls/com/test/data' already exists # rput handling /test/staging/TestMap/cls/com/test/data/testMembersPricingVO.class # rput handling /test/staging/TestMap/cls/com/test/data/testPriceDetailDTO.class # rput handling /test/staging/TestMap/cls/com/test/data/testTriggerVO.class # rput handling /test/staging/TestMap/cls/com/test/data/testPriceHeaderDTO.class # rput handling /test/staging/TestMap/src/com/test/trigger # rpath: /test/perf/staging/Apps/TestMap/src/com/test/trigger # _set_err code: 42, str: Remote directory '/test/perf/staging/Apps/TestMap/src/com/test/trigger' already exists # rput handling /test/staging/TestMap/src/com/test/trigger/TestMap.java # _set_status code: 3, str: Permission denied # _set_err code: 24, str: Couldn't setstat remote file: Permission denied Use of uninitialized value in numeric ne (!=) at /usr/lib/perl5/site_perl/5.8.8/Net/SFTP/Foreign/Common.pm line 79. # rput handling /test/staging/TestMap/src/com/test/data # rpath: /test/perf/staging/Apps/TestMap/src/com/test/data # _set_err code: 42, str: Remote directory '/test/perf/staging/Apps/TestMap/src/com/test/data' already exists # rput handling /test/staging/TestMap/src/com/test/data/testPriceHeaderDTO.java # rput handling /test/staging/TestMap/src/com/test/data/testTriggerVO.java # rput handling /test/staging/TestMap/src/com/test/data/testPriceDetailDTO.java # rput handling /test/staging/TestMap/src/com/test/data/testMembersPricingVO.java

        Could you wrap the output with <code></code> tags in order to make it understandable?