Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: sftp->do_write howto?

by insaniac (Friar)
on Sep 22, 2005 at 10:41 UTC ( [id://494073]=note: print w/replies, xml ) Need Help??


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

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

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

        Here's the latest with your debug code:
        #!/usr/bin/perl use Net::SFTP; use Net::SFTP::Constants qw/:flags/; use strict; use warnings; my ( $filename, $payload, $host, $sftp, %args, $path, $handle, $err_code, $err_mesg ); $filename = "testfile.dat"; $payload = "qwertyuiop"; %args = (user => "yyyyyyyyyy", password => "xxxxxx"); $host = "a.b.c.d"; $path = "/network/www/ticketattach/".$filename; print "connecting\n"; 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\n"; $handle = $sftp->do_open($path, SSH2_FXF_WRITE | SSH2_FXF_CREAT); #$handle = $sftp->do_open($path, 0x02| 0x08); ($err_code, $err_mesg) = $sftp->status; if($err_code) { print "Err_code:$err_code | err_mesg:$err_mesg\n"; exit $err_code }; print "write\n"; $sftp->do_write($handle, 0, $payload); ($err_code, $err_mesg) = $sftp->status; if($err_code) { print "Err_code:$err_code | err_mesg:$err_mesg\n"; exit $err_code }; print "close\n"; $sftp->do_close($handle); ($err_code, $err_mesg) = $sftp->status; if($err_code) { print "Err_code:$err_code | err_mesg:$err_mesg\n"; exit $err_code };
        and the response was...
        connecting Can't call method "status" on an undefined value at ./sftp.pl line 28.
        and as mentioned before I can ssh/sftp fine from the cmd line, but running this script from the same env manually does the above .. grrr... ie won't connect, forget about uploading.
        Cheers
        Chris

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://494073]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-26 08:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found