Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 404
  • Last Modified:

strange Ftp script

Please read the script below:
0
gram77
Asked:
gram77
  • 2
2 Solutions
 
gram77Author Commented:
This program is designed to do ftp.
I can't understand where are the ftp commands: put/get/lcd/ascii/binary etc needed to do ftp.
This scripts manufactures the ftp script. It Puts ftp commands into temporary script file


I need to know:
1. The name of the file being ftp'ed
2. The file is being ftp'd FROM which location
3. The file is being ftp'd TO which location

I have provided the code for the calling program as well as the called program:FTP_UTL.KSH.

-------------------------
File calling FTP_UTL.KSH
-------------------------
FNAME="SCP_CUS_FCST_PARM"
LNAME="scp_cus_fcst_ftp"
SNAME="CUS_FCST*.csv"
DNAME="scp_load_customer_forecast.dat"
FTP_TEMP_PARM=$EBI_BIN/$FNAME.KSH
FTP_SOURCE="TROY-FILE1-CUSFCST"

$EBI_BIN/FTP_UTL.KSH $FTP_SOURCE $FTP_TEMP_PARM $SNAME $LNAME $DNAME


------------
FTP_UTL.KSH
------------


#        LOCALS Variables:
          V_SH_NAME=`basename $0`
          RC_SUCCESS=0
          RC_ERROR=1
      cur_dir=`pwd`
        DW_MAIL_LIST=`cat $EBI_DAT/email.list`
        DT_STAMP=`date +%Y%m%d%H%M`
        PROCESS="FTP"
        ENAME="$4"
        MNAME="$4"
        HNAME="$4"
        FTPERRLOG=$EBI_LOG/$ENAME.lst
        FTPMSGLOG=$EBI_LOG/$MNAME.msg
        FTPHSTLOG=$EBI_LOG/$HNAME.history


echo "Starting process $PROCESS at $DT_STAMP " >  $FTPMSGLOG
echo "     " >> $FTPMSGLOG
echo "Logfile is $FTPMSGLOG" >> $FTPMSGLOG
echo "     " >> $FTPMSGLOG


#---------------------------------------------------------------------
# Function Name:        ftp_error_check
# Description:          This function reads ftp log file and checks
#                       it for common errors defined in ERRORLIST.
#---------------------------------------------------------------------
function ftp_error_check {
   typeset ftp_log_file=$1
   typeset -i count=0
   typeset -i error_flag=0
   while (( $count < ${#ERRORLIST[*]} ))  
   do
      error_txt=`grep -v "bytes" ${ftp_log_file} | grep "${ERRORLIST[count]}"`
      error_found=$?
      if [[ ${error_found} -eq 0 ]]
      then
        error_flag=1
        echo "Error found: $error_txt" >>$FTPERRLOG
      fi
      (( count = count + 1))
   done
   return $error_flag
   }


#############################################################################
#----------------------------------------------------------------------------
# MAIN PROCESS STARTS HERE
#----------------------------------------------------------------------------
#############################################################################

#----------------------------------------------------------------------------
# Verify number of parameters passed
#----------------------------------------------------------------------------
 if [ $# -lt 2 ]
 then
        echo "Error: Invalid number of parameters passed to $0" >>$FTPMSGLOG
        echo "Example: BBY_STORE_SCAN_FTP.KSH TROY-FILE1 BBY_STORE_SCAN_FTP_PARM SFA2_PULL*.dat bby_ftp bby_store_scan_aut_load.dat" >>$FTPMSGLOG
      exit $RC_ERROR
 fi      

#----------------------------------------------------------------------------
# Find file FTP_PARM (parameter #2)
#   1.Check if correct path is provided with the file name.
#   2.If file not found:
#        Check if file can be found from $APPL_TOP
#   3.If file not found:
#        use the begining of the FTP_PARM file name to find environment
#        variable that has path name to the file.
#        Example: FTP_PARM file has name XXINV_I063_FTP.
#                 XXINV will be used as key word to find $XXINV_TOP/bin path
#                 to the file.
#----------------------------------------------------------------------------
 ftp_parm=$2

 if [[ -f $ftp_parm ]]
 then
    appname=$ftp_parm
 elif [[ -f $EBI_BIN/$ftp_parm ]]
 then
    appname=$EBI_BIN/$ftp_parm
 else    
    typeset -u FTP_BIN=${ftp_parm%%_*}'_BIN'
    if [[ ! -a $(eval print '$'$FTP_BIN) ]]
    then
       echo "\nError: Variable $FTP_BIN not found." >>$FTPMSGLOG
       echo "       Name $ftp_parm not following standard." >>$FTPMSGLOG
    fi
    appname=$(eval print '$'$FTP_BIN'/bin/'$ftp_parm)
 fi

 if [[ ! -f $appname ]]
 then
    echo "\nError: Ftp_parm $appname not found.  Aborting!" >>$FTPMSGLOG
    exit $RC_ERROR
 fi
 ftp_parm=$appname
      
#----------------------------------------------------------------------------
# Check if log file needs to be validated for number of good puts
#       from parameter #3.
#----------------------------------------------------------------------------
 if [ $# -gt 2 ]
 then
    typeset    check_log='Y'
 else
    typeset    check_log='N'
 fi

#----------------------------------------------------------------------------
# Find and verify password file using parameter #1.
#     1.Find $home/abc/xyz.dat maping file.
#     2.Check if Parameter #1 can be found in mapping file and
#       get password file name.
#     3.Check if password file exists.
#     4.Get IP Address, User name and Password from password file.
#----------------------------------------------------------------------------

 parm_1=$1
 parm_2=$2
 parm_3=$3
 parm_4=$4
 parm_5=$5
#-----------------------------
# The "set --" command resets standard variables ($1,$2,etc) to new values
#              from xyz.dat file: SERVER_name, Password_file_name
#-----------------------------
 set -- `grep ^$parm_1 $HOME/xyz.dat`

 if [[ $# -gt  0 ]] && [[ $1 = $parm_1 ]]
 then
    FTP_PW_FILE=$HOME/$2
    FTP_RC=$3
 else
    echo "Error: Server name $parm_1 is invalid!" >>$FTPMSGLOG
    echo "Valid names are:" >>$FTPMSGLOG
    echo "===================$HOME/xyz.dat" >>$FTPMSGLOG
    cat $HOME/xyz.dat|grep -v "^#"|cut -f 1 -d ' ' >>$FTPMSGLOG
    echo "===================$HOME/xyz.dat" >>$FTPMSGLOG
    exit $RC_ERROR
 fi

 if [ ! -s $FTP_PW_FILE ]
 then
    echo "Error: Password file $FTP_PW_FILE for $parm_1 is missing or empty.  Aborting!" >>$FTPMSGLOG
    exit $RC_ERROR
 fi

 FTP_SERVER=`head -1 $FTP_PW_FILE`
#-----------------------------
# The "set --" command resets standard variables ($1,$2,etc) to new values
#              from Password file: Ftp_User_name, Ftp_Password
#-----------------------------
 set -- `head -2 $FTP_PW_FILE | tail -1 `
 FTP_USER=$1
 FTP_PW=$2

#----------------------------------------------------------------------------
# Create temporary script and log files.
# Put ftp commands from FTP_PARM file into temporary sript file
# Run FTP.
#----------------------------------------------------------------------------
 temp_script_file=`mktemp XXXXXX`
 chmod 777 $temp_script_file

#-----------------------------
# Build FTP command file
#(This command seems to build username/password for ftp)
# (This seems to extract username/passwd info.)
#-----------------------------
# Parm file is executed instead of cat

 echo user $FTP_USER $FTP_PW >  $temp_script_file
 tail +3 $FTP_PW_FILE        >> $temp_script_file
 . $ftp_parm               >> $temp_script_file

## cat $temp_script_file

#-----------------------------
# Change home directory to $DL_DATA
#(ftp put and get commands use pathnames from $DL_DATA)
#-----------------------------
 cd $EBI_BIN

#------------------------------
# Run FTP
#(This line seems to connect to the remote server and log results. There is no hint of any file being ftp'ed:put/mput/get/mget)
#-----------------------------
 ftp -n -v $FTP_SERVER <$HOME/$temp_script_file 2>$FTPERRLOG>>$FTPMSGLOG

#-----------------------------
# Change directory back.
#-----------------------------
 cd $cur_dir
      
#----------------------------------------------------------------------------
# FTP post processing:
#     1. Count number of good puts in log file
#     2. Display parm and log files
#     3. Validate common Errors in the log file
#     4. Check if number of good puts equal to number of excpected puts.
#     5. Delete temporary files.
#----------------------------------------------------------------------------
 
#-----------------------------
# Count number of good puts in the log
#-----------------------------
 typeset -i good_puts=`cat $FTPMSGLOG | grep -c "^$FTP_RC "`

#-----------------------------
# Display ftp parm and log files
#-----------------------------


 integer err_rc=0
 if [[ -s $FTPERRLOG ]]
 then
    err_rc=1
    echo ===== ftp err file:
    cat $FTPERRLOG
 fi
 echo ===== ftp log end >>$FTPMSGLOG

 cat $FTPERRLOG >> $FTPMSGLOG
 rm $FTPERRLOG

      
#-----------------------------
# Validate common Errors in the log file.
#-----------------------------
 ftp_error_check $FTPMSGLOG
 integer rc=$?
 echo good_puts=$good_puts rc=$rc err_rc=$err_rc>>$FTPMSGLOG
#-----------------------------
# clean up temporary script file
#-----------------------------
 rm -f $temp_script_file

#-----------------------------
# check for success
#-----------------------------
 if [ $check_log != 'Y' ]
 then
    ftp_puts=$good_puts
 fi

 if [ $rc -ne 0 -o $err_rc -ne 0 ]
 then
    echo 'Error: Ftp process completed with errors' >> $FTPMSGLOG
    cat $FTPMSGLOG
    exit $RC_ERROR
 else
    echo "DL Normal Completion" >>$FTPMSGLOG
    echo "Ftp process completed successfully" >>$FTPMSGLOG
    if [ $good_puts -gt 1 ]
    then
       cd $EBI_DAT
       cat $parm_3>$parm_5
       rm  $parm_3
    else
       if [ $good_puts -eq 1 ]
       then
          cd $EBI_DAT
          cat $parm_3>$parm_5
          rm $parm_3
       else
          echo "Warning, The source file $parm_3 not present in the server" >>$FTPMSGLOG
          mailx -s "$PROCESS data file $parm_3  not found " $DW_MAIL_LIST <$FTPMSGLOG
       fi
    fi
  cat $FTPMSGLOG
  cat $FTPMSGLOG >>$FTPHSTLOG
  rm  $FTPMSGLOG
  exit $RC_SUCCESS
 fi

#----------------------END OF FTP--------------------------------------------
0
 
gram77Author Commented:
Here is the parameter file: CUS_FCST_PARAM.KSH used in the ftp process.
#!/bin/ksh
#  SCRIPT:
#     CUS_FCST_PARM.KSH
#
#   DESCRIPTION:
#     Parameter file for ftp script.
# ==============================================================================
FTP_SOURCE='TROY-FILE1'

                    echo "ascii "         
                    echo "prompt "                                     
                echo "cd xyz "
                echo "lcd $EBI_DAT " 
                    echo "mget CUS_FCST*.csv "
                    echo "mdelete CUS_FCST*.csv "   
                    echo "quit "                 
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi gram,

Despite the author's best efforts, this is a relatively ugly shell script.  :(



A big hint is in the echo statements after:

  # Verify number of parameters passed

The script requires at least two parameters.


Then look after the line:

  # Find and verify password file using parameter #1.

The first parameter is the name of the destination (host name or IP address).  The second parameter is the name of the password file that contains the FTP credentials.

Additional parameters are the file names to send.


Good Luck,
Kent
0
 
ozoCommented:
1. The name of the file being ftp'ed
2. The file is being ftp'd FROM which location
3. The file is being ftp'd TO which location
seem to come from
tail +3 $FTP_PW_FILE        >> $temp_script_file
which seems to come from
FTP_TEMP_PARM=$EBI_BIN/$FNAME.KSH
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now