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); }

In reply to FTP Transfer by o0lit3

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.