Thursday, February 3, 2011

How to create a file transfer script via FTP in PERL

For you to be able to do a file transfer from your server to destination server, your server should be able to establish connection to destination server.

You only need the 4 details from the destination server:
  • Destination Server IP
  • Destination Port where you are connected into (by default is 22)
  • FTP Account Username
  • FTP Account Password.

Lets assume you have now the credentials and your server is allowed to connect to destination server via the IP and Port provided. We can now develop the script that will transfer files from your server to destination server.

Just follow the simple steps below with the complete code at the end for your reference.

1. We need the FTP library to be installed in your server.
  • Net::FTP 
2. Once installed, initialize Net::FTP by passing the [DESTINATION SERVER IP] and [DESTINATION SERVER PORT]. Save it to an object variable say $ftp, this is for you to be able to call the methods available for your script.

$ftp = Net::FTP->new([DESTINATION SERVER IP],Port=>[DESTINATION SERVER PORT]);

3. You can also get the return message or status of every object call by calling $object->message. The $object is the return object on step #2.

$ftp->message;

4. Submit the login credentials by calling $object->login(<username>, <password>).

$ftp->login([USERNAME], [PASSWORD]);

5. If successful, you can change or create your destination working directory by executing $object->cwd(<directory>) or $object->mkdir(<directory>).

$ftp->cwd($ftp_dir) or $ftp->mkdir($ftp_dir);

6. If your okay with your working directory, you can now transfer files from your server to destination server by calling $object->put(<source file>, <destination file>).

$ftp->put("[FILEPATH AND FILENAME OF SOURCE SERVER]", "[FILENAME OF DESTINATION SERVER]");

7. Then close the connection by calling $object->quit.

$ftp->quit;


That's it!! Hope you were able to follow the 7 simple steps above. You can also get the complete code with added functionality to support temporary files and renaming. Please see below.

#!/usr/bin/perl

use Net::FTP;

$ftp_ip = "<your destination ip>";
$ftp_port = "<your destination port>";
$ftp_user = "<your ftp account username>";
$ftp_pass = "<your ftp account password>";
$ftp_dir = "<your destination working directory>";

$ftp_src_file = "<your source file to be transfered include the full source path>";

$ftp_dest_file_temp = "<your temporary destination filename>";
$ftp_dest_file_good = "<your final destination filename>";

# create new instance
$ftp = Net::FTP->new($ftp_ip,Port=>$ftp_port) || die "ERROR: Unable to connect to host [$ftp_ip:$ftp_port]";
var_dump($ftp->message);

# login
$ftp->login($ftp_user, $ftp_pass) || die "ERROR: Unable to login [$ftp_user]";
var_dump($ftp->message);

# change working directory
$ftp->cwd($ftp_dir) or $ftp->mkdir($ftp_dir);
var_dump($ftp->message);

# transfer files
$ftp->put("$ftp_src_file", "$ftp_dest_file_temp") || die "ERROR: Transfer failed!",$ftp->message;
var_dump($ftp->message);

# rename files
$ftp->rename("$ftp_dest_file_temp", "$ftp_dest_file_good") || die "ERROR: Rename Failed!",$ftp->message;
var_dump($ftp->message);

# closing the ftp connection
$ftp->quit;

echo "done!";

1;

No comments:

Post a Comment

Leadership 101


  • Leadership demands sacrifices for the near-term to receive lasting benefits. the longer we wait to make sacrifices, the harder they become. Successful people make important decisions early in their life, then manage those decisions the rest of their lives.
  • Growth does not happen by chance. If you want to be sure to grow, you need a plan something strategic, specific, and scheduled. it's a discipline that would need incredible determination from us.
  • Success comes by going the extra mile, working the extra hours, and investing the extra time. The same is true for us. If we want to get to excel in any segment of life, a little extra effort can help. Our efforts can go a long way if we only work a little smarter, listen a little better, push a little harder, and persevere a little longer.
  • Making a difference in your work is not about productivity; it's about people. When you focus on others and connect with them, you can work together to accomplish great things.
  • Envision a goal you'd like to reach. Make it big enough to scare you a little. Now write down a plan for moving toward it. Create mini-goals within the big goal, to set yourself up for continual progress. And include some risks, too. Set yourself up for success.
  • Leaders build margins, not image. A leader may be forced to take unpopular stands for the good of the company. Popularity isn't bad, but decisions made solely on the basis of popular opinion can be devastating. So take courage and make the right though sometimes painful choices.