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

Oh Great Monks!
You have aided me many times in the past when I have come seeking answers and I must humbly ask for your wisdom again.
I too hope to someday be one of the "enlightened one".
Ok. I'm trying to send files via SFTP and I'm using Net::SFTP. I don't know what I'm doing wrong but it's giving me no love. See code below.

use strict; use warnings; use Net::SSH::Perl; use Net::SFTP; my $host = "sftp.someone.com"; my $user = "user"; my $pass = "pass"; my $home = "/path/to/file.txt"; my $path = "/new/path/to/file"; my $sftp = Net::SFTP->new($host, "user" => $user, "password +> $pass, +debug =>1) || die "can't login $!\n"; $sftp->put($home,$path) || die "can't open: $!"; $sftp->quit # I'm guessing on that because there isn't a way listed in + the CPAN docs about how to close.

It comes back and tells me that there isn't a file or directory.

please help!
Sebaw

Replies are listed 'Best First'.
Re: Net::SFTP Help
by hossman (Prior) on Apr 09, 2003 at 01:25 UTC
    I've never used Net::SFTP, but I have a few suggestions...
    1. When posting to PerlMonks, in addition to posting a complete sample program demonstrating your problem, you should post the exact error message you are getting.
    2. I don't see anything in the docs for Net::SFTP (v0.05) that says put will return true on success. Are you sure the copy isn't working just fine, and the only problem is that you are calling "die" ?
    3. Are you sure that the local file you want to put is "readable" by your local user?
    4. Are you sure that the remote directory you want to write to is "writable" by your remote user?
    5. Are you sure that your remote server supports SFTP?
    6. Have you tried something simpler then a put? like: print Dumper($sftp->ls('.')); ?
    7. Net::SFTP comes with a sample program called "psftp" that provides an interactive SFTP shell .. you should try using that to send your file once. If that works, then maybe you can dig into it's code to see what it does that you aren't.
      Very good point. The error I am getting when debug is on is that it can't open the remote directory to write to it. -- This is puzzling, because I can sftp to the directory and send the files by hand.
      I am logged in locally to the machine to look at it. I have tried opening the directory with $sftp->open($path) but nothing from a remote standpoint seems to work.
      I didn't know about the psftp but will try it. Thanks.
Re: Net::SFTP Help
by phydeauxarff (Priest) on Apr 08, 2003 at 18:49 UTC
    Sounds like either the source file or directory or destination directory doesn't exist.
      It sounds like that but I built this in FTP to test to make sure and it works in FTP but not in SFTP.
Re: Net::SFTP Help
by zentara (Cardinal) on Apr 09, 2003 at 15:39 UTC
    When I use sftp from the command line, I always run into that problem. It arises from the difference in the pwd "present working directory" and the lpwd "local working directory". You have to keep track of which directories you are in on both machines. I suggest you do the task manually with sftp and see where you have to do lcd "local change directory" and cd "change directory on remote machine". Then work that in to your perl script.
Re: Net::SFTP Help
by mikedshelton (Beadle) on Dec 21, 2003 at 02:31 UTC
    Sebaw, Have you taken a look at my Net::SFTP send file program in the following node Seek Critique of SFTP program, format, structure, overall "Perl"ness ?

    It is a fully functional program. While there are improvements to be made, I think you will find it very helpful.

    BTW $sftp->put() and $sftp-get() don't return a status for success / failure (I have submitted a bug on rt.cpan.org) like $sftp-new() does. It is necessary that you eval{} and catch{} the results (see main{}).

    Earlier this week I found that SFTP does not have a timeout default like FTP... you will need to roll your own alarm to handle this no-repsonse from host (bug reported) - refer to my posting ??? proper use of $SIG{ALRM} with SFTP ???.

    The suggestion about testing sftp'ing a file manually and keeping track of all your steps is a good one. This was my MO during testing. You will notice there are many checks in my send program, including those dealing with source directory / file existance and permissions.

    I also have a retrieve program that I will be posting in the near future for reference (doesn't hurt to share the knowledge)

    Thanks PerlMonks.com for letting me help others as I have been helped.

    mikedshelton