Getting error

Trying to check the SFTP status but if & else condition not working
Please help me

#!/bin/bash

DATE=`date +%Y-%m-%d:::%H:%M`
>/tmp/common.log
Log=`grep quit  /tmp/common.log | awk '{print $2}'`
echo "Starting to sftp...."          >> /tmp/common.log
{
 sftp SELTST@192.168.10.2  << EOF   >> /tmp/common.log
 quit                                                >> /tmp/common.log
EOF
}
if [ "$Log" == "quit" ]
  then
   echo "SFTP Succsfully"
else
   echo "SFTP unsucessfully :Please check"
fi

You are checking for 'quit' before you even run FTP, and right after you blank the log.

This effectively guarantees that you will never find 'quit' in the log. :slight_smile:

What does your log look like afterwards, anyhow? I'm not sure whether I'd expect 'quit' to be in there at all.

Also, you have some redundant statements in there.

Also, you had an extra space in your <<EOF which may have prevented it from ever ending.

#!/bin/bash

DATE=`date +%Y-%m-%d:::%H:%M`
: >/tmp/common.log

echo "Starting to sftp...."          >> /tmp/common.log
{
 sftp SELTST@192.168.10.2  <<EOF   >> /tmp/common.log
 quit
EOF
}
if grep quit  /tmp/common.log > /dev/null
then
   echo "SFTP Succsfully"
else
   echo "SFTP unsucessfully :Please check"
fi

But again, I'm not sure whether the log will ever contain 'quit'... What does your log look like after the program finishes?

We have SFTP server ,want to monitor the SFTP connection every day
in around 20 server so i want to automate it with script script should check the sftp connection if it works it should give a SFTP succesfull if not it should display SFTP failed

Well, what does your log file end up looking like then?

Hi Corona,

I have more than 20 systems each systems have SFTP connection from the each Servers , everyday I have to login into each server and test the SFTP connectivity.

If the SFTP connection works it should display SFTP successful if not it should send and SFTP failed.

Thanks,
Manjunath