How to automate ftp in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to automate ftp in perl
# 1  
Old 10-09-2008
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  
Old 10-09-2008
# 3  
Old 10-09-2008
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  
Old 10-09-2008
There is not way in ksh or perl that allow me to somehow treat the empty line as the password field?
# 5  
Old 10-09-2008
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  
Old 10-09-2008
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  
Old 10-09-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Automate [ls]ftp

Heyas Seen some of the ftp posts here and knowing i'll be writing a script using ftp soon, so i thought i'd write a script to automate, or at least simplify some basic tasks with it. I'm currently stuck at handling active/passiv modes. Neither lftp nor sftp seem to support them by arguments,... (3 Replies)
Discussion started by: sea
3 Replies

2. HP-UX

[Solved] Unable to rename file in ftp server .Net:FTP perl

Hello All, I am trying to connect to ftp server and get the files. Also i need to rename the file in other ftp dir. rename method is not allowing me to rename the file in other dir. When i tried copy command by using net::FTP:FILE then perl says it is not installed. Can some body help me to... (2 Replies)
Discussion started by: krsnadasa
2 Replies

3. UNIX for Advanced & Expert Users

TO Automate the FTP process

Plzz Some One Help in this matter I have scripts to load the data into tables and to copy files from ftp to our system. we use to run the scripts every day.... we hav the files in the FTP server and we hav to bring the files to our system and we hav to load the data into the tables. We... (0 Replies)
Discussion started by: nani1984
0 Replies

4. IP Networking

Automate FTP process and autorestart on link failure

Hi Guys, i have this lil challenge; i am to implement an automated script that searches/scans a directory for files then picks and sends this files to a very remote server via an ftp link. the challenge here is that the ftp link fails due to netwrk issues maybe; i therefore need to develop... (5 Replies)
Discussion started by: sdcoms
5 Replies

5. Shell Programming and Scripting

Automate daily FTP files

How to automate FTP files daily with the following constraints 1) Try (every 15 mins or 30 mins) FTP till it reconnects 2) Files that arrive in between 5:30 pm and 2:00 am 3) The sat and sun, mon files are to be FTP on monday. 4) Only the txt files are to be FTP'ed. The following are the... (2 Replies)
Discussion started by: bobbygsk
2 Replies

6. Shell Programming and Scripting

Automate FTP

Hi, Currently, i am using sftp manully to transfer files between two secure servers. Can anyone provide me a sample shell script which can automate the sftp process? (11 Replies)
Discussion started by: borncrazy
11 Replies

7. Shell Programming and Scripting

How to automate an FTP process?

Hello script experts, I am newbie to shell script. But I have to write a shell script (ASAP) where I need to ftp a file on daily basis to a remote server, and send an email with final status. I I should have a properties file with hostname, Userid, and pwd. And a shall script file should read... (1 Reply)
Discussion started by: ksak
1 Replies

8. UNIX for Advanced & Expert Users

Automate FTP

Hi all, I got this piece of code in this forum and I can't seem to get it work. The thread already closed so I just post a new thread. #!/usr/bin/ksh ftp -v -n "YOUR.IP.ADD.RESS" << cmd user "user" "passwd" cd /distant/directory lcd /local/directoryget ssh_install get ( or put) your... (4 Replies)
Discussion started by: CamTu
4 Replies

9. Answers to Frequently Asked Questions

Automate FTP / Scripting FTP Transfers

One of our most frequent questions is how to automate ftp transfers. There are several approaches. Since I'm writing this post, we will start with my favorite technique. :) In Automated FTP task I present a simple example of my ksh co-process technique. And note that later in this thread I... (0 Replies)
Discussion started by: Perderabo
0 Replies

10. UNIX for Dummies Questions & Answers

automate an ftp job

Hi, I am trying to write and automate a ftp job that connects to a IBM mainframe and pulls the same files everyday. To do this I assumen I create a .netrc file in my solaris home directory, then I write a shell script. How do I envoke ftp from a ksh script and pass it the info in .netrc? I... (11 Replies)
Discussion started by: flowrats
11 Replies
Login or Register to Ask a Question