#updated 9/2002 by Patrick Higgins $[ = 1; #this sets the array base to 1 - see 'pg 68 of Programming Perl'. # Remote_stability.PL - generic shell to perform ftp's # can run multiple times simultaneously # requires module defined with 10 arguments # 1 destination machine # 2 fully pathed local filename # 3 path to remote file # 4 remote filename # 5 userid for remote machine # 6 fully pathed netrc file # 7 mode - ascii/binary # 8 direction - get/put # 9 incremental loop time in seconds # 10 max delay time in seconds # Set up module definition as follows: # Name: FTP # Description: File Transfer Program # Application: BATCH # Library: BATCH # Program Type: SOSHELLS # Description: SQL*Oper Shells # Program Name: FTP.SH # Program Location: $SQLOPER_HOME/exec/FTP.SH # # Get the arguments for this ftp procedure # $err=0; $parenv = $ENV{ "SO_PAR" }; $seqnoenv = $ENV{ "seq_no" }; $DEBUG = $ENV{ "DEBUG" }; $CLEAN = $ENV{ "CLEAN" }; $net_connect = $ENV{ "net_connect" }; $db_login = $ENV{ "db_login" }; $db_password = $ENV{ "db_password" }; $SQLOPER_HOME = $ENV{ "SQLOPER_HOME" }; # print "In Remote_stability.pl\n"; # $dest = shift ( @ARGV ); $local_file = shift ( @ARGV ); $remote_path = shift ( @ARGV ); $remote_name = shift ( @ARGV ); $userid = shift ( @ARGV ); $netrc = shift ( @ARGV ); $mode = shift ( @ARGV ); $direction = shift ( @ARGV ); $sleep_count = shift ( @ARGV ); $max_count = shift ( @ARGV ); ## $DEBUG = 'YES'; ## $CLEAN = 'no'; # print "input parms1-3-", $dest, "-", $local_file, "-", $remote_path, "\n"; # print "input parms4-", $count, "\n"; # print "input parms5-6-", $remote_name, "-",$userid, "\n"; # print "input parms7-9- ", $netrc, "-", $mode, "-", $direction, "\n"; $remote_file = $remote_path.$remote_name; # print "calculated-- $remote_file...\n"; # print " -- $seqnoenv......\n"; # print "environmental.1.",$err,"-",$parenv,"-",$seqnoenv,"\n"; # print "environmental.2.",$DEBUG,"-",$CLEAN,"-",$net_connect,"\n"; # print "environmental.3.",$db_login,"-",$db_password,"-",$SQLOPER_HOME,"\n"; if ( $DEBUG eq 'YES' ){ $^D=9; #Set debug level...requires Perl be called with the -D flag.; printf("%s-%s-\n%s-%s-\n%s-%s-\n%s-%s-\n%s-%s-\n%s-%s-\n%s-%s-\n%s-%s-\n%s-%s-\n" ,"dest",$dest,"local_file",$local_file,"remote_file",$remote_file ,"remote_path",$remote_path,"remote_name",$remote_name ,"userid",$userid,"netrc",$netrc,"mode",$mode,"direction",$direction); printf("In FTP.lis.pL p1\n\r"); } if ( $direction eq "" ){ print "Too few arguments\n"; print "Format is Remote_stability.PRL destination local_file remote_path remote_file userid netrc_file ascii/binary get/put"; $err=2; exit 2; } $dir_name="$SQLOPER_HOME\\run\\ftp.$seqnoenv"; mkdir($dir_name,700); chdir $dir_name; if ( $dest eq $net_connect ){ system('del netrc'); $db_login=lc($db_login); $db_password=lc($db_password); open(NETRC,">netrc"); print NETRC "machine $net_connect login $db_login password $db_password"; $record = "machine $net_connect login $db_login password $db_password"; } else { if ( ! -f $netrc ){ print "Could not find $netrc file\n"; $err=3; &exit_ftp; } if ( $netrc eq "netrc" ){ print "The netrc file cannot be netrc\n"; $err=4; &exit_ftp; } # print "netrc:$netrc dest:$dest\n"; #open file for read open(NETRC,$netrc); while () { chop; if (/$userid/){ if (/$dest/){ $record=$_; } } } if ( $record eq '' ){ $err=5; print "Could not find user\n"; &exit_ftp; } } close(NETRC); chmod 600, netrc; if ( "$direction" eq "PUT" || "$direction" eq "put" ){ if ( ! -f $local_file ){ print "Could not find $local_file file\n"; $err=6; &exit_ftp; } $command="put $local_file $remote_name"; } if ( "$direction" eq "SUBMIT" || "$direction" eq "submit" ){ $command="put $local_file"; } if ( "$direction" eq "GET" || "$direction" eq "get" ){ $command="get $remote_name $local_file"; } if ( "$command" eq "" && "$direction" ne "OTHER" ){ print "Invalid direction $direction\n"; $err=7; &exit_ftp; } if ( "$mode" ne "ascii" && "$mode" ne "binary" ){ $err=8; &exit_ftp; } $old_home=$HOME; # print "finished parm validation\n"; open(PAR,"$parenv"); open(FTPa,">>ftpa.$seqnoenv"); while () { $i++; if ( $i > 10 ){ print FTPa "$_\n"; } } close(PAR); close(FTPa); ($m,$mv,$u,$uv,$p,$upasswd) = split(/ +/,$record); print "ready to ftp $command\n"; #printf("mach %s %s\nuser %s %s\nparsed password %s %s\n" #,$m,,$mv,$u,$uv,$p,$upasswd); # print "start directory check\n"; &get_directory_list; if ( "$check_for_file" eq "" ) { printf( "listed file not found in directory.\n"); $err=201; &exit_ftp; } # print "after initial directory check",$check_for_file,"\n"; $first_list = $check_for_file; $check_for_file = "xxxx"; $counter = 0; while ($counter < $max_count) { sleep $sleep_count; $counter = $counter + $sleep_count; &get_directory_list; if ( $first_list eq $check_for_file ) { $counter = $max_count; } } if ( $check_for_file ne $first_list ) { printf ( "file not stable\n"); $err=202; &exit_ftp; } # print "after last directory check...",$check_for_file,"\n"; # Put the ftp command together and run open(FTP,"|ftp -n >ftp2.$seqnoenv \n"); print FTP "open $dest\n"; print FTP "user $userid $upasswd\n"; print FTP "verbose\n"; print FTP "cd $remote_path\n"; print FTP "type $mode\n"; open(FTPSUB,"ftpa.$seqnoenv"); while () { print FTP $_; #print STDERR $_; } close(FTPSUB); print FTP "$command\n"; print FTP "quit\n"; close(FTP); unlink("ftpa.$seqnoenv"); open(FTPTMP,"ftp2.$seqnoenv"); while () { chop; if (/ransfer finished successfully/){ $check=$_; } } close(FTPTMP); print "check.. $check\n"; #end if ( "$check" eq "" ) { printf( "error has been detected.\n"); $err=2; } print "done.\n"; &exit_ftp; sub get_directory_list { open(DIR,"|ftp -n >ftp3.$seqnoenv \n"); print DIR "open $dest\n"; print DIR "user $userid $upasswd\n"; print DIR "verbose\n"; print DIR "cd $remote_path\n"; print DIR "ls -l \n"; print DIR "quit\n"; close(DIR); open(DIRTMP,"ftp3.$seqnoenv"); while () { chop; if (/$remote_name/){ $check_for_file=$_; } } close(DIRTMP); } sub exit_ftp { $HOME=$old_home; open(FTPSUB,"ftp2.$seqnoenv"); while () { print $_; } close(FTPSUB); if ( $CLEAN eq 'YES' ){ unlink("ftp2.$seqnoenv"); unlink("ftp3.$seqnoenv"); chmod 700, NETRC; unlink 'NETRC'; chdir '..'; rmdir $dir_name; } else { printf("Clean is no - Leaving directory $dir_name\n"); chmod 777, $ftp2.$seqnoenv; chmod 777, $ftp3.$seqnoenv; chdir '..'; chmod 777, $dir_name; } print "Exiting FTP with $err\n"; $ENV{ "err" } = $err; system ('%SQLOPER_HOME%\exec\ernotify %err%'); exit $err; }