Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^8: Perl sftp behavior when there are no files in the share

by Bindo (Acolyte)
on Mar 28, 2013 at 12:50 UTC ( #1025956=note: print w/ replies, xml ) Need Help??


in reply to Re^7: Perl sftp behavior when there are no files in the share
in thread Perl sftp behavior when there are no files in the share

Thank you again sir. I tried but it gives me this error.

Odd number of elements in hash assignment at /usr/lib/perl5/site_perl/ +5.10.0/Net/SFTP/Foreign.pm line 1432. Invalid option 'CODE(0xa53608)' or bad combination of options at ./gtp +_logs.pl line 66

Any thoughts sir? Also what would be the easiest way to get what I want without using CSPAN stuff? As in that simple progresss shown when you download the file manually? Many thanks in advance sir.


Comment on Re^8: Perl sftp behavior when there are no files in the share
Download Code
Re^9: Perl sftp behavior when there are no files in the share
by Corion (Pope) on Mar 28, 2013 at 12:55 UTC

    It seems there is a small error in the code by soonix. The Net::SFTP::Foreign documentation says that ->get takes the local name of the file as second parameter and the rest are options. So if you want to display the progress, you'll also have to provide the local name, for example like this:

    ... use File::Basename; my $local_name= basename( $REM_FILE ); warn "Saving '$REM_FILE' to '$local_name'"; $sftp->get($REM_FILE, $local_name, callback => \&show_progress) ...
Re^9: Perl sftp behavior when there are no files in the share
by soonix (Curate) on Mar 29, 2013 at 19:49 UTC

    This is actually two replies in one. First, thanks (and ++) to Corion for pointing out the mistake in my solution above.

    Now Bindo, your question for a solution without CPAN... I think doing SFTP in pure Perl, while possible, would be difficult and error-prone. If you just don't want any additional CPAN modules, that's another question:

    As far as I see, your $REM_FILE already is a file name without directory, so you don't need the basename function and module, and can simply

    $sftp->get($REM_FILE, $REM_FILE, callback => \&show_progress)
    Perhaps that already does what you want.

    Update: second parameter in get corrected

      Appologies gentlemen that I was away. Well thank you everyone who have shared the thoughts towards my query. Mr Soonix no luck sir. Following is exactly what I tried, No progress is shown when I downloaded file :(.

      print "Connecting to the sftp share! Please wait....!\n"; my $sftp = Net::SFTP::Foreign->new($LOG_HOST, user => +$REM_USER, password => $REM_PASSW); $sftp->setcwd($REM_PATH) or die "unable to change cwd: + " . $sftp->error; print "OK. On the share! Downloading the file \"$REM_F +ILE\"...................!\n\n\n"; $sftp->error and die "Problem connecting to the share. +..!!!! " . $sftp->error; # if ($sftp->get($REM_FILE)){ if ($sftp->get($REM_FILE, $REM_FILE, callback => \&sho +w_progress)) { print "File \"$REM_FILE\" downlaoded to ===> +\"$LOC_PATH$LOC_DIR\"\n\n"; } else { print "File \"$REM_FILE\" does not seem to hav +e been sent. Please re-request...!\n\n"; } sub show_progress { my($sftp, $data, $offset, $size) = @_; print "downloaded $offset of $size bytes\r";

        But the files are transferred and the other print statements are executed?

        If I understand Net::SFTP::Foreign correctly, the callback function should be executed at least once per call to ->get.

        Maybe it is overwritten too quickly - what if you remove the \r or replace it with \n?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1025956]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2014-11-28 02:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (192 votes), past polls