• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 368
  • Last Modified:

ftps file download

I am accessing an ftps server. I want to get the file that has todays date,
for example the file name starts with XXXXDownload20110312+/d example:
I login and do a pwd and go to the correct directory. I then do a list
and from the list, I use DUMPER to find the file matching the date, I then
do an ftps->get($filename) to get the file because with ftps-> get ($filename) the $filename
has to be the exact file name and in quotes for the get  work.

Three questions:

1. For the line of code Dumper($ftps->list()) with a date or a date pattern such as *20110312*
I put an output below and you will see that I can
see 16 files at time, if there are more than 16 files, I can't see beyond,
So far, the daily file has been on the displayed 16 files but I
fear that one day my daily file won't be in the displayed 16 files so I won't be able to get it.
I am not sure what the criteria is for list() to determine what 16 files it lists.
2. Is there a way to get the file using ftps->get but without the exact
filename. ftps->get(an expression for a file name), example get("*20110312*"); i've tried
this but it does not work. I don't think this is supported.
3. given the snippet of code can you suggest improvements?



use Data::Dumper;
use Net::FTPSSL;

my $ftps = Net::FTPSSL->new('x.y.z.w',
                              Port => 10021,
                           #   Encryption => 'E',
                              Debug => 1,
                              Croak => 1)
    or die "Can't open ftps";
$ftps->login('abcd', 'xyz');
$ftps->cwd("download_dir") or die "cannot go to the path: ", $ftps->last_message;
my $ret = Dumper($ftps->list())
    or error("Command error, $ftps->last_message");

foreach my $file (split(/;/,$ret)){
  print ($file."/n");

! here I hardcoded to get the file for march 24th
  if ($file =~ /\w+20110324\d+/)
     my $result = $&;
     print "my result is: $result\n";
     $result =~ s/\/n//;
     print "my result updated is $result\n";
     $results = $result.".xml".".gz";
     print "my results is $results\n";
     $results =~ s/\/n//;
     print "my results updated is $results\n";

$ftps->get($results) or die "cannot get the file :", $ftps->last_message;

for #1 here is an example of output from list() using snippet.

<<< 226 Directory send OK.
$VAR1 = '-rw-r--r--    1 516      501         11115 Mar 12 00:01 ABCDEDownload20110312020109.xml.gz'/n
$VAR2 = '-rw-r--r--    1 516      501          4760 Mar 13 00:01 ABCDEDownload20110313020107.xml.gz'/n
$VAR3 = '-rw-r--r--    1 516      501          2442 Mar 14 00:01 ABCDEDownload20110314020109.xml.gz'/n
$VAR4 = '-rw-r--r--    1 516      501         13629 Mar 15 00:01 ABCDEDownload20110315020103.xml.gz'/n
$VAR5 = '-rw-r--r--    1 516      501         10807 Mar 16 00:01 ABCDEDownload20110316020137.xml.gz'/n
$VAR6 = '-rw-r--r--    1 516      501         11081 Mar 17 00:01 ABCDEDownload20110317020116.xml.gz'/n
$VAR7 = '-rw-r--r--    1 516      501         18348 Mar 18 00:02 ABCDEDownload20110318020200.xml.gz'/n
$VAR8 = '-rw-r--r--    1 516      501          8885 Mar 19 00:01 ABCDEDownload20110319020127.xml.gz'/n
$VAR9 = '-rw-r--r--    1 516      501          4597 Mar 20 00:01 ABCDEDownload20110320020057.xml.gz'/n
$VAR10 = '-rw-r--r--    1 516      501          1684 Mar 21 00:03 ABCDEDownload20110321020301.xml.gz'/n
$VAR11 = '-rw-r--r--    1 516      501          1387 Mar 22 00:02 ABCDEDownload20110322020220.xml.gz'/n
$VAR12 = '-rw-r--r--    1 516      501         12222 Mar 23 00:01 ABCDEDownload20110323020120.xml.gz'/n
$VAR13 = '-rw-r--r--    1 516      501          8665 Mar 24 00:01 ABCDEDownload20110324020111.xml.gz'/nmy result is: ABCDEDownload20110324020111
my result updated is ABCDEDownload20110324020111
my results is ABCDEDownload20110324020111.xml.gz
my results updated is ABCDEDownload20110324020111.xml.gz
  • 4
  • 2
3 Solutions
Well if you are not determined to do it as a unix perl script and instead you just want to get it done now as in 5 minutes ago you could do run the Robo-FTP command script below as a service on a Windows box.

Robo-FTP can parse the directory listing returned by the remote FTPS server to find the file you you actually want to download..
LOG "daily_script.log"
TRACELOG "daily_trace.log"
SETRIGHT year = %datetime 4
SETEXTRACT month = %date "-" 1
SETEXTRACT day = %date "-" 2
SET filespec = "*Download" + year + month + day + "*"
LOGMSG filespec  ;; this may be helpful for debugging
FTPLOGON "" /user="userID" /pw="secret" /servertype=FTPS /trust=all
RCVFILE filespec
CRON "crontab.txt"  
GOTO loop_top

Open in new window

You'll need to put the server's dns name or address and your username and password on that row with the FTPLOGON command.
The crontab.txt file would be in the Robo-FTP install folder and would contain one line... for example to have it run at 30 minutes after 6am every day the one line would be:
30 6 * * *

Open in new window

The way this code is structured now it will run once when you first kick it and then at 6:30am each day after.  To change it so it waits for the next 6:30 before running just move the CRON line from down near the bottom to up near the top right after the top of the loop and right before the row where you get the current year.

Sorry if you really need a perl solution and this post wasn't helpful.
Johannne1Author Commented:
Hi AlexPace,
Can I use this in a korn script? I already have a korn script that was calling my perl script.
I'm sorry this is Robo-FTP command script language so its no good to you unless you have a WinDoze box.  
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Johannne1Author Commented:
No comment
Johannne1Author Commented:
Can you please take this question off my list.
Johannne1Author Commented:
I tried to cancel this question many times. I wrote twice to the help desk to two different addresses but I was unssucessful. I answered my own question within the question. The solution was helpful but PC based instead of unix based.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now