Script for SFTP Status Checking

Greetings...

I have to construct shell script to check the SFTP status,

Define a global variable (say sftpStatus). Set it to default value "success" when you define it first time outside the script.
check the current SFTP status (say currentStatus - local variable within the script)

if (sftpStatus is success and currentStatus is failed)
   send an alert to support team
   set sftpStatus to failed
else 
if (sftpStatus is failed and currentStatus is success)
   send service restoration mail
   set sftpStatus to success
 

No alert is needed if both these variables are success or failed

Please Let me know script

Are you running SFTP using a batch file? Please provide us more information.

no it's not a batch file ,External SFTP services are running in Linux Box we have to monitor the services with shell script

Ok. Looks like you want to retain the value of these 2 variables each time you run your script. I suggest storing these variable values in files. Here is a template of how you can achieve this, you can modify as per your need:-

sftpSTATUS=`cat sftpstatus.dat 2> /dev/null`

if [ -z "$sftpSTATUS" ]
then
        echo "success" > sftpstatus.dat # Setting default status for sftpSTATUS
fi

echo "#Status of SFTP service (success/failed)" > currentstatus.dat

currentSTATUS=`cat currentstatus.dat 2> /dev/null`

if [ "$sftpSTATUS" = "success" ] && [ "$currentSTATUS" = "failed" ]
then
        # send an alert to support team
        echo "failed" > sftpstatus.dat
elif [ "$sftpSTATUS" = "failed" ] && [ "$currentSTATUS" = "success" ]
then
        # send service restoration mail
        echo "success" > sftpstatus.dat
fi

I hope it helps.

1 Like

Is it possible to do the same script without calling the file name outside the script with golbal variable if yes, can you please provide me script with golbal variable

When the script execution terminate, the value stored in the variable will be lost. This is the reason why we have to store it in a file or DB. AFAIU you want this variable value available during each execution which is why I suggested this approach.

bipinajith,

Do you have any alternate solution which we can use the global variable so that the values also should be stored in the script itself with export

Please suggest and shares the scipt if you have

---------- Post updated at 07:34 AM ---------- Previous update was at 07:28 AM ----------

I don't know about any other method to store the value.

Can you plese help me if any issue with existing script which i posted

not able to execute it correctly

What error are you getting when executing?
What do you have in the file /tmp/currentstatus.dat?

when executing the script getting the below message

Change initcap to lowercase in your sftp batch file:-
!echo "success"

I have changed it to lowercase for success and now I am seeing below

output is it correct.

Can you explain what result you are expecting or what exactly is the problem with the script? Please be more specific.

1). when condition success and failed i should get an alert

2). Each time when the script runs sftpstatus.dat will only success
string but no the failed string

That is because you are not setting currentSTATUS value to failed anywhere in your script.

can you please correct me where i am missing , I have to satisfy both condition if and elif condition