Hello,
I am trying to write a script that will lftp a file. The parameters are being passed in to the script from ETL tool. The put command is not working. put $file_name $tgt_file_name in the function ftpfile(). When I hardcode the file name with path its working. can some one help me with the error. Since I am only triggering the script from an ETL job. Log is not getting created. Also please let me know how to output a log when script runs.
#! /bin/ksh
# script to lftp a file. The parameters are being passed to the script from ETL tool.
# Return Values
# 0 - FTP Successful
# 1 - Insufficient Parameters
# 2 - Directory Not Found
# 3 - FTP Failed
# 4 - File Not Found
#####################################################################################################
host_name=''
user_name=''
passwd=''
remote_dir=''
default_dir=''
file_path=''
file_name=''
file_list=''
ftp_status=0
dir_found=0
temp_size=0
ftp_command=''
ftp_dir_cmd=''
tgt_file_name=''
usage()
{
cat << EOF
Usage: $0 options
options:
-s Host Server Name
-u User Name
-p Password
-l Local Directory
-f Source File Name
-t Target File Name
[-r Remote Directory]
[-d Default Directory]
[-c FTP Command]
EOF
}
ftpfile()
{
#echo "
#quote USER $user_name
#quote PASS $passwd
#$ftp_dir_cmd
#$ftp_command
#put \"$file_name\" \"$tgt_file_name\"
#quit
#"
lftp -c 'set ftp:ssl-allow true ; set ssl:verify-certificate yes; open -u $user_name,$passwd -e "put $file_name $tgt_file_name"; quit" $host_name'
# |
ftp -in $host_name > temp.log 2>&1
temp_size=`ls -l temp.log|tr -s " "|cut -d" " -f5`
if [ $temp_size -ne 0 ]
then
ftp_status=2
else
ftp_status=1
fi
}
Thanks for the reply. Wrong file is getting ftped. Log is not getting created. I want the script to create a log. can you please let me know how to get the log.
You have a usage message but no option-getting code. You don't redirect the output of lftp but redirect an empty ftp session to a log file. You include password in the script. Please use the .netrc file with the format
This may help; it has not been tested but is based on working code. I have used your usage function with options no longer relevant prefixed with #. Your exit codes have been used where possible.
#!/bin/ksh
usage()
{
cat << EOF
Usage: $0 options
options:
-s Host Server Name
-u User Name
[-c] Continue/retry
# -p Password
# -l Local Directory
-f Source File Name
[-t Target File Name]
[-r Remote Directory]
# [-d Default Directory]
# [-c FTP Command]
EOF
}
while getopts u:r:s:f:t:hc opt
do
case $opt in
u) user="-u $OPTARG" ;;
r) rdir="cd $OPTARG ;" ;;
h) usage; exit ;;
c) cont=-c ;;
s) host_name="$OPTARG" ;;
f) lfile="$OPTARG" ;;
t) tgt_file_name="$OPTARG" ;;
esac
done
shift $(( OPTIND - 1 ))
if [[ -n "${user:+u}${host_name:+s}${lfile:+f}" ]]
then
printf "Missing required options: %s\n" "${user:+u}${host_name:+s}${lfile:+f}" >&2
exit 1
fi
PREFIX_CMD="set cmd:fail-exit yes;set ftp:ssl-allow true;set ssl:verify-certificate yes;"
# (1) Causes lftp to exit on failure
# (2) Try to negotiate SSL connection with FTP server
# (3) Verify server's certificate to be signed by a known Certificate
# Authority and not be on Certificate Revocation List.
if [[ ! -f ${lfile} ]] # Check it exists
then
print -u2 "File ${lfile} does not exist"
exit 4 # exit with return val 4
fi
rfile=${tgt_file_name:-${lfile##*/}}
CMD="put ${cont} ${lfile} -o ${rfile};"
if [[ -n "${CMD}" ]]
then
lftp ${user} -e "${PREFIX_CMD} ${rdir} ${CMD} exit" ${host_name} > log_file 2>&1
ftp_status=$?
fi
if (( ftp_status == 0 ))
then exit 0
else exit 3
fi