Reputation: 1
For testing purposes on my end, I need to write a program that uses [Net::FTP
][Net::FTP] to connect to a server, and then receives a file in a certain directory. Once it is received, put it right back in that same spot.
Here is my code:
#!/usr/bin/perl
use Net::FTP;
$host = "serverA";
$username = "test";
$password = "ftptest123";
$ftpdir = "/ftptest";
$file = "ftptest.txt";
$ftp = Net::FTP->new($host) or die "Error connecting to $host: $!";
$ftp->login($username,$password) or die "Login failed: $!";
$ftp->cwd($ftpdir) or die "Can't go to $ftpdir: $!";
$ftp->get($file) or die "Can't get $file: $!";
$ftp->put($file) or die "Can't put $file: $!";
$ftp->quit or die "Error closing ftp connection: $!";
Any ideas on how to go about this? It seems to run fine, but when it hits the put
statement it shoots out this at me:
[Net::FTP]: https://metacpan.org/module/Net::FTP
Upvotes: 0
Views: 2747
Reputation: 126722
First of all you should always use strict
and use warnings
, and declare all your variables at their first point of use using my
. That way many trivial errors that you would otherwise ovrelooked will be highlighted for you.
The documentation for Net::FTP
is incomplete in that it doesn't supply any information on the message
method. However it is clear from the synopsis that information on any error can be accessed using $ftp->message
.
Of course this doesn't apply to the constructor, as if that fails there is no object to provide the message
method, so in this case the information appears in the built-in variable $@
.
Try this variation on your program. It will probably tell you immediately why it is failing.
#!/usr/bin/perl
use strict;
use warnings;
use Net::FTP;
my $host = 'serverA';
my $username = 'test';
my $password = 'ftptest123';
my $ftpdir = '/ftptest';
my $file = 'ftptest.txt';
my $ftp = Net::FTP->new($host) or die "Error connecting to $host: $@";
$ftp->login($username,$password) or die "Login failed: ", $ftp->message;
$ftp->cwd($ftpdir) or die "Can't go to $ftpdir: ", $ftp->message;
$ftp->get($file) or die "Can't get $file: ", $ftp->message;
$ftp->put($file) or die "Can't put $file: ", $ftp->message;
$ftp->quit or die "Error closing ftp connection: ", $ftp->message;
Upvotes: 1
Reputation: 3498
check the error message in $ftp->message
, not in $!
. It'll probably tell you you don't have wrote access to the directory, or aren't allowed to overwrite an existing file...
Upvotes: 1