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

Hi All,

I built a code for pushing the data from my Windows machine to Unix machine using the SFTP protocol. For the purpose I used NET::SFTP::Foreign. The part of code is mentioned below.

#!c:\Perl\bin\perl use Net::SFTP::Foreign; use Net::SSH2; $user="abcd"; $passwd="abcd@1234"; $host = "192.168.10.2"; $port = "22"; $sftp = Net::SFTP::Foreign->new($host, backend => 'Net_SSH2',username +=> $user, password => $passwd, port => $port) || die $sftp->error; print " SFTP Connected \n"; for ($i = 0; $i <= 23; $i++) { $filename = "testfile".$i.".csv"; $sftp->put("c:\\documents\\$filename", "/usr/home/abcd/$filename"|| pr +int $sftp->error; print $sftp->status; }

Using the above code, I am able to transfer the files but as soon as the it tries to transfer the 4th file it gives me error "write failed: LIBSSH2_ERROR_EAGAIN (-37): Unable to send channel data" and Status as "Connection lost". All the subsequent files while putting generate error "Connection to remote server stalled" with status message "Connection lost". I need to run my script few more times to transfer remaining files.

The OS is Windows 7, Perl version is v5.10.1 and Net::SFTP::Foreign version is 1.62.

Can someone please guide me why connection is getting stalled after transferring few files and what is way out?

Replies are listed 'Best First'.
Re: Strange behavior for PUT function in Net::SFTP::Foreign
by salva (Canon) on Sep 05, 2011 at 10:55 UTC
    I believe that problem is caused by a bug in Net::SSH2.

    I have uploaded a new version of Net::SFTP::Foreign::Backend::Net_SSH2 that includes a work around for it.

      Thanks Salva

      I will try to use Net::SFTP::Foreign::Backend::Net_SSH2

      Hi Salva

      I tried using the method mentioned on page http://search.cpan.org/~salva/Net-SFTP-Foreign-Backend-Net_SSH2-0.05/lib/Net/SFTP/Foreign/Backend/Net_SSH2.pm but the results were still the same. I am still getting the same error. I only made following inclusions at starting

      $ssh2 = Net::SSH2->new(); $ssh2->connect($host) or die "Unable to connect $host $@ \n"; print " SSH Connected \n"; $ssh2->auth_password($user,$passwd) or die "Unable to login $@ \n"; print " SSH Authenticated \n"; $sftp = Net::SFTP::Foreign->new(ssh2 => $ssh2, backend => 'Net_SSH2') +|| die "$sftp->error";

      rest of the code remains the same. Can you please guide me what am I doing wrong here?

        And BTW, you don't need to do anything else besides installing it. Leave your script as it was.
Re: Strange behavior for PUT function in Net::SFTP::Foreign
by kcott (Archbishop) on Sep 05, 2011 at 11:01 UTC

    The first thing I noticed was that there is no closing parenthesis for: $sftp->put(...

    I recommend that you add use strict; and use warnings; to the beginning of your code. This will highlight a number of issues.

    Unless you actually have an array called @1234 which you wish to interpolate, change the $passwd assignment to use single quotes. In fact, except for the $sftp->put(... arguments, I can't see anywhere that double quotes are required.

    -- Ken