Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ftp problem

Status
Not open for further replies.

sasuser2006

Technical User
May 8, 2006
32
US
I'm trying to log into an ftp server to monitor for a file but when I do an ftp->ls it's not returning anything. Any help would be much appreciated.

Code:
#!/usr/bin/perl

use Net::FTP;
my $now = localtime;
my $report_path = "/u01/home/ftp.log";
my $path = "path";
my $ftpdest = "host";
my $ftpuser = "user";
my $ftppass = "pass";

open (REPORT, ">$report_path");
print REPORT "It is now $now and the Outbound script has started!\n";
print REPORT "At $now connection to $ftpdest has started\n";

my $ftp;
$ftp = Net::FTP -> new($ftpdest, Debug => 0 ) || print REPORT "At $now connection to $ftpdest failed!\n";
print REPORT "At $now connection has been established\n";
$ftp->login($ftpuser,$ftppass) || print REPORT "At $now login attempt to $ftpdest failed!\n";
print REPORT "At $now login was successful!\n";
$ftp->cwd($path) || print REPORT " At $now change path to $path failed!\n";
$ftp->binary;
print REPORT "At $now the transfer method was changed to binary\n";
my $curr_dir = $ftp->pwd;
print REPORT "Current working directory is $curr_dir\n";
@all_files = $ftp->ls || print REPORT "Unable to list files";
foreach $file (@all_files){
$ftp-> get($file);
}
print REPORT "Downloaded $file!!!\n";
print REPORT "$file\n";
$ftp->close();
print REPORT "At $now the Outbound script completed!\n";
close REPORT;
exit;


The report looks like this...
Code:
It is now Thu Jul 20 18:30:25 2006 and the Outbound script has started!
At Thu Jul 20 18:30:25 2006 connection has started
At Thu Jul 20 18:30:25 2006 connection has been established
At Thu Jul 20 18:30:25 2006 login was successful!
At Thu Jul 20 18:30:25 2006 the transfer method was changed to binary
Current working directory is path
Downloaded !!!

At Thu Jul 20 18:30:25 2006 the Outbound script completed!

I can't get the ftp->ls to list the files in the current directory. In case you're wondering the path returned is valid but for privacy purposes I've replace it. Any advice?
 
Not sure if this matters as I's still a bit of a newbie here, but I have a similar ftp script, and when I list the files to retrieve I use this.

@files = $ftp->ls();

and mine works fine. I wonder if you need the () after ls.

 
Also a newbie...thanks for the quick post but I've put () after the ls and still returns nothing.

I've manually posted the files to the ftp site and can see them when I log in.

At first I thought it was a path error but the report returns the correct path when the $ftp->pwd; command is ran.

I'm really lost here.
 
try this, after this line:

@all_files = $ftp->ls || print REPORT "Unable to list files";

insert:

print "$_\n" for @all_files;

or:

print REPORT "----Listing files----\n";
print REPORT "$_\n" for @all_files;
print REPORT "----End of list----\n";

the list is probably more than just the filenames, so trying to use the list to download the files may not work as you expect.
 
Thanks for the reply!

Unfortunately the problem still exists and here is the new error log...

Code:
---Listing File---

 for ARRAY(0x881dbc0)
---End of list---
Downloaded !!!

Any other suggestions...
 
I just tried a simple script and it worked fine. the ls() function does return a list of just the file/folder names.

Code:
#!/usr/bin/perl
use strict;
use warnings;
use CGI::Carp qw( fatalsToBrowser );
use CGI;
use Net::FTP;
my ($user,$password) = ('*****','*****');
my $ftp=Net::FTP->new("[URL unfurl="true"]www.exmaple.com",[/URL] Debug => 1);
my $html = new CGI;
print $html->header;
if ($ftp->login($user,$password)){
print "login successful<br>";
}
else {
print "login failed<br>";
}
my @all_files = $ftp->ls;
print "$_\n<br>" for @all_files;
$ftp->quit;

repost the exact code you are now using that generated the output you posted above. This looks like a reference to an array: ARRAY(0x881dbc0) not sure why you are getting that instead of a regular list.
 
Thanks for continuing to look at this. The exact code is...

Code:
#!/usr/bin/perl

use Net::FTP;

my $now = localtime;
my $report_path = "/u01/home/ftp.log";
my $path = "/***/***/***/";
my $ftpdest = "***";
my $ftpuser = "***";
my $ftppass = "***";

open (REPORT, ">$report_path");
print REPORT "It is now $now and the Outbound script has started!\n";
print REPORT "At $now connection has started\n";

my $ftp;
$ftp = Net::FTP -> new($ftpdest, Debug => 0 ) || print REPORT "At $now connection to $ftpdest failed!\n";
print REPORT "At $now connection has been established\n";
$ftp->login($ftpuser,$ftppass) || print REPORT "At $now login attempt to $ftpdest failed!\n";
print REPORT "At $now login was successful!\n";
$ftp->cwd($path) || print REPORT " At $now change path to $path failed!\n";
$ftp->binary;
print REPORT "At $now the transfer method was changed to binary\n";
my $curr_dir = $ftp->pwd;
print REPORT "Current working directory is $curr_dir\n";
my @all_files = $ftp->ls() || print REPORT "Unable to list files";
print REPORT "$_\n<br>" for @all_files;
print REPORT "---Listing File---\n";
print REPORT "$_\n for @all_files\n";
print REPORT "---End of list---\n";
foreach $file (@all_files){
$ftp-> get($file);
}
print REPORT "Downloaded $file!!!\n";
print REPORT "$file\n";
$ftp->quit;
print REPORT "At $now the Outbound script completed!\n";
close REPORT;
exit;

The new ftp.log file reads...

Code:
It is now Fri Jul 21 08:17:23 2006 and the Outbound script has started!
At Fri Jul 21 08:17:23 2006 connection has started
At Fri Jul 21 08:17:23 2006 connection has been established
At Fri Jul 21 08:17:23 2006 login was successful!
At Fri Jul 21 08:17:23 2006 the transfer method was changed to binary
Current working directory is /***/***/***
ARRAY(0x8ccfe8c)
<br>---Listing File---

 for ARRAY(0x8ccfe8c)
---End of list---
Downloaded !!!

At Fri Jul 21 08:17:23 2006 the Outbound script completed!
 
try again, but change this line:

Code:
print REPORT "$_\n for @all_files\n";

to:

Code:
print REPORT "$_\n" for @all_files;
 
I've made the change and the log file is still returning the array error...

ARRAY(0x8ccfe8c)

I'm not understanding this...it's not too complicated but for some reason it won't work.

Any other suggestions???
 
Sorry, I am stumped. It looks like it should work if there are in fact files/folders to list in the directory you are requesting the list from.
 
There are files in the directory so I as well am stumped. Thanks for all your help on this though.
 
Not sure if you have gotten this to work yet. Here is something that you can try. I was not able to get it to work with the code in blue, but was able to set a variable and used that to determine the next steps.

let me know if this works for you.

Code:
#!/usr/bin/perl

use Net::FTP;

my $now = localtime;
my $report_path = "/u01/home/ftp.log";
my $path = "/***/***/***/";
my $ftpdest = "***";
my $ftpuser = "***";
my $ftppass = "***";

open (REPORT, ">$report_path");
print REPORT "It is now $now and the Outbound script has started!\n";
print REPORT "At $now connection has started\n";

my $ftp;
$ftp = Net::FTP -> new($ftpdest, Debug => 0 ) || print REPORT "At $now connection to $ftpdest failed!\n";
print REPORT "At $now connection has been established\n";

$ftp->login($ftpuser,$ftppass) || print REPORT "At $now login attempt to $ftpdest failed!\n";
print REPORT "At $now login was successful!\n";

$ftp->cwd($path) || print REPORT " At $now change path to $path failed!\n";

$ftp->binary;
print REPORT "At $now the transfer method was changed to binary\n";

my $curr_dir = $ftp->pwd;
print REPORT "Current working directory is $curr_dir\n";

[blue]#my @all_files = $ftp->ls() || print REPORT "Unable to list files";[/blue]
my @all_files = $ftp->ls or $newerr=1;

if ( not $newerr) 
   {
      print REPORT "$_\n<br>" for @all_files;
      print REPORT "---Listing File---\n";
      print REPORT "$_\n for @all_files\n";
      print REPORT "---End of list---\n";

      foreach $file (@all_files)
      {
         $ftp-> get($file);
         print REPORT "Downloaded $file!!!\n";
      }

      print REPORT "$file\n";
   }
else
 { print REPORT "Unable to list files" if $newerr; }


print REPORT "$file\n";

$ftp->quit;
print REPORT "At $now the Outbound script completed!\n";

close REPORT;

exit;
 
Sorry I forgot to repost and tell everybody that something about the || print REPORT was messing it up. I took that out and it worked.

On a side note I've got a new problem...

For the below statement...

Code:
      foreach $file (@all_files)
      {
         $ftp-> get($file);
         print REPORT "Downloaded $file!!!\n";
      }

In some cases the $file comes in as "Test File.txt" with a space which is causing problems later on. I've tried to do...
Code:
s/\x20/\x5F/g
But for some reason it's not replacing the spaces with '_'. Any suggestions to replacing spaces in $file where $file is the file name and not removing spaces within the file itself.

Thanks in advance.
 
I have modified the script to save the current filename, convert the space to Underscores, then compare them to see which type of ftp needs to be done.

check this out and see if it does what is needed.

Code:
      foreach $file (@all_files)
      {
         $file1 = $file;                    # save filename before converting      
         $file1 =~ s/\x40/\x5F/g;           # convert space to _
         if ($file eq file1)                # compare filenames
            {                               # if equal then get just the file
               $ftp-> get($file);
               print REPORT "Downloaded $file!!!\n";
            }
         else                 
            {                               # else get filename as new filename 
                $ftp-> get($file,$file1);
                print REPORT "Downloaded $file as $file1 !!!\n";
            }    
      }
 

The only thing that I see that is missing in this script is to add some error checking around the FTP to report if i can not find the file after the listing is made.

Should be a simple addition and no problem, was good to get back into doing some Perl Scripting agian.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top