Hi Monks

I am trying to use Net::SFTP to send a file from one server to another.

The script I have written works fine when called from the command line, but wont work when called from a web browser.

Originally I got an error and had to create the directory /var/www/.ssh for the apache user. Now I don't get any errors, it just hangs.

I assumed it was to do with permissions, so I tried a couple of things. I added the apache user (temporarily of course) to the root group, but that didn't fix it. So then I gave the apache user shell access (temporarily), su'ed to apache, ran the script and it worked. I can't work out why it won't work when run from a web browser. Code is below:

#!/usr/bin/perl -w use Net::SFTP; my $upload_dir = "/var/www/vhosts/domainname/httpsdocs/companyname/t +mpfiles"; print "Content-type: text/plain\n\n"; my $filename = "2007-03-27-0088.PDF"; my $sftp = undef; my %args = (user => 'username', password => 'password', debug => 1); eval{$sftp = Net::SFTP->new("domainname", %args);}; if ($@) { print "Error: cannot connect to SFTP server. $@\n"; exit(); } eval{$sftp->put("$upload_dir/$filename", "temp/$filename");}; if ($@) { print "Error: cannot copy file to the SFTP server\n$@\n"; exit(); } print "Complete\n";

The debug output is as follows:

Reading configuration data /.ssh/config Reading configuration data /etc/ssh_config Connecting to domainname, port 22. Remote protocol version 1.99, remote software version OpenSSH_4.6 Net::SSH::Perl Version 1.34, protocol version 2.0. No compat match: OpenSSH_4.6. Connection established. Sent key-exchange init (KEXINIT), wait response. Algorithms, c->s: 3des-cbc hmac-sha1 none Algorithms, s->c: 3des-cbc hmac-sha1 none Entering Diffie-Hellman Group 1 key exchange. Sent DH public key, waiting for reply. Received host key, type 'ssh-dss'. Host 'domainname' is known and matches the host key. Computing shared secret key. Verifying server signature. Waiting for NEWKEYS message. Send NEWKEYS. Enabling encryption/MAC/compression. Sending request for user-authentication service. Service accepted: ssh-userauth. Trying empty user-authentication request. Authentication methods that can continue: publickey,password. Next method to try is publickey. Next method to try is password. Trying password authentication. Login completed, opening dummy shell channel. channel 0: new [client-session] Requesting channel_open for channel 0. channel 0: open confirm rwindow 0 rmax 32768 channel 1: new [client-session] Requesting channel_open for channel 1. Sending subsystem: sftp Requesting service subsystem on channel 1. channel 1: open confirm rwindow 0 rmax 32768 sftp: Sending SSH2_FXP_INIT

Any help will be greatly appreciated. I found a few people on google who experienced it hanging at the same point as me (sftp: Sending SSH2_FXP_INIT), but no solutions.

Many thanks

James


In reply to SFTP is not working when called through a browser by Anonymous Monk

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.