0

I am trying to send an input to ftp opened handle in Perl. My script is

open (CALC, "|ftp abc.cde.com");
print "hello";
close CALC;

The website will only accept anonymous users as input. I have tried using $guess = <STDIN>; but it wont work. How can i force Perl to ask for user as well as hard coding a user?

1

1 Answer 1

4

You would probably save alot of code and frustration using this module. Net::FTP is a class implementing a simple FTP client in Perl as described in RFC959. It provides wrappers for a subset of the RFC959 commands.

use Net::FTP;

$ftp = Net::FTP->new("some.host.name", Debug => 0)
  or die "Cannot connect to some.host.name: $@";

$ftp->login("$user","$Password")
  or die "Cannot login ", $ftp->message;

$ftp->cwd("/pub")
  or die "Cannot change working directory ", $ftp->message;

$ftp->get("that.file")
  or die "get failed ", $ftp->message;

$ftp->quit;

As for the user prompt, if you can use STDIN the following should work.

print "Enter username or press enter for anonymous: ";
my $user = <STDIN>; 
chomp $user;

if (length($user //= '')) {$user="anonymous";$password="INSERTPASSWORD"}
else {
print "Enter password for $user: ";
my $password = <STDIN>; 
chomp $password;
}

I would request the user and password as early as possible in your script, and then you can pass it to the $ftp object. Using the module is better then using exec or pipes as it will have built in error reporting and is going to be easy to OO.

Hope this helps and comment if you need more help.

Sign up to request clarification or add additional context in comments.

1 Comment

I'll second that. FTP is a hideous protocol to re-implement by hand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.