How to automate ftp in perl

My situations is I cannot use NET::ftp. So I need to have a way to automate ftp. I know how to do it in ksh:

#!/usr/bin/ksh
ftp -i -v -n $host_name <<_FTP >> $log_file 2>&1
user $user_name

lcd $local_dir
cd $remote_dir
put $file_name
bye
_FTP

But how can I do it in perl?

Note: No password required for ftp.

And if I want to run this ksh script in perl, using system() or backticks, I got prompt for passwd. Any ideas?

Thanks.

think this may help
Using Perl Expect To Automate SFTP Access | Parth Patil's Blog

Sadly that requires Expect, which he probably also cannot use. Egyfan, Are you sure you cannot install Net::FTP into your local account?

Anyway, what you have to do is to create a pair of pipes, spawn the FTP process using fork(), then pains-takingly read/write to the pipes. You have to handle anything unexpected, which is usually difficult at best. Check out the perlipc man page.

There is not way in ksh or perl that allow me to somehow treat the empty line as the password field?

Ohh, I think now I know what you are trying to do. There's a file you can use ".netrc" which will allow you to log into a site via FTP without specifying username and password. However, you still need to specify the commands after that. Here's an example of a .netrc file:

machine remote.host.edu login foobar password SuperSecret73 

You'll need to make sure the .netrc has permissions 600 (use chmod).

You could write the ftp code out to a file and then use the system command to execute that file. Here is a script that I use:

#!/usr/bin/perl  
# #########################################################################
# # This script was written to search directories read      ##
# # from the file pwdirs and search them for monthly reports to be sent  ##
# # to .  You must provide the beginning day, ending day and     ##
# # month on the command line.  The beginning day, ending day and month  ## 
# # must be two digits ex: 01,02....31.  This script will pull files for ## 
# # days and month specified. The command to run this is:                ##
# # perl pwcopy.pl BEGINNING_DAY ENDING_DAY MONTH .  ex:                 ##
# #        perl pwcopy.pl 01 03 09                                       ##
# # The above command would copy reports from the 1st through 3rd days   ##
# # for the month of September.                                          ##
# #########################################################################
# # These are passed from the command line
$begin=$ARGV[0];                      #Get the begin day of month
$end=$ARGV[1];                        #Get the end day of the month
$month=$ARGV[2];                       #Get the month of the reports to copy 
# Get the current date
($csec,$cmin,$chour,$cmday,$cmon,$cyear,$cwday,$cyday,$cisdst) = localtime(time);
$cyear=$cyear+1900;                   # get the correct year
$cmon=$cmon+1;                        # Get the correct month
if($cmon<10) {$cmon="0$cmon";}
if($cmday<10) {$cmday="0$cmday";}
if($chour<10) {$chour="0$chour";}
if($cmin<10) {$cmin="0$cmin";}
if($csec<10) {$csec="0$csec";}
# # Open file to write ftp script to
open(ftpsc, '>/usr/local/bin/pwpmftp') or die "Can't open ftp script file";
print ftpsc "# This script is created by the script pwcopy.pl\n";
print ftpsc "ftp -in 00.00.00.00 <<EOF\n";
print ftpsc "user userid password\n";
print ftpsc "type ascii\n";
# # Open the file containing the directories to be searched
open(DIRFIL, 'pwdirs') or die "Can't open directory file pwdirs\n";
while (<DIRFIL>) {
      $dir = $_ ;
      chomp($dir);
# # Open the directory to be read for files
      opendir(CURR, $dir) or die "Can't open directory: $dir $!\n";
      while (defined($file = readdir(CURR))) {
      if ($file =~ m/ARM|AMI|AMR[^AMRA|^AMRB|^AMRD]/) {
          $mtime = (stat("$dir/$file"))[9]; # Get the last accessed date
# # Create a readable date to compare to the current date from above
          ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($mtime);
          $mon=$mon+1;
          $year=$year+1900;
          if($mon<10) {$mon="0$mon";}
          if($mday<10) {$mday="0$mday";}
          if(($mday ge $begin && $mday le $end) && $mon eq $month && $year eq $cyear) {
             $count=$count+1;
             $fdate_time="$cmon$cmday$cyear$chour$cmin$csec$count";
             $new_file=substr($file,4,4);
             $odir=substr($dir,10,3);
             print ftpsc "put $dir/$file /$odir/$odir$new_file$fdate_time.txt\n";
            }
         }
      }
}
print ftpsc "quit\n";
print ftpsc "EOF\n";
close ftpsc;
system("./pwpmftp");

I am sure someone more experienced than I could write a more efficient perl script, but it works for me. Good Luck.

Thank you all. But the things is the "open" command and .netrc file still won't work for me. I tried both. In both cases, the program prompt me for password. I put "" (double quot) in open and .netrc for passwd.

So I might have to use NET::Ftp then.