in reply to sftp->do_write howto?

Since you said that you are trying to do this without creating a local file, the previous response doesn't really answer the question. Reading the source, the flags for open are the Constants prefixed with SSH2_FXF_. Specifically,

'SSH2_FXF_READ' => 0x01, 'SSH2_FXF_WRITE' => 0x02, 'SSH2_FXF_APPEND' => 0x04, 'SSH2_FXF_CREAT' => 0x08, 'SSH2_FXF_TRUNC' => 0x10, 'SSH2_FXF_EXCL' => 0x20,

Which should look familiar to anyone who has ever opened a file à la C. You say you want to append to an existing remote file, so you do not need READ, CREAT(e), TRUNC(ate) or EXCL(ude). You do need WRITE and APPEND, so your flags are:

SSH2_FXF_WRITE | SSH2_FXF_APPEND

(Remember that you OR flags when you want them both)

Update: As is pointed out by insaniac, this is not the only code change. You either neet to use the hex listed above (less portable) or follow the instructions in the docs:

use Net::SFTP::Constants qw( SSH2_FXP_WRITE SSH2_FXF_APPEND );

The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. -- Cyrus H. Gordon

Replies are listed 'Best First'.
Re^2: sftp->do_write howto?
by chrism01 (Friar) on Sep 22, 2005 at 01:38 UTC
    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
      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