Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Perl sftp behavior when there are no files in the share

by Bindo (Acolyte)
on Mar 21, 2013 at 10:14 UTC ( #1024703=perlquestion: print w/ replies, xml ) Need Help??
Bindo has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Good people..!

I'm a beginner who just joined this community with lots of hopes. With much attempt I've written a script whcih does the following.

1. Will create a directory under “/x01/xx/xx/xx” by the current date, in the format of “YYYYMMDD” if it has not already been created. i.e: if the script is run on “01st of jan 2013”, the directory “20130101” will be created under the said path. So whenever there is a need to inspect the logs always look for a directory by the current date. i.e: if the script is run on “01st of jan 2013”, the directory “20130101” will be created under the said path. So whenever there is a need to inspect the logs always look for a directory by the current date.

2. Check if the log file(s) have already been downloaded earlier within the same day, and if not log(s) will be downloaded to the TODAY’s directory.

As you see on the subroutine "get_LOGS" I'm terminating the script if there are no files can be found on the server. Can someone go through the script and let me know what I should do to return a message saying that there are no files in the server? For an instance if I specify 2 or more files to be downloaded that are not really on the remote share, it would just complain about the first file and exit.(I know this is because of the die function, its just that I have no idea how to address such a scenario

Usage of the script: abc_logs.pl <file1> <file2>..file(n)

)
use Net::SFTP::Foreign; use Cwd; my $LOGS_LOCAL_PATH = "/x02/ABC/abc/"; chomp $LOGS_LOCAL_PATH; my $LOGS_REM_PATH = "/x01/INT/ABC/vim/"; chomp $LOGS_REM_PATH; my $TODAY = `date +%Y%m%d`; chomp $TODAY; my @GETLOOP = @ARGV; unless ($#ARGV >= 0) { print "\nUsage: gtp_logs.pl <file1> <file2> <file3>.....<file(n)>\n\n +"; exit; } system("clear"); unless ( -d "$LOGS_LOCAL_PATH"."$TODAY") { print "Directory \"$TODAY\" doesn't exist. So creating the dir +ectory..!\n"; print "OK..Done.....!\n\n"; system("mkdir $LOGS_LOCAL_PATH/$TODAY"); } else { print "Directory already exists. Logs will be downloaded to == +> \"$LOGS_LOCAL_PATH$TODAY\".....!\n\n"; } # if_DOWNLOADED($LOGS_LOCAL_PATH,$TODAY,@GETLOOP); chdir("$LOGS_LOCAL_PATH"."$TODAY") || die "cannot cd to ($!)"; foreach my $GETL (@GETLOOP) { my $is_downloaded = if_DOWNLOADED($LOGS_LOCAL_PATH,$TODAY,$GETL); if(!$is_downloaded) { get_LOGS("172.25.70.221","abc","abc1234321","/x01/INT/abc/vim" +,$GETL); print "File \"$GETL\" downloaded to ==> \"$LOGS_LOCAL +_PATH$TODAY\"\n\n"; } else { print "File \"$GETL\" has already been Downloaded to ==> + \"$LOGS_LOCAL_PATH$TODAY\"\n\n"; } } sub get_LOGS { my $LOG_HOST = shift; my $REM_USER = shift; my $REM_PASSW = shift; my $REM_PATH = shift; my $REM_FILE = shift; print "Connecting to the sftp share! Please wait....!\n"; my $sftp = Net::SFTP::Foreign->new($LOG_HOST, user => $REM_USE +R, password => $REM_PASSW); $sftp->setcwd($REM_PATH) or die "unable to change cwd: " . $sf +tp->error; print "OK. On the share! Downloading the file \"$REM_FILE\"... +................!\n\n\n\n"; $sftp->error and die "Problem connecting to the share...!!!! " + . $sftp->error; $sftp->get($REM_FILE) or die "File does not seem to be present + on the remote share. Please re-request..!!!" . $sftp->error; return $REM_FILE; } sub if_DOWNLOADED { my $DWD_FILE_PATH = shift; my $DWD_DIR = shift; my $DWD_FILE = shift; if (-e "$DWD_FILE_PATH/$DWD_DIR/$DWD_FILE") { return 1; } else { return 0; }

PLease help out gentlemen. This will solve lots of other doubgts that I have when it comes to perl subroutines.

/Bindo

Comment on Perl sftp behavior when there are no files in the share
Download Code
Re: Perl sftp behavior when there are no files in the share
by salva (Monsignor) on Mar 21, 2013 at 10:18 UTC

      Yes I have cross posted it because I would like to solve this problem sooner. Is that a crime?

        Have you noticed that I am the one who approved your question?

        My comment was just informative and any negativity you see there comes from your head.

        Is that a crime?

        In fact, it is. Why would you ask people to waste their time answering your question if you've already been provided a solution elsewhere?

        Then to get uppity about it is the height of arrogance. If you are going to cross post, then you better state that fact up front and provide a link to your other post.

Re: Perl sftp behavior when there are no files in the share
by roboticus (Canon) on Mar 21, 2013 at 10:44 UTC

    Bindo:

    Rather than use die, just print the error and return from get_LOGS to let the program try to get the next logfile.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Thank you sir Robotics

      Even though it solves my problem mind you that I have a print statements just after when the sub "get_LOGS" is called.

      get_LOGS("172.25.70.221","abc","abc123","/x01/INT/abc/vim",$GETL); print "File \"$GETL\" downloaded to ==> \"$LOGS_LOCAL +_PATH$TODAY\"\n\n";

      so the script return the following output

      Connecting to the sftp share! Please wait....! OK. On the share! Downloading the file "mmm"...................!

      File does not seem to be present on the remote share. Please re-request..!!! Couldn't stat remote file: No such fileFile "mmm" downloaded to ==> "/x02/abc/abcd/20130321"

      Dont I need condition in my "get_LOGS" perhaps after the following line? Could you please try to write that for me?

      $sftp->get($REM_FILE) or print "File does not seem to be present on the remote share. Please re-request..!!!\n" . $sftp->error\n;
        You should print your confirmation message from within get_LOGS, like this:
        if $sftp->get($REM_FILE) print "file '$REM_FILE' downloaded\n" else print "File does not seem to be present on the remote share. Pleas +e re-request..!!!\n" . $sftp->error\n;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (13)
As of 2014-07-28 18:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (206 votes), past polls