How to check the status of sftp connection is successful or not in Linux?

Hi All,

We are working on linux with putty terminal for file transferring using SFTP server...
here we want to know /We have Urgent Requirement
If SFTP connection is successfull then we should get .txt log file in target locaton as "Success/Failure"

Please provide batch script for above requirement...
'it's very urgent..

Thanks,
Sravan

---------- Post updated at 01:46 AM ---------- Previous update was at 12:31 AM ----------

Please provide script for above requirement..

It's very urgent for us

Thanks,
Sravan

There are specific threads for urgent matters.
We're not a place to do your work, so you get the salary.

As it is urgent, and according to the forum rules, let me ask this first - as you had 11 hrs time:

  1. What have you tried so far?
  2. Where are you stuck, and why?
  3. Is it homework (class/course)?

Thank you

We have code as per my requirement of algorithm ....

Once SFTP server connection successful ...

then after few days i want to know the status of SFTP server connection success/failure...

In my google search
I found Below script...

STATUS=`ftp -nv $HOST <<EOF
<...ftp commands...>
EOF

if [ "$(echo $STATUS | grep -c 226)" -ge 1 ]; then echo "Success"; fi

My question regarding above script...

is it working or not..?
how can we know the status of SFTP connection ..

we are don't want try with unnecessary code...if the code working well we need to implement...

because we are in production side...in my project level...

we need accurate output script..

Thnaks,
Sravan

Please use code tags as required by the forums rules you have agreed to.
Again, what have YOU tried?

But ok, according to the information you share:
That should work, why shouldnt it?

For any further information,
please wait until my magic ball is back from repair,
or provide the required information, you know the data of your project required for the script, dont you?

Try:

echo $STATUS

Probably you will need to parse that output for better details.

hth

It is pretty safe to say that a shell script with mismatched backquotes will not work.

If you had matched backquotes, without knowing what ftp commands you're running on the server named by the expansion of $HOST , we have absolutely no way to guess at whether or not one or more occurrences of the string 226 will appear in the output of your unspecified ftp commands.

You talk about SFTP and you call ftp in your script. We assume that you know that there is no SFTP command, that sftp and ftp are similar but different, and that copying a script from a Google search without knowing that you are trying to do EXACTLY the same thing described in that search means that you should be prepared to adapt that script to match your environment and requirements.

Since you haven't told us what constitutes successful completion of your stfp commands, we have absolutely no way to guess whether or not the unspecified ftp commands in your script have succeeded or not. In many cases you could check the exit status of an ftp or sftp command to determine whether or not it succeeded, but again we do not know what you're trying to do.

A very good way to determine whether or not some code will work is to run that code and see if it works. Run it with tracing turned on and learn more about what each step is doing. Check the exit status of every command you're running and learn more about what each command is doing. You make it sound as though you haven't tried running any code. We can tell you that unless you try running code in your environment, we will never be able to reliably predict whether or not any code will work on a network of machines about which we know nothing.

Don Cragun,

Please let me, actually i want to know how to check the status of sftp connection is successful or not in Linux?...

i don't the how to check status...
please let me shell script....

Thanks,
Sravan

Try this:

<any valid command>
echo $?
[ condition ]
echo $?

There is your 'successfull' (0) or not (1), in case of bash that is.

hth

You haven't told us what shell you're using. So we can only tell you what works with the most common shells. If you're using one of the less common shells, the following advice will not work.

Your continued refusal to show us EXACTLY what you are trying to do and to define what you consider successfully connecting with sftp makes it impossible to give you a definitive answer. The basic answer is that the command sequence:

sftp [sftp_option...] host
if [ $? -eq 0 ]
then    echo 'sftp connected successfully and ran requested commands successfully.'
else    echo 'sftp did not complete successfully or one or more requested commands failed.'
fi

which assumes that you are running this script interactively and manually entering sftp commands if sftp successfully connects to the specified host.

1 Like