http://qs321.pair.com?node_id=358380

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=>"ftp.xxx.com", + PeerPort=>"ftp(21)", Timeout=>10) || return(0); $receiver = IO::Socket::INET->new(Proto=>"tcp", PeerAddr=>"ftp.yyy.com +", 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); }