#!/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]->{filename}); #print $_[0]->{filename}, " ", $attrs->gid, " ", $attrs->size, " ", $attrs->flags, " ", $attrs->perm & 49152, "\n"; if (($attrs->perm & 49152) != 16384) { sleep 20; my $get_rtn = $sftp->get("$rdir/$_[0]{filename}","$ldir/$_[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]{filename},$fdir."/".$_[0]{filename} or errtext_opc "Can't move file $_[0]{filename} from $ldir to $fdir\n", $AP_CNA, $SEV1, $GRP3, "1010", $STAT1; print "mv_rtn is $mv_rtn\n"; if ( $mv_rtn == 1 ) { logtext("Moved file $ldir/$_[0]{filename} to $fdir/$_[0]{filename} locally\n"); } else { errtext_opc "Can't move file $_[0]{filename} 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} from $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_CNA, $SEV1, $GRP3, "1010", $STAT1; #die "Can't open credentials file $CREDFILE\n"; while () { # next if /^\s*(?:#|$)/; # ignore comments/ 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 local_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 to\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/protocols/prod/inform/pg2003134 -f /var/opt/clin/protocols/prod/2003134/eloader/scan\n"; print "./" . basename($0) . " -r ./argus/pg2003134 -l /var/opt/clin/protocols/prod/inform/argus \n"; exit; }