The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Automate FTP borncrazy Shell Programming and Scripting 11 02-21-2008 12:10 AM
here document to automate perl script that call script hogger84 Shell Programming and Scripting 3 10-22-2007 11:15 AM
Automate config changes nhatch Shell Programming and Scripting 3 02-06-2007 03:31 PM
Automate FTP CamTu UNIX for Advanced & Expert Users 4 02-25-2005 10:08 AM
automate an ftp job flowrats UNIX for Dummies Questions & Answers 11 07-24-2002 09:47 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-09-2008
egyfan egyfan is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 4
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.
  #2 (permalink)  
Old 10-09-2008
ashishparimoo ashishparimoo is offline
Registered User
  
 

Join Date: Oct 2005
Location: UK
Posts: 8
think this may help
Using Perl Expect To Automate SFTP Access | Parth Patil's Blog
  #3 (permalink)  
Old 10-09-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,886
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.
  #4 (permalink)  
Old 10-09-2008
egyfan egyfan is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 4
There is not way in ksh or perl that allow me to somehow treat the empty line as the password field?
  #5 (permalink)  
Old 10-09-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,886
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:

Code:
machine remote.host.edu login foobar password SuperSecret73
You'll need to make sure the .netrc has permissions 600 (use chmod).
  #6 (permalink)  
Old 10-09-2008
jyoung jyoung is offline
Registered User
  
 

Join Date: Nov 2001
Location: Flint, MI
Posts: 228
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:
Code:
#!/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.
  #7 (permalink)  
Old 10-09-2008
egyfan egyfan is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 4
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0