in reply to Re^6: transfer a file via SFTP
in thread transfer a file via SFTP

Strange indeed.

Still, permissions is the likely problem. Many times I have checked and re-checked permissions, only to find that I have overlooked something when I look again. Check every directory in the full path.

You say it works when the file is in the same directory as the script. Maybe the problem is on the source server where you are running the script, rather than on the remote server to which you are trying to write the script. Check the permissions on the local server also.

Replies are listed 'Best First'.
Re^8: transfer a file via SFTP
by cc (Beadle) on Jul 29, 2009 at 19:12 UTC
    I checked, the permissions on the local and remote machine are OK.
    Still cannot understand why this simple script:
    cc:/sftp/test# # cat test_sftp.cgi #!/usr/bin/perl -w use strict; use warnings; use Net::SFTP; my $file = 'TEST.txt'; my $server = 'X.X.X.X'; my $user = 'user'; my $sftp; my %args = ( ssh_args => { user => 'user', identity_files => [ '/root/.ssh/id_r +sa' ], debug => 1 } ); $sftp = Net::SFTP->new($server, %args) or die "could not open connection to $server\n"; $sftp->put($file, $file) or die "could not upload $file\n"; exit;
    cc:/sftp/test# perl test_sftp.cgi cc: Reading configuration data /root/.ssh/config cc: Reading configuration data /etc/ssh_config cc: Allocated local port 1023. cc: Connecting to X.X.X.X, port 22. cc: Remote protocol version 2.0, remote software version 4.0.7.1 SSH S +ecure Shell Windows NT Server cc: Net::SSH::Perl Version 1.34, protocol version 2.0. .inux.ch.bluee.net: No compat match: 4.0.7.1 SSH Secure Shell Windows +NT Server cc: Connection established. cc: Sent key-exchange init (KEXINIT), wait response. cc: Algorithms, c->s: 3des-cbc hmac-sha1 none cc: Algorithms, s->c: 3des-cbc hmac-sha1 none cc: Entering Diffie-Hellman Group 1 key exchange. cc: Sent DH public key, waiting for reply. cc: Received host key, type 'ssh-dss'. cc: Host 'X.X.X.X' is known and matches the host key. cc: Computing shared secret key. cc: Verifying server signature. cc: Waiting for NEWKEYS message. cc: Send NEWKEYS. cc: Enabling encryption/MAC/compression. cc: Sending request for user-authentication service. cc: Service accepted: ssh-userauth. cc: Trying empty user-authentication request. cc: Authentication methods that can continue: gssapi,publickey,passwor +d. cc: Next method to try is publickey. cc: Trying pubkey authentication with key file '/root/.ssh/id_rsa' cc: Login completed, opening dummy shell channel. cc: channel 0: new [client-session] cc: Requesting channel_open for channel 0. cc: channel 0: open confirm rwindow 100000 rmax 16384 cc: channel 1: new [client-session] cc: Requesting channel_open for channel 1. cc: Sending subsystem: sftp cc: Requesting service subsystem on channel 1. cc: channel 1: open confirm rwindow 100000 rmax 16384
    works and send a file without any problems, but if change this script to:
    my $file = '/sftp/test/TEST.txt';
    then cannot send due to the following problem:
    ............................................................... ............................................................... ............................................................... cc: Sending subsystem: sftp cc: Requesting service subsystem on channel 1. cc: channel 1: open confirm rwindow 100000 rmax 16384 Couldn't get handle: Permission denied at test_sftp.cgi line 21 could not upload /sftp/test/TEST.txt
    In both cases the file is in the same directory where the script is and the path is 100% correct.
    There is no permission problem, the script cannot handle path.

    greetings
    chris

      You are using the same path on both systems. It might help you isolate the problem if you changed the path for one end at a time.

      # cat test_sftp.cgi #!/usr/bin/perl -w use strict; use warnings; use Net::SFTP; my $from = 'TEST.txt'; my $to = '/sftp/tmp/TEST.txt'; my $server = 'X.X.X.X'; my $user = 'user'; my $sftp; my %args = ( ssh_args => { user => 'user', identity_files => [ '/root/.ssh/id_r +sa' ], debug => 1 } ); $sftp = Net::SFTP->new($server, %args) or die "could not open connection to $server\n"; $sftp->put($from, $to) or die "could not upload $file\n"; exit;

      Are you running a system with Security Enhanced (SE) Linux (e.g. RedHat)? If so, there may be restrictions on your /sftp directory and/or ssh processes preventing access. If SELinux is preventing access it will probably be logging the faults. Check /var/log/messages (probably).

        thx a lot, you are completely right.
        There are different paths and the simple script can send a file without problems.

        It seems to be a problem with the path on the remote server, I changed my original script:
        #!/usr/bin/perl -w use strict; use warnings; use File::Copy; use File::Find; use Net::Netrc; use Net::SFTP; use MIME::Lite; use Getopt::Std; use Mail::Sender; my $from = '/srv/*.txt'; my $to = '/HOME/*.txt'; my $server = 'X.X.X.X'; my $user = 'user'; my $sftp; my %args = ( ssh_args => { user => 'user', identity_files => [ '/home/.ssh/id_r +sa' ], protocol => '2,1', debug => 1, } ); my $linux = "admin\@domain.net"; my $recipient1 = "recipient1\@domain.net"; my $recipient2 = "recipient2\@domain.net"; my $recipient3 = "recipient3\@domain.net>"; # write a log BEGIN { use CGI::Carp qw(carpout); my $errorlog = "/srv/logs/transferlog.txt"; open(LOG, ">$errorlog") or die("Unable to open $errorlog: $!\n"); print LOG "Errors:\n"; carpout(*LOG); } # create backup subfolder my @dt = localtime; my $subfolder_name = ((((1900 + $dt[5]) * 100 + 1 + $dt[4]) * 100 + $d +t[3]) * 100 + $dt[2]) * 100 + $dt[1]; mkdir "/srv/OUT/$subfolder_name" or die "$subfolder_name: $!"; foreach my $from (</srv/*.txt>) { # sftp file transfer $sftp = Net::SFTP->new($server, %args) or die "could not open connecti +on to $server\n"; $sftp->put($from, $to) or die "could not upload $from\n"; # move files to the backup directory unless(move("$from", "/srv/OUT/$subfolder_name")) { print STDERR "Oops! Couldn't move the file: $!"; } move "/srv/logs/transferlog.txt", "/srv/OUT/$subfolder_name"; sleep (1 * 5) } # send a mail if transfer completed my $sender = new Mail::Sender {smtp => 'localhost', from => $linux}; $sender->MailFile({to => "$recipient1, $recipient2, $recipient3", subject => 'data transfer', msg => 'data transfer should be completed', file => "/srv/OUT/$subfolder_name/transferlog.txt"}); exit;

        but I'm still getting this problem:
        ........................... ........................... ........................... cc: Sending subsystem: sftp cc: Requesting service subsystem on channel 1. cc: channel 1: open confirm rwindow 100000 rmax 16384 [Thu Jul 30 13:40:45 2009] cc.cgi: Couldn't get handle: Failure at cc. +cgi line 60 [Thu Jul 30 13:40:45 2009] cc.cgi: could not upload /srv/28072009.txt