Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-25 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found