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

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

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

Re^5: sftp->do_write howto?
by insaniac (Friar) on Sep 23, 2005 at 11:53 UTC
    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
        Hm... further investigations make me think there's a bug in Net::SSH::Perl or Net::SFTP.

        try adding this to your code:

        my %args = ( user => 'user', password => 'password', ssh_args => [ debug => 1, options => [ "PreferredAuthentications 'keyboard-interactive,password,h +ostbased,publickey'", ] ], );
        and then:
        eval { $sftp = Net::SFTP->new($host, %args) }; die "Connection failed: $!\n" if $!; die "No SFTP object\n" unless $sftp;
        my output goes like:
        wolf: Reading configuration data /home/insaniac/.ssh/config wolf: Reading configuration data /etc/ssh_config wolf: Connecting to bear, port 22. wolf: Remote protocol version 2.0, remote software version OpenSSH_4.2 +p1 Debian-4 wolf: Net::SSH::Perl Version 1.28, protocol version 2.0. wolf: No compat match: OpenSSH_4.2p1 Debian-4. wolf: Connection established. wolf: Sent key-exchange init (KEXINIT), wait response. wolf: Algorithms, c->s: 3des-cbc hmac-sha1 none wolf: Algorithms, s->c: 3des-cbc hmac-sha1 none wolf: Entering Diffie-Hellman Group 1 key exchange. wolf: Sent DH public key, waiting for reply. wolf: Received host key, type 'ssh-dss'. wolf: Host 'bear' is known and matches the host key. wolf: Computing shared secret key. wolf: Verifying server signature. wolf: Waiting for NEWKEYS message. wolf: Enabling incoming encryption/MAC/compression. wolf: Send NEWKEYS, enable outgoing encryption/MAC/compression. wolf: Sending request for user-authentication service. wolf: Service accepted: ssh-userauth. wolf: Trying empty user-authentication request. wolf: Authentication methods that can continue: publickey,keyboard-int +eractive. wolf: Next method to try is publickey. wolf: Trying pubkey authentication with key file '/home/insaniac/.ssh/ +id_dsa' wolf: Authentication methods that can continue: publickey,keyboard-int +eractive. wolf: Next method to try is publickey. Connection failed: No such file or directory

        So when I try authenticating with an ssh-key, all works out perfectly. When I try to supply a username and a password, they seem to be ignored.. maybe we need to supply a specific value to ssh_args ?
        Per default, Net::SFTP uses the username that's executing the script as SSH username...

        I'll keep you informed if I find a solution...

        Cheers, Johnny

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

        Of course! How foolish of me... all we needed was debugging enabled ;-)

        You're remote SSH server is *probably* not accepting cleartext password tunneling. In other words, its /etc/ssh/sshd_config is saying something like:

        PasswordAuthentication no
        And that's what's causing (my) error when trying to use a username and password for authentication. When I changed it to yes, and restarted the remote SSH server, the scripts works, because debugging now said:
        wolf: Sending request for user-authentication service.
        wolf: Service accepted: ssh-userauth.
        wolf: Trying empty user-authentication request.
        wolf: Authentication methods that can continue: publickey,password,keyboard-interactive.
        
        Where it used to be (with PasswordAuthentication no):
        wolf: Sending request for user-authentication service.
        wolf: Service accepted: ssh-userauth.
        wolf: Trying empty user-authentication request.
        wolf: Authentication methods that can continue: publickey,keyboard-interactive.
        
        So the above debugging says that the remote SSH server only accepts ssh keys or a keyboard interactive password..

        My advise is: create an ssh-key for the user that runs your script, and add the public key of that user to the authorized_keys file of your remote user. This is more secure than enabling clear text password tunneling ;-)

        HTH

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