Pages

Wednesday, June 17, 2009

Change Peoplesoft FTP to SFTP - Part 4

A sample script that ties up things using the information discussed.
1. minimize code changes to delivered script & call custom code in separate script
:::::
cd ${PS_HOME}/bin

# source my sftp script
. my_sftp_script

ftp_cmd_file=$1
::::::
::::::
echo "about to submit ftp -vin $1"
#$FTP_CMD -vin < $1
my_sftp_function

break
;;

esac

::::::::


2. my_sftp_script:
#####################################################
#
# My SFTP function
#
#####################################################
function my_sftp_function
{

#singal handler
trap "cleanup" 0
trap "echo Cancelled; cleanup;" 1 2 3 15

err=0
feedback=1

SFTP_CMD="/usr/bin/sftp"
sftp_cmd="h_sftp_cmd."$$
sftp_tmp="h_sftp_tmp."$$
sftp_log="h_sftp_log."$$
dev=me@some.place.com
user=/somehost/somedir/user_list

##### sftp messages --change text as needed
SFTP_LOGIN_ERR="Permission denied (publickey,password,keyboard-interactive)"
SFTP_NO_FILE="No such file or directory"

##### ftp messages & codes --change text as needed
FTP_NO_FILE="A file or directory in the path name does not exist."
FTP_LOGIN_ERR="Login incorrect."
FTP_MAX_SIZE="Exceeded storage allocation"

FTP_DEL_NOFILE_CD=532
FTP_LOGIN_ERR_CD=530
FTP_MAX_SIZE_CD=552
FTP_GET_NOFILE_CD=550

FTP_PUT_OK="226 Transfer complete."
FTP_GET_OK="226 Transfer complete."
FTP_DEL_OK="250 DELE command successful."

###############
# get ftp xfer command, remote dir, remote file name

cmd=''
r_file=`grep ^put $ftp_cmd_file | awk '{print $3}' | tr '\"' '\0'`

if [ ${r_file:-none} == 'none' ]; then

r_file=`grep ^get $ftp_cmd_file | awk '{print $2}' | tr '\"' '\0'`

if [ ${r_file:-none} == 'none' ]; then

r_file=`grep ^delete $ftp_cmd_file | awk '{print $2}' | tr '\"' '\0'`

if [ ${r_file:-none} != 'none' ]; then
cmd=delete
fi
else
cmd=get
fi
else
cmd=put
fi

r_dir=`grep ^cd $ftp_cmd_file | awk 'BEGIN{var=""} {if ($2!="/") var=var"/"$2; else var=var$2; } END{print var}' | sed 's/\/\//\//g'`

#echo "!sleep 500" > $sftp_cmd

# convert ftp to sftp commands
grep -e ^lcd -e ^cd -e ^put -e ^delete -e ^get -e ^quit $ftp_cmd_file | sed s/^delete/rm/ | awk '!/^rm/ {print $0} /^rm/ {print $0"\""}' > $sftp_cmd


echo 'sftp commands---------------' > $sftp_log
cat $sftp_cmd >> $sftp_log

echo '\nsftp log--------------------' >> $sftp_log

# run sftp command batch file
$SFTP_CMD -b $sftp_cmd `grep ^user $ftp_cmd_file|awk '{print $2}'`@`grep ^open $ftp_cmd_file|awk '{print $2}'` 1>>$sftp_log 2>&1

err=$?

if [ $err -ne 0 ]; then

grep -qi $SFTP_LOGIN_ERR $sftp_log

if [ $? -eq 0 ]; then
echo $FTP_LOGIN_ERR_CD" "$FTP_LOGIN_ERR
else
grep -qi $SFTP_NO_FILE $sftp_log

if [ $? -eq 0 ]; then

if [ $cmd = 'delete' ]; then
echo $FTP_DEL_NOFILE_CD" "$FTP_NO_FILE
else
echo $FTP_GET_NOFILE_CD" "$FTP_NO_FILE
fi
else

grep -qi $FTP_MAX_SIZE $sftp_log

if [ $? -eq 0 ]; then

echo $FTP_MAX_SIZE_CD" "$FTP_MAX_SIZE

fi
fi
fi

else
# check sftp results

echo '\nverifying results------------' >>$sftp_log

chk_results

err=$?

if [ $err -eq 0 ]; then

if [ $cmd = 'get' ]; then
echo $FTP_GET_OK
else
if [ $cmd = 'put' ]; then
echo $FTP_PUT_OK
else
echo $FTP_DEL_OK
fi
fi
fi
fi

# send feedback
if [ $err -ne 0 ] && [ $feedback -eq 1 ]; then

echo $cmd' failed.' >> $sftp_log

echo '\nftpunx log------------------' >> $sftp_log
echo ${ftp_cmd_file%.*}".log:" >> $sftp_log
cat `echo ${ftp_cmd_file%.*}".log"` >> $sftp_log

echo '\nEnd: '`date` >> $sftp_log

if [ -f $user ]; then
mailx -s"My SFTP failed" `cat $user` < $sftp_log else mailx -s"My SFTP failed" $dev < $sftp_log fi fi return $err } ################################################ # # check SFTP results # ################################################ function chk_results { case $cmd in put | get) # compare file sizes echo " `grep ^cd $ftp_cmd_file` ls -l "$r_file" quit " > $sftp_cmd

$SFTP_CMD -b $sftp_cmd `grep ^user $ftp_cmd_file|awk '{print $2}'`@`grep ^open $ftp_cmd_file|awk '{print $2}'` 1>$sftp_tmp 2>&1

if [ $? -ne 0 ]; then
return 1
fi

# get temp local file name

if [ $cmd = 'put' ]; then
l_file=`grep ^lcd $ftp_cmd_file | awk 'BEGIN{var=""} {if ($2!="/") var=var"/"$2; else var=var$2; } END{print var}' | sed 's/\/\//\//g'`/`grep ^put $ftp_cmd_file | awk '{print $2}' | tr '\"' '\0'`
else
l_file=`grep ^lcd $ftp_cmd_file | awk 'BEGIN{var=""} {if ($2!="/") var=var"/"$2; else var=var$2; } END{print var}' | sed 's/\/\//\//g'`/`grep ^get $ftp_cmd_file | awk '{print $3}' | tr '\"' '\0'`
fi

l_file_size=`ls -l $l_file | awk '{print $5}'`
r_file_size=`grep -v ^sftp $sftp_tmp | grep $r_file | awk '{print $5}'`

if [ $l_file_size -ne $r_file_size ]; then

echo 'file size different:' >> $sftp_log
echo 'local file size='$l_file_size' ('$l_file')' >> $sftp_log
echo 'remote file size='$r_file_size' ('$r_dir'/'$r_file')' >> $sftp_log

return 1
fi

break
;;

delete)

# DELETE - verify file does not exist; use subshell so sftp does not error off

(
echo "
`grep ^cd $ftp_cmd_file`
ls -l "$r_file"
quit
"
) | $SFTP_CMD `grep ^user $ftp_cmd_file|awk '{print $2}'`@`grep ^open $ftp_cmd_file|awk '{print $2}'` 1>$sftp_tmp 2>&1

if [ $? -ne 0 ]; then
return 1
fi


grep -q "$2 not found" $sftp_tmp

if [ $? -ne 0 ]; then
cat $sftp_tmp >> $sftp_log

return 1
fi

break
;;
esac

return 0
}


function cleanup
{
rm $sftp_tmp 2>/dev/null
rm $sftp_cmd 2>/dev/null
rm $sftp_log 2>/dev/null

}


Navigation to related pages:

Page Used to Define an FTP Server for File Attachments

Page Name

Object Name

Navigation

Usage

URL Maintenance

URL_TABLE

PeopleTools, Utilities, Administration, URLs

Specify an FTP server for storing file


Page used to Set Up File Attachment Servers

Page Name

Object Name

Navigation

Usage

Maintain File Att Servers

PV_ATT_SRV_ADMIN

Set Up Financials/Supply Chain, Utilities, Administer File Attachments, Administer Attachment Servers

Used to set up the FTP server location for PDF and XML file attachments which are included with notifications.


Pages Used to Set Up Attachments for Transactions

Page Name

Object Name

Navigation

Usage

Maintain File Att Servers

PV_ATT_SRV_ADMIN

Set Up Financials/Supply Chain, Utilities, Administer File Attachments, Administer Attachment Servers

Identify servers on which to store attachments.

Maintain Component Subdirs

PV_ATT_SRV_ADMIN

Set Up Financials/Supply Chain, Utilities, Administer File Attachments, Administer Attachment Servers, Maintain Component Subdirs

Define component names and paths where the attachments are stored.

Vendor Address

VNDR_ADDRESS

eProcurement, Procurement Application Admin, Maintain Vendors, Vendor Profile, Address

Define vendor email addresses for sending attachments.

URL Maintenance

URL_TABLE

eProcurement, Procurement Application Admin, Maintain Overall System Options, Maintain FTP Server Location

Requires end users to enter the file location manually for attachments (not the recommended method).

  1. Set up a location for storing attachments. This is usually an FTP server. Add a subfolder named PV to the root directory. Give read and write privileges to anonymous users or any users that are specified in the FTP string.

  2. Create a URL identifier that is named PV for the FTP location for PeopleSoft eProcurement attachments, and specify the FTP server address. The FTP root location must contain a subdirectory that is named PV.


Change Peoplesoft FTP to SFTP - Part3