Help deciphering FTP get perl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help deciphering FTP get perl script
# 1  
Old 12-13-2007
Help deciphering FTP get perl script

I found this very useful perl script that will check a remote ftp server, search for files of a specific time and get them. When I run the script it works, but it gave me the following error:

Couldn't get filename_12-13-07.txt Bad file number

What in this script would cause this? I know nothing about perl.

#!/usr/bin/perl
use Net::FTP;
$date=shift @ARGV;
@months=qw(null Jan Feb Mar Apr My Jun Jul Aug Sep Oct Nov Dec);
@hosts=qw(ftp.server.com);
@dirs=qw(/);
@logins=qw(username);
@passwords=qw(password);



$x=0;
foreach(@months) {
$months{$_}=$x++;
}
# we need this hash later



if (not $date) {
$now=time();
$now -= (24 * 3600 );
# yesterday
($nowsec,$nowmin,$nowhr,$nowday,$nowmon,$nowyr,$nowdow,$nowdoy,$nowdst)=localtime($now);
$nowyr+=1900;$nowmon++;
$date=sprintf("%.2d/%.2d/%.4d",$nowmon,$nowday,$nowyr);
print "Using $date\n";
}



$now=time();
($nowsec,$nowmin,$nowhr,$nowday,$nowmon,$nowyr,$nowdow,$nowdoy,$nowdst)=localtime($now);
$nowyr+=1900;



# need $nowyr later



($month,$day,$year)=split /\//,$date;



#
# broken next century - blame me then
#
$year+=2000 if $year < 100;



$x=0;
foreach (@hosts) {
$newerr=0;
$ftp=Net::FTP->new($_,Timeout=>240) or $newerr=1;
push @ERRORS, "Can't ftp to $_: $!\n" if $newerr;
next if $newerr;
print "Connected $_\n";



$ftp->login($logins[$x],$passwords[$x]) or $newerr=1;
push @ERRORS, "Can't login to $_: $!\n" if $newerr;
$ftp->quit if $newerr;
next if $newerr;
print "Logged in $_\n";



$ftp->cwd($dirs[$x]) or $newerr=1;
push @ERRORS, "Can't cd $dirs[$x] on $_ $!\n" if $newerr;
$ftp->quit if $newerr;
next if $newerr;
print "Getting file list $_\n";



@files=$ftp->dir or $newerr=1;
push @ERRORS, "Can't get file list on $_ $!\n" if $newerr;
$ftp->quit if $newerr;
next if $newerr;
print "Got list $_\n";



print "Looking for $date $time\n";
foreach(@files) {
$_=substr($_,41);
s/ */ /g;
s/^ *//g;
chomp;
@stuff=split / /;
# if it's today, the year slot will have time instead
# so make it this year
$stuff[2]=$nowyr if /:/;



$ftp->quit if ($stuff[2] < $year);
next if ($stuff[2] < $year);
$ftp->quit if ($months{$stuff[0]} < $month and $stuff[2] == $year);
next if ($months{$stuff[0]} < $month and $stuff[2] == $year);
$ftp->quit if ($stuff[0] < $day and $stuff[2] == $year and $months{$stuff[0]} == $month);
next if ($stuff[1] < $day and $stuff[2] == $year and $months{$stuff[0]} == $month);



print "Getting $_\n";
$ftp->get($stuff[3],$stuff[3]) or $newerr=1;
push @ERRORS, "Couldn't get $stuff[3] $!\n" if $newerr;



}



$ftp->quit;
}



print @ERRORS;
exit 0;
# 2  
Old 12-13-2007
This looks like an error from the ftpd to me.
What happens if you manually try to ftp over and get that file?
# 3  
Old 01-23-2008
You're right I guess it was an error. I tried to run the script again and I'm getting just looking for 1/22/08. This is not what I need. I need a script that will:

1. Connect and Log into a remote FTP Server
2. Look for the latest file called "This_Sample_File_01-22-08.txt"
3. Change directories on my local box to /this/dir/for/file This_Sample_File_01-22-08.txt
4. Get the file "This_Sample_File_01-22-08.txt"
5. Close the connection.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

FTP download using perl script

Need assistance I have a script which i can download the files from ftp server using perl . But i want to download multiple files at a time #!/usr/bin/perl -w use Net::FTP; $ftp = Net::FTP->new("ftp.ncdc.noaa.gov"); $ftp->login('username', 'password'); $ftp->cwd("<dir>");... (9 Replies)
Discussion started by: ajayram_arya
9 Replies

2. Shell Programming and Scripting

Unable to get the size of remote file using Net::FTP Perl Script

Hi, I am using below piece of code to get the size of the remote file. $ftp->cwd($destination) or $error=$ftp->message; if(!$error) { $ftp->put($file) or $error=$ftp->message; print "FTP size = \n"; ... (3 Replies)
Discussion started by: FarooqOnline
3 Replies

3. 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

4. Programming

Some help with Perl please (deciphering)

I am trying to simplify the coding in a script I was given, but it was written 7-10 years ago and is pretty complicated. below is a tidbit, if someone can break it down for me I would appreciate it. sub ParseText { my ($line, $key, $value, $sub, $script); foreach $line (@_)... (0 Replies)
Discussion started by: callyvan
0 Replies

5. Shell Programming and Scripting

script FTP in perl

this script should search directories read,,search for file for daily reports. you must provide the beginning day, ending day and month. this script will pull files for days and month specified and Ftp them to another server. Thanks for any help you provide (2 Replies)
Discussion started by: lemseffert
2 Replies

6. UNIX for Dummies Questions & Answers

Need help deciphering this

I'm reading about command substitutions and came across this little function in my book: function lsd { date=$1 ls -l |grep -i "^.\{42\}$date"|cut -c55- } it's a little example which is supposed to select files by modification date, given as an argument to the function. I... (3 Replies)
Discussion started by: Straitsfan
3 Replies

7. Shell Programming and Scripting

FTP Perl Script File Size Mismatch.

Hello, I've written a Perl script that copies a set of files from one server to another. Prior to transferring a file the script gets the file size from the source system and compares this to the file size received in the target system. Except that the file sizes are slightly different. ... (1 Reply)
Discussion started by: mbb
1 Replies

8. UNIX for Dummies Questions & Answers

Deciphering the Code

Hi people I am trying to learn this code and see how it relates to the old DOS days. I have a line of code that I am not sure what the first part does. Any help will be greatly appreciated. It is from a Save command that is used to backup files to a directory. It goes like this if ;then... (10 Replies)
Discussion started by: coyote1967
10 Replies

9. Shell Programming and Scripting

Help deciphering script

There are files on a remote server with the file name ending in "mm-dd-yy.txt". The script I am running is: mls "Daily_Service_Text_File_*" /my/local/dir/Filelisting.txt nawk -F_ -f file.awk /my/local/dir/Filelisting.txt | sort -k1n | cut -f2- | tail -1 It worked up too "12-31-07.txt" but... (3 Replies)
Discussion started by: bbbngowc
3 Replies

10. Shell Programming and Scripting

Having a problem with a FTP Script using perl

Hello All, I have a problem that has me stumped. I am using perl to do some ftp'ing of files. I have the script in place on another environment that is functioning flawlessly. But after tweaking it to the new environment I can't get it to function. I have a search string that searches for... (6 Replies)
Discussion started by: scottzx7rr
6 Replies
Login or Register to Ask a Question