Help needed regarding perl script that uses SFTP

Posted on 2007-10-03
Last Modified: 2012-06-27

   Help needed related to the below perl script. It is using SFTP to tranfer files, but i dont find any login info.
Is it trying to transfer from one server to another or on the same server. what the below code is trying to do.

   How can we convert the following code which uses normal ftp.

   while connect to ftp , should we compulsarily write use Net::FTP or is there any other way. If we want to connect
to the same server and transfer from one location to another using ftp, how can we achieve it.

help appreciate.


# Reteive File using SFTP
  open FT ,"|/usr/local/bin/sftp XFFILES";
  print REQ "|/usr/local/bin/sftp XFFILES\n";
  print FT "cd $copy_from_dir\n";
  print REQ "cd ".$copy_from_dir."\n";
  print FT "lcd $copy_to_dir\n";
  print REQ "lcd ".$copy_to_dir."\n";
  print FT "mget ".$price_file.".csv\n";
  print REQ "mget ".$price_file.".csv\n";
  print FT "bye\n";
  print REQ "bye\n";
  close FT;

# Create File for Report Output to be viewed in Concurrent Manager
$reqfile = $applcsf."/".$applout."/o".$request_id.".out";

# Open File for Writing Output
open (REQ,">>$reqfile") || die "Could not Create";

Question by:amankhan
    LVL 6

    Expert Comment


    Net::FTP is the best perl module available to support FTP. In perl if you would like to use any module then you have to write use <Module Name>.

    So in this cas it is necessary to write use Net::FTP;

    Please visit following link to understand more about the implementaion using Net::FTP.

    LVL 39

    Expert Comment

    There is also a module that supports SFTP.  If you want that, see here:

    Author Comment


      how to connect to a ftp using perl. also, if we want to run a command at the prompt, then we use system command i think. can u brief me with an example.

     also, to connect to ftp, and run some cd and get commands , how can they be run in perl script.

     help appreciated if briefed with an exampled.



    Author Comment


    Version of my perl is 5.005_03

    How can i aceive ftp on this version.

    LVL 39

    Expert Comment

    use Net::SFTP;    #include Net::SFTP module

    #Create sftp object
    my $sftp = Net::SFTP->new($host);

    #download remote file "foo", save as local file "bar"
    $sftp->get("foo", "bar");


    Author Comment


     what does the following do. i need to use this or use ftp without using net::ftp or net::sftp

    LVL 39

    Accepted Solution

    The above code will log into an SFTP server and get a file.
    Why don't you want to use a Net::SFTP or Net::FTP module?  That is the standard method?

    You could try this:
    open FTP ,"|/path/to/ftp $host";
    print FTP "$username\n";
    print FTP "$password\n";
    print FTP "get foo\n";
    print FTP "quit\n";


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
    I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now