Operating System - HP-UX
1833871 Members
1792 Online
110063 Solutions
New Discussion

Re: sending a mail thru automatic ftp script

 
Vijayakumar  S
Advisor

sending a mail thru automatic ftp script

Hi,

I have script that is doing ftp automatically.
My concern is once the file has been ftped I need to send a confirmation mail.

if [ $? != 0 ]
then
echo "Sorry error"
else
echo "Working fine"
fi

But above checking is not working.

Anybody recommend good method of doing this.

Thanks
Vijay S
6 REPLIES 6
Geoff Wild
Honored Contributor

Re: sending a mail thru automatic ftp script

Why not send the entire result?

I do something like this for rdist, could do the same for ftp:

( su - prdadm -c "rdist -f $DRPDIR/distfile prdadm"; ) 2>&1 |\
tee $DRPDIR/drp.log 2>&1 |\
mailx -s "prdadm DRP rdist output" `cat /usr/local/ge/mailadmin.list`


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Muthukumar_5
Honored Contributor

Re: sending a mail thru automatic ftp script

We can check file transfer success as,

redirecting the ftp log to another file and check for the success and use the return there.

Example:

script /tmp/ftplog.log
ftp -i -n server < inputfile
exit

# final exit
exit

--inputfile --
user root test
get /tmp/testfile
bye
---------------

We have to grep the contents on /tmp/ftplog.log file.

If you want to simply then come to rcp / scp then..


rcp remotemachine:/tmp/testfile /tmp/.
if [ $? -ne 0 ]
then
echo "Sorry error" | mail <...>
else
echo "Working fine" | mail <...>
fi

You have to setup .rhosts file.

To do more secure then go for scp there.
Easy to suggest when don't know about the problem!
Vijayakumar  S
Advisor

Re: sending a mail thru automatic ftp script

Hi Geoff,

The Script is like this. What happens if the ftp ip is changed what happnes to the script. Mean the ftp IP has changed 10 days ago even though its not sending any mail that ftp has failed.


Thanks
Vijay S



#/usr/bin/sh

# This script is used to extract audit table info from AUD$ table and ftp to
# a central location.
# Server:
# Username:
# Password:
# Please format your logs like:
# For OS: servername_os_yyyymmdd.log
# For DB: servername_dbx_yyyymmdd.log
# Where for the db the x is the instance. 1 for the first, 2 for the second.

# Developed by:
# e-mail:
#

# Global env vars for this program

# User Defined Paramters
export ORACLE_SID=$1
export USER=$2
export PSWD=$3
export AUDITHOME=$4
export ORACLE_HOME=$5

NOTIFY=
NOTIFYONSUCCESS=0
AUDDATE=`date +%Y%m%d`
HOSTNM=`hostname`
FILENAME=${HOSTNM}_db_${ORACLE_SID}_${AUDDATE}.log

# Set all Oracle variables to run sqlplus
# . $HOME/.profile
export PATH=$PATH:$ORACLE_HOME:$ORACLE_HOME/bin:$ORACLE_HOME/lib
export PATH=$PATH:.
export SHLIB_PATH=$ORACLE_HOME/lib:$SHLIB_PATH
export LIBPATH=$ORACLE_HOME/lib:$LIBPATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH

cd $AUDITHOME

sqlplus $USER/$PSWD @${AUDITHOME}/scripts/getauditdata.sql ${AUDITHOME}/log/${FILENAME}

if [ $? -ne 0 ]
then
mailx -s "${ORACLE_SID} Audit Extract Failed" $NOTIFY < ${AUDITHOME}/log/${FILENAME}
exit 1
fi

if [ $NOTIFYONSUCCESS = 1 ]
then
mailx -s "${ORACLE_SID} Audit Extract Successful" $NOTIFY < /dev/null
fi

ftp -v -n<< EOF
open
user
ascii
put ${AUDITHOME}/log/${FILENAME} ${FILENAME}
bye
EOF


# The code below doesn't work as expected. The above ftp command always
# returns with a code 0. Commenting this code for now
#if [ $? -ne 0 ]
#then
#echo 'removing log'
#rm ${AUDITHOME}/log/${FILENAME} ${FILENAME}
#exit 0
#fi;
# For now, I am assuming that the above ftp is always successful and
# removing the audit log
rm ${AUDITHOME}/log/${FILENAME}
Procnus
Frequent Advisor

Re: sending a mail thru automatic ftp script

We have something similar running, I set it up as follows:
echo '

...
ls
quit' | ftp -i $srvr | grep '^-rw-' | grep $rem_nm >> $LOG

if [ $? -eq 1 ]; then
echo "Failed"
else
echo "Succeed"
fi

The check on $? in the if looks at the status of the last grep command and not the ftp command.

Cheers
Steven
Muthukumar_5
Honored Contributor

Re: sending a mail thru automatic ftp script

We can manage ftp with hostname or FQDN for the ftp ip which is modified on 10 days there. Using hostname / Fully qualified domain name is always safe there.

And use ftp script as

ftp -v -n << EOF | tee /tmp/ftplog.log
open
user
ascii
put ${AUDITHOME}/log/${FILENAME} ${FILENAME}
bye
EOF

Then audit the /tmp/ftplog.log file ${FILENAME} information's and success there. And ftp return return code will be always 0 because it is ending without any problem there. So return type check after ftp completion is not worth to this.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: sending a mail thru automatic ftp script

Ftp for this requirement seems to be not adoptable. And file transfer using ftp is not fast / efficient at this point. I suggest you to use rcp (remote copy) It is very fast compared to other's and easy to handle. We can manage return type to handle the SUCCESS / FAILURE of file transfer.


machine2 <--------> machine1
$HOME/.rhosts $HOME/.rhosts
machine1 user machine2 user

So that user can be accessible between machine1, machien2 without passwd. We can check success with return type 0 there.
Easy to suggest when don't know about the problem!