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:

#!/usr/bin/perl # create a remote file, without creating a local file # use Net::SFTP; use Net::SFTP::Constants qw/:flags/; use strict; use warnings; my $filename = "testfile.dat"; my $file_data = "qwertyuiop"; my %args = ( user => 'insaniac', password => 'xxxx' ); #my $host = "moretrix.com"; my $host = "bear"; my $remote_path = 'not_allowed/'.$filename; my ($err_code, $err_mesg); print "Connecting to $host\n"; my $sftp; eval { $sftp = Net::SFTP->new($host,%args) }; ($err_code, $err_mesg) = $sftp->status; if($err_code){ print "Err_code:$err_code | err_mesg:$err_mesg\n"; exit $err_code }; print "Open $host:$remote_path\n"; my $remote_handle; eval { $remote_handle = $sftp->do_open($remote_path, SSH2_FXF_WRITE | SSH +2_FXF_CREAT) }; ($err_code, $err_mesg) = $sftp->status; if($err_code){ print "Err_code:$err_code | err_mesg:$err_mesg\n"; exit $err_code }; print "Write to remote file:\n$file_data\n"; eval { $sftp->do_write($remote_handle, 0, $file_data) }; ($err_code, $err_mesg) = $sftp->status; if($err_code){ print "Err_code:$err_code | err_mesg:$err_mesg\n"; exit $err_code }; print "Close remote file handle\n"; $sftp->do_close($remote_handle);
The output of the script:
Connecting to bear Open bear:not_allowed/testfile.dat Couldn't get handle: Permission denied at net_sftp.pl line 33 Err_code:3 | err_mesg:Permission denied
And the directory on the remote host is:
bear: [/home/insaniac]# ls -ld not_allowed/ d--------- 2 root root 4096 Sep 23 13:40 not_allowed/

HTH

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


In reply to Re^5: sftp->do_write howto? by insaniac
in thread sftp->do_write howto? by chrism01

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.