#!/bin/ksh # # 24Apr2019 JPN Updated the ftp section of code to create and then look in an ftp session logfile for the message: ransfer complete -- more robust error checking # 30Apr2014 JPN Added code to make the destination directory (if it does not already exist) on local hosts (when using # the cp command to copy the file), or either local or remote hosts (when using scp to copy the file). # 31Jul2009 JPN Added $LINENO code # 18Apr2006 JPN Updated the code to run an ftp session to transfer the specified file # 06Jan2004 JPN For arg#5 = cp, don't attempt copy if source and destination files are identical! # 02Jan2004 JPN Initial version # # Local variables and input default values # command=$0 # name of this command basecmd=$(basename $command) currhome="$HOME" # current user's home directory currdir=$(pwd) # current directory currhost=$(echo $(hostname) | cut -d"." -f1) # get current host (Note that Linux returns,e.g., edward.ssec.wisc.edu- we want edward) curruser=$(whoami) pid=$$ # Current Process ID (PID) ftp_logfile="ftplog.${pid}.out" def_copy_cmd="cp" # default for input arg#5 # # Determine if help option is in effect. # allargs=$* echo "$allargs" | grep [Hh][Ee][Ll][Pp] > /dev/null helpstat=$? # truthfulness of prior command. if [ "$helpstat" -eq 0 -o "$1" = "?" -o "$1" = "H" -o "$1" = "h" ] then # # HELP menu # cat < /dev/null debugstat=$? # truthfulness of prior command... if [ $debugstat -eq 0 ] then # # grep found some combination of bug on the command line. # bugflag=1 # debug is in effect else bugflag=0 # debug is not in effect fi dirflag=0 # Assume the file to be copied is NOT a directory. ftp_cd=0 # Assume we DO NOT need to change directories before put/mput within the ftp or ncftp session. if [ -z "$1" -o "$1" = "x" -o "$1" = "X" ] then echo "($command) (See script line # $LINENO) ERROR: You must enter the file to be copied as arg#1 -- ABORT!" ; exit 100 else file_to_copy="$1" if [ ! -s $file_to_copy ] then echo "($command) (See script line # $LINENO) ERROR: The file named $file_to_copy either DOES NOT EXIST, or does exist but has a 0-byte length -- ABORT!" ; exit 100 elif [ -d $file_to_copy ] then dirflag=1 # The file to be copied IS a directory. fi fi if [ -z "$2" -o "$2" = "x" -o "$2" = "X" ] then echo "($command) (See script line # $LINENO) ERROR: You must enter the full pathname of the destination file as arg#2 -- ABORT!" ; exit 100 else destination_file="$2" echo $destination_file | grep "/" > /dev/null 2>&1 ; slashfound=$? # truthfulness of prior command... if [ $slashfound -eq 0 ] then # # grep found a "/" within $destination_file # ftp_cd=1 ftpdir_destination_file=$(dirname $destination_file) ; error=$? if [ $error -ne 0 ] then echo "($command) (See script line # $LINENO) ERROR stripping out directory name from $destination_file = $error -- ABORT!" ; exit 100 fi ftpname_destination_file=$(basename $destination_file) ; error=$? if [ $error -ne 0 ] then echo "($command) (See script line # $LINENO) ERROR stripping out base filename from $destination_file = $error -- ABORT!" ; exit 100 fi fi destination_dir=$(dirname ${destination_file}); error=$? if [ $error -ne 0 -o -z "${destination_dir}" ] then echo "($command) (See script line # $LINENO) ERROR determining destination_dir from destination_file (${destination_file}) = $error (destination_dir = ${destination_dir}) -- ABORT!" ; exit 100 fi fi if [ -z "$3" -o "$3" = "x" -o "$3" = "X" ] then destination_host="$currhost" else destination_host="$3" fi if [ -z "$4" -o "$4" = "x" -o "$4" = "X" ] then destination_user="${curruser}" else destination_user="$4" fi if [ -z "$5" -o "$5" = "x" -o "$5" = "X" ] then copy_cmd="${def_copy_cmd}" else copy_cmd="$5" fi # # Quality control # if [ "${copy_cmd}" != "cp" -a "${copy_cmd}" != "scp" -a "${copy_cmd}" != "ftp" -a "${copy_cmd}" != "ncftp" ] then echo "($command) (See script line # $LINENO) ERROR: You must enter one of cp, scp, ftp or ncftp for arg#4 -- ABORT!" ; exit 100 fi echo echo "($basecmd) (See script line # $LINENO):" echo echo "($basecmd) File to copy (arg#1) : ${file_to_copy}" echo "($basecmd) Destination file (arg#2) : ${destination_file}" echo "($basecmd) destination_dir = ${destination_dir}" echo "($basecmd) Destination host (arg#3) : ${destination_host}" echo "($basecmd) Destination user (arg#4) : ${destination_user}" echo "($basecmd) Copy command (arg#5) : ${copy_cmd}" echo if [ "${copy_cmd}" = "cp" ] then # ############################# # Transfer the file using cp. ############################# # # Copy the local file to another directory using cp. # # command format: cp option(s) file_to_copy remote_filename , where # # option = p = preserve modification times, access times, and modes # r = recursively copy entire directories, or just a single file # cmd="cmp $file_to_copy ${destination_file} > /dev/null 2>&1" if [ $bugflag -ne 0 ] then echo "($basecmd) (See script line # $LINENO) Execute: $cmd" fi eval $cmd ; error=$? # A non-zero error return probably means the two files are different. if [ $error -ne 0 ] then if [ ! -d ${destination_dir} ] then echo echo "($basecmd) (See script line # $LINENO) WARNING: The file destination directory (${destination_dir}) DOES NOT EXIST on ${currhost} -- Create it!" echo cmd="mkdir -pv ${destination_dir}" echo "($basecmd) (See script line # $LINENO) Execute: $cmd" eval $cmd ; error=$? if [ $error -ne 0 ] then echo "($command) (See script line # $LINENO) ERROR executing: $cmd = $error - ABORT!" ; exit 100 fi fi cmd="cp -pr $file_to_copy ${destination_file}" echo "($basecmd) (See script line # $LINENO) Execute: $cmd" eval $cmd ; error=$? if [ $error -ne 0 ] then echo "($command) (See script line # $LINENO) ERROR executing: $cmd = $error - ABORT!" ; exit 100 fi else echo "($basecmd) (See script line # $LINENO) WARNING: $file_to_copy is identical to $destination_file -- DO NOT copy!" fi elif [ "${copy_cmd}" = "scp" ] then # ############################## # Transfer the file using scp. ############################## # # Copy the local file to the local or remote computer using scp. # # command format: scp option(s) file_to_copy remote_user@remote_host:remote_filename , where # # option = p = preserve modification times, access times, and modes # r = recursively copy entire directories, or just a single file # v = verbose # # FIRST make sure that the destination directory is present! # if [ "${destination_host}" != "${currhost}" ] then # # We are using scp to copy a file to somewhere on a remote host. Make sure that the # destination directory exists on the remote host before copying the file. Note that "mkdir -pv ..." # will NOT cause a problem if the directory already exists. # echo echo "($basecmd) (See script line # $LINENO) NOTE: First make sure that the file destination directory (${destination_dir}) exists on ${destination_host}!" echo cmd="ssh -l ${destination_user} ${destination_host} 'mkdir -pv ${destination_dir}'" echo "($basecmd) (See script line # $LINENO) Execute: $cmd" eval $cmd ; error=$? if [ $error -ne 0 ] then echo "($command) (See script line # $LINENO) ERROR executing: $cmd = $error -- ABORT!" ; exit 100 fi else # # We are using scp to copy a file to somewhere on the current host. Make sure that the # destination directory exists before copying the file! # if [ ! -d ${destination_dir} ] then echo echo "($basecmd) (See script line # $LINENO) NOTE: The file destination directory (${destination_dir}) DOES NOT EXIST on ${currhost} -- Create it!" echo cmd="mkdir -pv ${destination_dir}" echo "($basecmd) Execute: $cmd" eval $cmd ; error=$? if [ $error -ne 0 ] then echo "($command) (See script line # $LINENO) ERROR executing: $cmd = $error - ABORT!" ; exit 100 fi fi fi if [ $bugflag -ne 0 ] then cmd="scp -prv $file_to_copy ${destination_user}@${destination_host}:${destination_file}" else cmd="scp -pr $file_to_copy ${destination_user}@${destination_host}:${destination_file}" fi echo "($basecmd) (See script line # $LINENO) Execute: $cmd" eval $cmd ; error=$? if [ $error -ne 0 ] then echo "($command) (See script line # $LINENO) ERROR executing: $cmd = $error - ABORT!" ; exit 100 fi elif [ "${copy_cmd}" = "ftp" ] then # ############################## # Transfer the file using ftp. ############################## # if [ $bugflag -ne 0 ] then ftpoptions="-vni" else ftpoptions="-vni" fi if [ $dirflag -ne 0 ] then ftpcmd="mput" else ftpcmd="put" fi cmd="ftp ${ftpoptions} ${destination_host}" # complete ftp transfer command password="${curruser}@ssec.wisc.edu" # password to use for ftp login to the remote machine echo "($basecmd) (See script line # $LINENO) Execute: $cmd" ; echo ###$cmd << EOF $cmd << EOF > ${ftp_logfile} user ${destination_user} $password binary pwd ${ftpcmd} ${file_to_copy} ${destination_file} bye EOF error=$? if [ $error -ne 0 ] then echo "($command) (See script line # $LINENO) ERROR executing: $cmd = $error - ABORT!" echo echo "($basecmd) (See script line # $LINENO) Contents of the file: ${ftp_logfile}:" echo cat ${ftp_logfile} echo rm -f ${ftp_logfile} exit 100 fi if [ ! -s ${ftp_logfile} ] then echo "($command) (See script line # $LINENO) ERROR: CANNOT FIND a nonzero length version of the file: ${ftp_logfile} in the directory: $(pwd) - ABORT!" rm -f ${ftp_logfile} exit 100 else echo echo "($basecmd) (See script line # $LINENO) Contents of the file: ${ftp_logfile}:" echo cat ${ftp_logfile} echo # # Look for the message: "ransfer complete" in the file: $ftp_logfile} # grep "ransfer complete" ${ftp_logfile} ; transfer_complete_found=$? if [ $transfer_complete_found -ne 0 ] then echo "($command) (See script line # $LINENO) ERROR: The message: ransfer complete WAS NOT FOUND within the ftp session logfile (${ftp_logfile}) in the directory: $(pwd) - Some type of ftp problem probably occurred - ABORT!" rm -f ${ftp_logfile} exit 100 else rm -f ${ftp_logfile} # The ftp session appears to have worked correctly. fi fi elif [ "${copy_cmd}" = "ncftp" ] then # ################################ # Transfer the file using ncftp. ################################ # if [ $bugflag -ne 0 ] then ncftpoptions= else ncftpoptions= fi if [ $dirflag -ne 0 ] then ncftpcmd="put -Rf" else ncftpcmd="put -f" fi cmd="ncftp ${ncftpoptions} ${destination_host}" password="${curruser}@ssec.wisc.edu" # password to use for ncftp login to the remote machine if [ $bugflag -ne 0 ] then echo echo "($basecmd) (See script line # $LINENO):" echo echo "($basecmd) file_to_copy : ${file_to_copy}" echo "($basecmd) ncftpoptions : ${ncftpoptions}" echo "($basecmd) ncftpcmd : ${ncftpcmd}" echo if [ $dirflag -ne 0 ] then echo "($basecmd) (See script line # $LINENO) ${file_to_copy} is a DIRECTORY." else echo "($basecmd) (See script line # $LINENO) ${file_to_copy} IS NOT a directory." fi echo if [ $ftp_cd -ne 0 ] then echo echo "($basecmd) (See script line # $LINENO)" echo echo "($basecmd) Before the put command, cd into: $ftpdir_destination_file" echo "($basecmd) (ncftp) put the following file : $ftpname_destination_file" ; echo $cmd << EOF1 binary cd ${ftpdir_destination_file} pwd ${ncftpcmd} ${file_to_copy} ${ftpname_destination_file} bye EOF1 else echo "($basecmd) (See script line # $LINENO) (ncftp) put the following file : $ftpname_destination_file" ; echo $cmd << EOF2 binary pwd ${ncftpcmd} ${file_to_copy} ${ftpname_destination_file} bye EOF2 fi else : fi error=$? if [ $error -ne 0 ] then echo "($command) (See script line # $LINENO) ERROR executing: $cmd = $error - ABORT!" ; exit 100 fi fi echo echo $startmsg echo "(${basecmd})"' End at: '"$(date -u)" echo echo "(${basecmd}) ***************************" echo "(${basecmd}) ***** END_OF_SCRIPT *****" echo "(${basecmd}) ***************************" echo exit 0