Thanks everyone for the input. I was able to get it working very well. The only outstanding issue is the ability to capture the result of the sftp->get command. It doesn't appear to give a return code and I don't know what all the possible textual responses are from this command. Does anyone have any ideas?

Just for your viewing, here is the code now. Ignore the junk around the get. I just haven't cleaned up stuff that doesn't work (but doesn't hurt).

#!/var/opt/pgpdba/perl/bin/perl #use strict; use Net::SFTP; use Net::SFTP::Attributes; use Net::SFTP::Util qw (fx2txt); use Logger; use Platform; use Getopt::Std; use File::Basename; $0 =~ s/^.*(\/|\\)//; # Strip a path if it is exists my $root = $0; $root =~ s/\..*$//; # Clean up script name my $LOG_FILE = "./${root}.log"; getopts('hur:l:f:'); print_usage_and_quit() if ($opt_h); my $CREDFILE = "./usertest.txt"; my $host=""; my $user=""; my $password=""; my $rdir=$opt_r; my $ldir=$opt_l; my $fdir=$opt_f; openlog("$LOG_FILE"); load_credentials(); my $sftp = Net::SFTP->new($host, user => $user, password => $password, debug => 1); #debug => 0); $sftp->ls($rdir,sub{my $attrs = $sftp->do_stat($rdir . "/" . $_[0]->{f +ilename}); #print $_[0]->{filename}, " ", $attrs->gid, " ", $attrs->s +ize, " ", $attrs->flags, " ", $attrs->perm & 49152, "\n"; if (($attrs->perm & 49152) != 16384) { sleep 20; my $get_rtn = $sftp->get("$rdir/$_[0]{filename}","$ldi +r/$_[0]{filename}"); print "get return code is $get_rtn\nret is $ret\n"; if ( !$_ ) { logtext("Retrieved file $rdir/$_[0]{filename} from + $host. Put the file in $ldir.\n"); my $rtrn = $sftp->do_rename("$rdir/$_[0]{filename} +","$rdir/processed/$_[0]{filename}"); $status = fx2txt($rtrn); print " status is $rtrn\n"; print " status is $status\n"; if ( $rtrn == 0 ) { logtext("Moved file $rdir/$_[0]{filename} to $ +rdir/processed on $host\n"); if ($opt_f) { my $mv_rtn = rename $ldir."/".$_[0]{filena +me},$fdir."/".$_[0]{filename} or errtext_opc "Can't move file $_[0]{f +ilename} from $ldir to $fdir\n", $AP_CNA, $SEV1, $GRP3, "1010", $STAT +1; print "mv_rtn is $mv_rtn\n"; if ( $mv_rtn == 1 ) { logtext("Moved file $ldir/$_[0]{filena +me} to $fdir/$_[0]{filename} locally\n"); } else { errtext_opc "Can't move file $_[0]{fil +ename} from $ldir to $fdir\n", $AP_CNA, $SEV1, $GRP3, "1010", $STAT1; } } } else { errtext_opc "Can't move file $_[0]{filename} +from $rdir to $rdir/processed on $host. The error was - $status\n", +$AP_CNA, $SEV1, $GRP3, "1010", $STAT1; } } else { errtext_opc "Can't transfer file $_[0]{filename} f +rom $rdir on $host to $ldir locally\n", $AP_CNA, $SEV1, $GRP3, "1010" +, $STAT1; } } }) ; closelog("$LOG_FILE"); sub callback { my($sftp, $data, $offset, $size) = @_; if ($offset == $size) { logtext("Retrieved file $rdir/$_[0]{filename} from $host. Put + the file in $ldir.\n"); } } sub load_credentials { open (CREDFILE1, "<$CREDFILE") or quittext_opc "Can't open credentials file $CREDFILE\n", $AP_CN +A, $SEV1, $GRP3, "1010", $STAT1; #die "Can't open credentials file $CREDFILE\n"; while (<CREDFILE1>) { # next if /^\s*(?:#|$)/; # ignore commen +ts/ blank lines chomp; my @fields = split /:/; if ($fields[0] eq 'user') { $user=$fields[1]; } if ($fields[0] eq 'pwd') { $password=$fields[1]; } if ($fields[0] eq 'host') { $host=$fields[1]; } } return 0; } sub print_usage_and_quit { print "\n"; print "Usage: ./" . basename($0) . " [-h] [-r remote_dir] [-l loca +l_dir] [-f final_dir]\n"; print "\n"; print "Where:\n"; print "-h is the option to print the help\n"; print "-r is the remote directory to scan for files\n"; print "-l is the local directory to initially transfer the files t +o\n"; print "-f is the final directory to move the files to\n"; print "\n"; print "Examples:\n"; print "./" . basename($0) . " -r ./oc/pg2003134 -l /var/opt/clin/p +rotocols/prod/inform/pg2003134 -f /var/opt/clin/protocols/prod/200313 +4/eloader/scan\n"; print "./" . basename($0) . " -r ./argus/pg2003134 -l /var/opt/cli +n/protocols/prod/inform/argus \n"; exit; }
Thanks, Jim

In reply to Re^2: using sftp to get file information for processing by jblapham
in thread using sftp to get file information for processing by jblapham

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.