Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Net::SFTP::Foreign getting files

by kofs79 (Initiate)
on Apr 11, 2013 at 13:18 UTC ( [id://1028156]=perlquestion: print w/replies, xml ) Need Help??

kofs79 has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that is supposed to pull a few files from a remote server daily. I am having a problem with the the file names since they have the timestamp at the end. I want to be able to use wildcards for the last part of each file and the first characters are known. This is what I have now. This only trys to get a file called 0 and 1. I can't find a way to match "IRP_*" to find a file called "IRP_0304041213"

use Net::SFTP::Foreign; use Switch; use Net::SFTP::Foreign::Constants qw(:error); my $leap_year = 0; my $inter_server = "0.0.0.0"; my $ai_user = "guest"; my $ai_pass = "password"; #Want to encrypt these in script my $ai_dir = ""; my $ai_dest_day = "/APP/FTP/Daily/Input"; my $ai_dest_mon = "/APP/FTP/Monthly/Input"; my @ai_files_day = qw{IRP_ trigextract_ VRP_}; my @ai_files_mon = qw{snapshot_ }; my ($sec, $min, $hour, $day, $month, $year) = localtime; my $sftp = Net::SFTP::Foreign->new($inter_server, user => "$ai_use +r", password => "$ai_pass"); # Add 'more => '-v'' debug $sftp->error and warn "SFTP connection failed: " . $sftp->error; print ("Connected to $inter_server($inter_server)\n"); print "Trying to login to $inter_server \n"; print ("Login was successful.\n"); foreach my $file (@ai_files_day){ #daily list our $newerr = "0"; $file .= "$yesterday"; #$mfile .= "mod_yester"; $file = $sftp->glob("${file}*"); print ("$file\n"); #This is for debug. $sftp->mget("$ai_dir/${file}", "$ai_dest_day/${file}") or $newerr= +1; print "Failed to retreive file $file : $!\n" if $newerr==1; next if $newerr; print ("Retreiving file $file ...... Success!\n"); if ($newerr != 1){ $sftp->remove("$ai_dir/${file}*")or $newerr=3; print "Deleting file $file ...... Failed!: $!\n" if $newer +r==3; print ("Deleting file $file ...... Success!\n"); } $newerr = "0"; }

Any suggestions for something to try to make this work?

Replies are listed 'Best First'.
Re: Net::SFTP::Foreign getting files
by salva (Canon) on Apr 11, 2013 at 13:35 UTC
    as $ai_dir is "", $ai_dir/${file} becomes /$file, so, it is trying to download the files from the file system root. Probably not what you want.

    As a workaround, use setcwd to let the module compose paths for you.

    Also, mget second argument must point to a directory.

    $sftp->setcwd($ai_dir); $sftp->mget($file, $ai_dest_day) or $newerr=1;

      Thanks for pointing out the setcwd as it was something I missed. The system that is being connected to does have a jailed ftp directory so it does show as root but it could've been a problem in the future.

      I still get a number being assigned as the file name in the output. I had added some print statements to see what file names I was getting and I don't get anything. In the glob I had also tried names_only=>1

      Connected to 0.0.0.0(0.0.0.0) Trying to login to 0.0.0.0 Login was successful. 0 Failed to retreive file 0 : 1 Failed to retreive file 1 : 1 Failed to retreive file 1 : Failed to retreive file snapshot_20130410 :
        Oh, I see, you are calling glob in scalar context where it returns the number of files matching. Also, you don't need mget when you have already used glob to resolve the name. get will do in that case:
        ($file) = $sftp->glob($file, names_only => 1); defined $file or die "file not found"; $sftp->get($file, "$ai_dest_day/$file");

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1028156]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-03-28 16:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found