cURL / Mailing Lists / curl-users / Single Mail

curl-users

libcurl (perl) FTP download using wildcards

From: Miguel Laborde <Miguel.Laborde_at_qlogitek.com>
Date: Thu, 8 May 2003 14:03:02 -0400

Hello all,

        I've just started working with libcurl on perl today and I'm having some difficulties. Right now I have to write a script that will connect to an ftp server and to download files. After much playing I've realized that one specifies either the directory to cd into, or the file to download by setting that with the CURLOPT_URL option. My problem is this, I wish to specify a wildcard, *, when referencing the files to download as I don't know ahead of time what the names are.

        So what I've tried doing is to set the CURLOPT_URL just to the directory that the files will be located in and then using the CURLOPT_POSTQUOTE options I'm trying to get a diretory listing of the files in there, but this is failing. Here is a copy of my script (Login and password are hidden):

        use strict;
use Curl::easy;
use Data::Dumper;

use constant DEBUG => 1;

# Variables
my @body;
my $address = 'ftp://rezox.com/carlos/';
my $login = '****'
my $password = '****

# Init the curl session
my $curl= Curl::easy->new() or die "curl init failed!\n";

# Verbose debugging
$curl->setopt(CURLOPT_VERBOSE, 1) if(DEBUG);

# Set curl parameters
$curl->setopt(CURLOPT_URL, $address);
$curl->setopt(CURLOPT_USERPWD, $login.':'.$password);
$curl->setopt(CURLOPT_FTP_USE_EPSV, 0);
open ERRFH, ">err.txt" && $curl->setopt(CURLOPT_STDERR, *ERRFH);

sub body_callback {
    my ($chunk,$context)=@_;
    push @{$context}, $chunk;
    return length($chunk); # OK
}
$curl->setopt(CURLOPT_WRITEFUNCTION, \&body_callback);
$curl->setopt(CURLOPT_FILE, \@body);
$curl->setopt(CURLOPT_STDERR, *ERRFH);

my @commands = 'LIST';
$curl->setopt(CURLOPT_POSTQUOTE, \@commands);

$curl->perform();
print join("", @body);

------- OUT PUT

* About to connect() to rezox.com:21
* Connected to rezox.com (216.12.212.200) port 21
220 ProFTPD 1.2.4 Server (FTPd) [216.12.212.200]
> USER ****
331 Password required for ****.
> PASS ****
230 User **** logged in.
* We have successfully logged in
> PWD
257 "/" is current directory.
* Entry path is '/'
> CWD carlos
250 CWD command successful.
> PASV
227 Entering Passive Mode (216,12,212,200,224,167).
* About to connect() to rezox.com:57511
* Connecting to 216.12.212.200 (216.12.212.200) port 57511
* Connected the data stream with PASV!
> TYPE A
200 Type set to A.
> LIST
150 Opening ASCII mode data connection for file list
* Getting file with size: -1
226 Transfer complete.
> list
425 Can't build data connection: Connection refused
* QUOT string not accepted: list
* Connection #0 left intact

<SNIPPED THE DIRECTORY LISTING THAT CURL AUTOMATICALLY PRODUCES>

So I'm getting an error when I try and do a directory listing... In fact I'm getting an error with anything that requires me opening up a data stream.. ANy idea what I'm doing wrong?

                Thanks! :)

-------------------------------------------------------
Enterprise Linux Forum Conference & Expo, June 4-6, 2003, Santa Clara
The only event dedicated to issues related to Linux enterprise solutions
www.enterpriselinuxforum.com
Received on 2003-05-08