Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

FTP Transfer

by o0lit3 (Friar)
on Jun 01, 2004 at 18:46 UTC ( #358380=perlquestion: print w/replies, xml ) Need Help??

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

Fellow Monks,

Several years ago, I wrote a script to be executed daily that would grab a file via ftp and store it on a local machine. I remember at the time attempting several different means by which to do this, and ending up with the code below.

The problem was that I couldn't figure out how to simply open a single ftp port and save the file. Instead, (and I don't what possessed me to even try this), I opened two ports, the second of which pointed to the very machine that was executing the script.

Now, I've been presented with a task that requires the same type of file transfer, but I want to do it the right way. However, before I reinvent the wheel, I have a few questions.

1) Are there problems with the code below that makes this method worse than any other method?

2) Would the script below also work if it were being executed on a third machine (a benefit that may weigh on my decision to rewrite this)?

3) Why does this work? I'm baffled that I'm able to save a file this way as it seems there should be some sort of security violation.

use IO::Socket; ... ... $sender = IO::Socket::INET->new(Proto=>"tcp", PeerAddr=>"", + PeerPort=>"ftp(21)", Timeout=>10) || return(0); $receiver = IO::Socket::INET->new(Proto=>"tcp", PeerAddr=>" +", PeerPort=>"ftp(21)", Timeout=>10) || return(0); ... ... sub ftpGrab { ## Login to remote sender. until (<$sender> =~ m/220/) {print;} print $sender "USER xxx\r\n"; $reply = <$sender>; return(0) if ($reply !~ m/331/); print $sender "PASS xxx\r\n"; $reply = <$sender>; return(0) if ($reply !~ m/230/); ## Login to receiver. until (<$receiver> =~ m/220/) {print;} print $receiver "USER yyy\r\n"; $reply = <$receiver>; return(0) if ($reply !~ m/331/); print $receiver "PASS yyy\r\n"; $reply = <$receiver>; return(0) if ($reply !~ m/230/); return(0) if (!grabData("xxx", "yyy")); ## Close socket connections. close($sender); close($receiver); return(1); } sub grabData { ## Declare local variables. my $port; my $rest; ## Open passive port on receiver and store port. print $receiver "PASV\r\n"; $reply = <$receiver>; return(0) if ($reply !~ m/227/); ($rest, $port, $rest) = split(/[()]/, $reply); ## Open data port on remote sender. print $sender "PORT $port\r\n"; $reply = <$sender>; return(0) if ($reply !~ m/200/); ## Change data transfer type to ASCII on remote sender. print $sender "TYPE A\r\n"; $reply = <$sender>; return(0) if ($reply !~ m/200/); ## Send file from remote sender. print $sender "RETR $_[0]\r\n"; $reply = <$sender>; return(0) if ($reply !~ m/150/); ## Store file on receiver. print $receiver "STOR $_[1]\r\n"; $reply = <$receiver>; return(0) if ($reply !~ m/150/); $reply = <$sender>; return(0) if ($reply !~ m/226/); $reply = <$receiver>; return(0) if ($reply !~ m/226/); print "File $_[0] Transferred!\n"; return(1); }

Replies are listed 'Best First'.
Re: FTP Transfer
by McMahon (Chaplain) on Jun 01, 2004 at 18:54 UTC
    I'm not sure what you were thinking when you decided to roll your own with IO::Socket, but here's an easier way:

    use warnings; use strict; use Net::Ftp; use File::Find; my $ftp = Net::FTP->new("", Debug => 0) or die "Cannot connect to host: $@"; $ftp->login("user",'password') or die "Cannot login ", $ftp->message; my @files = $ftp->ls(); foreach my $file(@files) { $ftp->get($file) or die "get failed", $ftp->message; }
      Is File::Find really required for this? Or is there something I'm missing ?
        I included that part as an illustration of how to get around the lack of "mget" in Net::Ftp.
      Many thanks. I've never used Net::Ftp
Re: FTP Transfer
by mhi (Friar) on Jun 01, 2004 at 21:36 UTC
    Well, there are two general modes for FTP: active and passive.
    • In active mode the client opens a control connection to the server and for every data transfer from the server a separate data connection is opened (initiated by the server). This is probably what you were thinking of.
    • Passive mode FTP has only one joint control/data connection initiated by the client. If you are behind a firewall-setup that will not permit incoming connections, this is the way to go.

    Not having looked deeply into your code: If you're on a network with no special security and your original setup was geared towards a regular FTP-Server, your original script should be portable to a new box.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2023-05-27 23:39 GMT
Find Nodes?
    Voting Booth?

    No recent polls found