in reply to Re: Net:SSH2 connects, but scp_get fails
in thread Net:SSH2 connects, but scp_get fails

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.

  • Comment on Re^2: Net:SSH2 connects, but scp_get fails

Replies are listed 'Best First'.
Re^3: Net:SSH2 connects, but scp_get fails
by knc (Initiate) on Mar 29, 2010 at 04:14 UTC

    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