in reply to Net:SSH2 connects, but scp_get fails

You are mixing to different protocols, SFTP and SCP and it seems that SFTP works but SCP doesn't. It may be forbidden at the server, it is not something unusual. Can you transfer files from the remote host using scp from the command line? On Windows you can use the pscp utility that comes with PuTTY to try.

The way to transfer files through SFTP using Net::SSH2 is to open the remote file and then read from the returned remote file handle and write to a local file...

Or much easier, you can use Net::SFTP::Foreign that is a full fledged SFTP client. You can even run it on top of Net::SSH2 with the helper module Net::SFTP::Foreign::Backend::Net_SSH2.

Replies are listed 'Best First'.
Re^2: Net:SSH2 connects, but scp_get fails
by Anonymous Monk on Mar 27, 2010 at 01:22 UTC

    Thanks salva!

    I tried to use scp from a UNIX command line and it didn't work. I'll update the script to use just SSH2 (or change it to use Net::SFTP::Foreign --haven't decided) and post my results back here.

      That was it! Mixing SSH2 and SFTP commands is a bad thing. For the sake of future Novices, I'm posting change required to get the sample script working:

      Replace the original code block (around line 157):

      printf("# scp_get(%s,%s)\n", $myFile, $localDir . "\\$myFile"); $rc=$ssh2->scp_get($myFile, $localDir . "\\$myFile"); printf("# rc=%s\n", defined($rc)?$rc:"<UNDEFINED>");
      with the more verbose (but more correct) block
      # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv # The previous code block was replaced with code to # open remote and local files, then read the remote file # and write it to the local file. # The binmode() call is necessary because this is a # Windows(tm)-to-UNIX transfer and line-endings must # be preserved. my $rfh; # Remote File Handle $rfh = $sftp->open($sftpRemoteDir . "/" . $myFile); if (defined($rfh)) { if (open(LFH, ">$localDir\\$myFile")) { my $myLine; # Force binary transfers to preserve line endings # when transferring between Windows and UNIX. binmode(LFH); while ($myLine = <$rfh>) { printf(LFH "%s", $myLine); } close(LFH); } else { # ERROR: Could not open local file for transfer printf("Could not open local file '%s' for transfer\n" +, $myFile); } } else { # ERROR: Could not open remote file for transfer printf("Could not open remote file '%s' for transfer\n", $myFile); }

      Many thanks for your help! You saved me hours of banging my head against a wall!

      Kevin