in reply to Re: sftp->do_write howto?
in thread sftp->do_write howto?

Thx for the hints.
Actually, I'm not appending, i'm simply "copying" (ie creating) the attachments to their own files on the remote system, but without creating a local copy first (inefficient).
I've tried this test prog:
$filename = "testfile.dat"; $payload = "qwertyuiop"; $args{user} = "xxx"; $args{password} = "yyy"; $host = "s.a.c.u"; $path = "/some/path/".$filename; print "connecting\n"; $sftp = Net::SFTP->new($host, %args); print "open\n"; $handle = $sftp->do_open($path, SSH2_FXF_WRITE | SSH2_FXF_CREAT); print "write\n"; $sftp->do_write($handle, 0, $payload); print "close\n"; $sftp->do_close($handle);
(where I've obscured the real values).
but it says

Permission denied at /usr/lib/perl5/site_perl/5.8.5/Net/SFTP.pm line 62

which is this line

$ssh->login($param{user}, $param{password});

I've tried replacing this with the hardcoded vals from my program and printed out the host value (looks ok), but same error.
FYI, I can successfully login manually from the cmd line using the same values(!), so I'm stuck...
Any ideas??
Cheers
Chris

Replies are listed 'Best First'.
Re^3: sftp->do_write howto?
by insaniac (Friar) on Sep 22, 2005 at 10:41 UTC
    tip 1: use strict; use warnings;! Then you would have seen:
    Bareword "SSH2_FXF_WRITE" not allowed while "strict subs" in use at ne +t_sftp.pl line 17. Bareword "SSH2_FXF_CREAT" not allowed while "strict subs" in use at ne +t_sftp.pl line 17. Execution of net_sftp.pl aborted due to compilation errors.
    When you use the hex values shown above, the code connects to your remote host.
    It even sends the file! Your code works just by replacing SSH2_FXF_WRITE | SSH2_FXF_CREAT by their hex values.

    Update:
    See idsfa's remark about the Net::SFTP::Constants ;-)

    Cheers!

    to ask a question is a moment of shame
    to remain ignorant is a lifelong shame

      Funnily enough, I do put use strict and #!/usr/bin/perl -w in all my progs normally. I just forgot the use strict in this test (very unusual) but did have -w.
      Anyway, amended prog to have both and changed do_open() line to:
      $handle = $sftp->do_open($path, 0x02| 0x08);
      Prob is, still get the same error ie:
      Permission denied at /usr/lib/perl5/site_perl/5.8.5/Net/SFTP.pm line 62
      which as I said before is the login bit which is called from Net::SFTP->new(), ie it never even gets as far do_open().
      Any ideas?
      Cheers
      Chris
        Very strange... maybe something's wrong with the module? Look, this is the code I used. I've tried connecting with an ssh key (so without username and pass), it works; I've also tried with a username and pass, it also works.
        use Net::SFTP; use Net::SFTP::Constants qw/:flags/; use strict; use warnings; my $filename = "testfile.dat"; my $payload = "qwertyuiop"; my %args; $args{user} = "insaniac"; $args{password} = "xxxx"; #my $host = "moretrix.com"; my $host = "bear"; my $path = $filename; print "file $path\n"; print "connecting\n"; my $sftp = Net::SFTP->new($host,%args) ; print "open\n"; my $handle = $sftp->do_open($path, SSH2_FXF_WRITE | SSH2_FXF_CREAT); print "write\n"; $sftp->do_write($handle, 0, $payload); print "close\n"; $sftp->do_close($handle);
        I don't think anything's wrong with your code, what version are you using? Is the pathname on the remotehost correct and do you have permission to write in that directory (very important!!! the first time I thought your $path variable was a local path!) This is the output I always get:
        connecting open write close
        Maybe try marto 's code above with your login credentials and try uploading a file (to the same $path), see if that works. Otherwise... I have no more ideas :-/

        Update: Code is updated to use idsfa's suggestion

        to ask a question is a moment of shame
        to remain ignorant is a lifelong shame

        OK, I'm positive now: you have a permission problem om your remote host.
        I've tried uploading to a directory with perm settings 000, and then I get the permission denied error message.

        Here's a version with more testing code:

        HTH

        to ask a question is a moment of shame
        to remain ignorant is a lifelong shame