Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

How could i FTP specific file from remote system ?

by bh_perl (Monk)
on Sep 20, 2006 at 08:16 UTC ( [id://573858]=perlquestion: print w/replies, xml ) Need Help??

bh_perl has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Anybody could help me how could i FTP and GET specific file from the remote system ?

As example, there are a lot of files in remote system such as :-

nokc10_1810_200609201437.mss nokc10_1811_200609201508.mss ... tecc10_1810_200609201437.mss tecc10_1811_200609201508.mss ...

And I want to GET all the nok* files, such as :-

nokc10_1810_200609201437.mss nokc10_1811_200609201508.mss ...

Previously I tried to copy all the listed files in remote system into specify array but my memory are not enough to captured it, as example :-

my @list = $ftp->ls; print "TEST: @list\n";

20060920 Janitored by Corion: Added formatting, code tags, removed PRE tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: How could i FTP specific file from remote system ?
by Eyck (Priest) on Sep 20, 2006 at 12:19 UTC
    use Net::Lite::FTP; my $tlsftp=Net::Lite::FTP->new(); $tlsftp->open("ftp.tls.pl","21"); $tlsftp->user("user"); $tlsftp->pass("password"); $tlsftp->cwd("sth"); my $files=$tlsftp->list("nok*"); foreach $f (@$files) { print "Getting file: $f\n"; $tlsftp->get($f); };

    This assumes FTP servers support wildcards, when there were exploits for wildcards in the wild, some FTP servers would turn off this feature, thus, you would like to list all the files and then grep it on client side - this might require some memory if the list is large. (if you know that full list is too big to fit in memory, but your wildcarded list fits fine, you can try grepp-ing the list on-line).

    If the list is really huge, and even your smaller list wouldn't fit in memory, you would need two FTP connections - one for list, and the other for files.

Re: How could i FTP specific file from remote system ?
by rodion (Chaplain) on Sep 20, 2006 at 12:34 UTC
    I think you're looking for something along the lines of the following:
    use Net::FTP; $ftp = Net::FTP->new($host, Debug => 0) or die "Cannot connect to $host $@"; $ftp->login($user_name,$password) or die "Cannot login ", $ftp->message; $ftp->cwd($target_dir) or die "Cannot cd to $target_dir", $ftp->message; my @files = $ftp->ls('nok*'); for (@files) { $ftp->get($_) or die "couldn't get $_", $ftp->message; ... do something with the file ... } $ftp->quit;
    Check the response from the ls command when you debug; for some hosts you may need to rework the ls output to get it into the right format.
Re: How could i FTP specific file from remote system ?
by rinceWind (Monsignor) on Sep 20, 2006 at 11:27 UTC

    I presume that you are using Net::FTP, but this isn't clear. Please post more of your code.

    To answer your specific question, you want to use the nlst or list methods, and read the file names individually from the dataconn object that these methods return.

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

Re: How could i FTP specific file from remote system ?
by samizdat (Vicar) on Sep 20, 2006 at 13:52 UTC
    You can also set up for the system to do it for you. Read the docs for the ftp command and the netrc file.

    Here's a sample netrc:
    machine foo.network-lynx.net login don password zebra macdef init cd /usr/local/test_data/reports binary mget *599* exit
    and the command that invokes it:
    system("ftp -in -N /var/web/scripts/netrc don@foo.network-lynx.net") && die "Couldn't complete netrc ftp! $!\n";
    Note that the netrc file MUST ONLY be readable by the user that invokes the script.

    Don Wilde
    "There's more than one level to any answer."
Re: How could i FTP specific file from remote system ?
by aquarium (Curate) on Sep 20, 2006 at 09:04 UTC
    hi. first you want to turn off prompting with "prompt" ftp command. then you can use the ftp "mget" command and specify nok*. you probably also want to set binary/ascii mode before transfer as well.
    the hardest line to type correctly is: stty erase ^H

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://573858]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-16 23:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found