1828418 Members
3662 Online
109977 Solutions
New Discussion

SFTP script

 
Prashant Zanwar_4
Respected Contributor

SFTP script

I want to write a customized sftp script.
1> Check for some particular files..
2> IF file exists send check for destination ping/ssh.
3> Put file across to destination host.
4> Mail across result of success fail to a address..

Anyone has it in place? Or idea's, please send across..

Thanks and regards
Prashant
"Intellect distinguishes between the possible and the impossible; reason distinguishes between the sensible and the senseless. Even the possible can be senseless."
2 REPLIES 2
Ranjith_5
Honored Contributor

Re: SFTP script

Hi Prashant,

Try the following script. Modify according to your servers / setup.




#! /usr/bin/ksh
cls
echo
echo
echo
echo " SCRIPT TO SCP THE ARCHIVE LOGS TO DR "
echo
echo
echo
echo "Enter the Starting Sequence Archive no to scp: \c"
read a
echo "Enter the Ending Sequence Archive no to scp: \c"
read b
if [ \( `ls -lrt | grep -i $a | wc -l` = 1 \) -a \( `ls -lrt | grep -i $b | wc -l` = 1 \) ]
then
echo
else
echo "Uniqueness failed, recheck your file name and enter again"
exit
fi
reala=`ls -lrt | grep -i $a | awk '{print $9}'`
if [ $? = 0 ]
then
echo "Confirm the file to scp is $reala type (y or n): \c"
read ans1
if [ "$ans1" = "y" ]
then
echo
echo "scp will be started from file $reala \n"
echo
else
echo
echo "scp is aborted becoz of wrong file name $reala \n"
echo
exit 1
fi
else
echo "File with sequence $a doesn't exist \n"
fi
realb=`ls -lrt |grep -i $b | awk '{print $9}'`
if [ $? = 0 ]
then
echo "Confirm the Ending file to scp is $realb type (y or n): \c"
read ans2
if [ "$ans2" = "y" ]
then
echo
echo "scp will be started up to the file $realb \n"
echo
else
echo
echo "scp is aborted becoz of wrong file name $realb \n"
echo
exit 2
fi
else
echo
echo "File with sequence $b doesn't exist \n"
fi
reala1=`echo $reala | awk -F _ '{print $3}'|awk -F . '{print $1}'`
realb1=`echo $realb | awk -F _ '{print $3}'|awk -F . '{print $1}'`
while [ $reala1 -le $realb1 ]
do
# echo "present value is *$reala1* "
scp -p *$reala1* USERID@IP_OF_SERVER:/DIRECTORY_PATH
if [ $? = 0 ]
then
echo "Completed the scp of file `ls *$reala1*` on `date`" >> /tmp/scplogs
else
echo "COULD NOT DO SCP TO DR on `date` EITHER LINK IS DOWN / FILE SYSTEM IS FULL / DESTINATION INVALID " >> /tmp/scperror
mailx -s SCP_FAILED_TO_DR_CHECK_LINK EMAIL_ID < /tmp/scperror
exit
fi
reala1=`expr $reala1 + 1 `
done
exit



Hope this helps.
Syam
Ranjith_5
Honored Contributor

Re: SFTP script

One more thing...

You will need to enable an scp without password to make this script enabled. You can do this by generating authorized_keys in /.ssh folder.

The above script will also store your error and log files in specific files.

Wish you good luck
Regards,
Syam