Operating System - Linux
1827243 Members
2385 Online
109716 Solutions
New Discussion

can not get perl Net:FTP remote file listing to work

 
SOLVED
Go to solution
Steve Post
Trusted Contributor

can not get perl Net:FTP remote file listing to work

I want to use perl to connect to a remote ftp site and get a list of files in a remote directory.

I want a simple list of files (ls -1). I don't want a long listing "ls -l" command.
1 (aka ONE) is different from l (aka L).

I can't seem to get any output from nlist in the perl module.
my $ftp = Net::FTP->new($hostname) || die "cant connect to host" ;
$ftp->binary();
$ftp->login($username,$password) || die "cant login to host";
print $ftp->cwd($DIR);
foreach ( $ftp->nlst ) {
print $_;
}

Instead of a list from this $ftp->nlst, I get a binary memory address. I know the answer should be simple. But I can't find it.

PS: In the event this is posted twice....sorry. My internet access blew up about 15 minutes ago. I just got it back.
4 REPLIES 4
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: can not get perl Net:FTP remote file listing to work

I would make a small change and note that $ftp->ls() returns a list of filenames and that $ftp->dir() is the ls -l equivalent.


use constant GOOD_BOY => 2;
my $stat = 0;
my $ftp = Net::FTP->new($hostname) || die "cant connect to host" ;
$ftp->binary();
$ftp->login($username,$password) || die "cant login to host";
$ftp->cwd($DIR);

my @a = $ftp->ls();
$stat = $ftp->status;
if ($stat == GOOD_BOY)
{
my $i = 0;
while ($i <= $#a)
{
printf ("%s\n",$a[$i]);
++$i;
}
}
else
{
printf STDERR ("ls failed. status %d\n",
$stat);
}
$ftp->quit;
}

If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: can not get perl Net:FTP remote file listing to work

Hi Steve:

Using:

...
foreach ($ftp->ls) {
...

returns the list of filenames without additional information.

You can use 'for' as a shortened notation in lieu of 'foreach', too.

Regards!

...JRF...
Steve Post
Trusted Contributor

Re: can not get perl Net:FTP remote file listing to work

I want to assign 10 points to both spots because the answers were great.

Unfortunately everytime I try to "submit points" the website comes back with "service unavailable."

Hopefully this message will provide some sort of anti-jinx to that button.

Steve Post
Trusted Contributor

Re: can not get perl Net:FTP remote file listing to work

a ha. I see it worked. Who would expect some type of voodoo mumbo jumbo would work on the website.