in reply to Re: using sftp to get file information for processing
in thread using sftp to get file information for processing

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