Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

transfer a file via SFTP

by cc (Beadle)
on Jul 22, 2009 at 18:12 UTC ( [id://782397]=perlquestion: print w/replies, xml ) Need Help??

cc has asked for the wisdom of the Perl Monks concerning the following question:

hi

I have written this perl script to transfer a *.txt file via SFTP.

It should:
1.) create and write log file.
2.) create backup subfolder.
3.) transfar *.txt file via sftp.
4.) move *.txt file and log file to the backup directory.
5.) send a mail with the log file if transfer completed.
#!/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 $server = "X.X.X.X"; my $user = "myuser"; my $password = ""; my %args = (ssh_args => []); $args{debug} = 1; $args{user} = "myuser"; # file my $file = "/srv/*.txt"; # ftp directories my $remote_directory = ""; my $linux = "admin\@domain.net"; my $recipient1 = "recipient1\@domain.net"; my $recipient2 = "recipient2\@domain.net"; my $recipient3 = "recipient3\@domain.net>"; # write the 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 $file (</srv/*.txt>) { # sftp transfer my $sftp=Net::SFTP->new($server, %args) or die "could not open + connection to $server\n"; # change remote directory for the first file # $sftp->cd($remote_directory); # $sftp->binary; $sftp->put($file, $file) or die "could not upload a file\n"; #quit SFTP #$sftp->quit; # move to the backup directory unless(move("$file", "/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 the mail, when 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 the *.txt file is not transfered and I'm getting this problem:
linux: Next method to try is publickey. Permission denied at /usr/local/share/perl/5.10.0/Net/SFTP.pm line 62

I have permission to this server and to transfer manually works well.
What's wrong and howto solve this problem?
THX

Replies are listed 'Best First'.
Re: transfer a file via SFTP
by ig (Vicar) on Jul 23, 2009 at 01:01 UTC

    I get the same permission denied error, except line 62 instead of line 51 (perhaps different versions of Net::SFTP??), when username and/or password are incorrect.

    On CentOS5 linux ssh logins are logged to /var/log/secure. On a failed login attempt with an invalid user I get entries like the following:

    I suggest you review the logs on your ssh server to determine whether the user authentication is succeeding and the cause of any authentication failure.

    update: Don't know what made me think the error message cc received was indicating line 51.

      THX, but still cannot find a solution for this problem.
      I've created my public and private keys using:
      # ssh-keygen -t rsa
      and the public key id_rsa.pub from the client machine was installed on the sftp server.
      The user name is absolutely correct.
      I can login from the command line to this server and transfer a file without problems:
      # sftp myuser@X.X.X.X Connecting to X.X.X.X... sftp> put TEST.txt Uploading TEST.txt to /HOME/TEST.txt TEST.txt 100% 22 0.0KB/s 00:00 sftp> ls TEST.txt sftp> rm TEST.txt Removing /HOME/TEST.txt sftp> quit
      I cannot understand why this perl script above doesn't work.

        The ssh logs from your server should be very helpful in determining why permission is denied.

        You have generated an RSA key but Net::SSH::Perl, on which Net::SFTP is based, uses DSA by default. By default it appears to ignore RSA keys. The version I have also ignores ~/.ssh/identity, despite the documentation that this is one of the default identity files. Therefore, it appears necessary to specify an RSA identity file explicitly.

        I have done this with the following configuration:

        my %args = (ssh_args => { identity_files => [ "/home/username/.ssh/id_ +rsa" ], } ); $args{debug} = 1; $args{user} = "username";

        With the identity_files argument to Net::SSH::Perl specified, as above, the id_rsa file was read and authentication completed successfully.

        Alternatively, you can create a DSA identity file with ssh-keygen -t dsa. Net::SSH::Perl will use a DSA key named $ENV{HOME}/.ssh/id_dsa by default.

Re: transfer a file via SFTP
by salva (Canon) on Jul 30, 2009 at 07:22 UTC
      Is there any issue with Net::SFTP? Just confused
        Yes, the module is mostly unmaintained, there have been just a couple of releases since 2005 and also its API is rather limited, hard to use and full of subtle gotchas.

        Also, at the time I posted my answer, the state of Net::SSH::Perl (which is used by Net::SFTP under the hood) was quite similar, around 2014 Steffen Schwigon took it over and between him and Lance Kinley, they did and incredible work renovating the module, adding support for modern algorithms (interoperability at that point was quite bad) and in general, just improving it... but then the latest release was in 2017.

        It is a different module with an entirely different back-end. See Net::SFTP::Foreign Vs. Net::SFTP Vs. Net::SSH2::SFTP for a discussion of the differences and similarities.

        As a tip to the unwary, this thread was started over a decade ago and therefore some of the details in other posts may be out of date and/or of historical interest only.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-24 11:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found