Reputation: 23
I am using the below code to connect with ftp node. I just want to know that how would I check if I am unable to connect with ftp server or ftp server is not responding. In any case it would alert me ftp server is ok or down. Actually I want to embed the normal bash code for checking of connectivity.
#!/bin/ksh
ftp -nv <<EOF
open xxx.xx.xx.xx
user xxx xxxxxxxxx
bye
EOF
Upvotes: 1
Views: 2055
Reputation: 364
Use this command to check whether an ftp-server is available:
sleep 1 | telnet ftp.example.com 21 2> /dev/null | grep -c 'FTP'
What it does:
You might have to adjust the grep pattern 'FTP' if the expected welcome response from the ftp server you want to check differs from the standard response, which usually reads something like this:
Trying 93.184.216.34...
Connected to ftp.example.com.
Escape character is '^]'.
220 FTP Service
Upvotes: 0
Reputation: 16
I've got the same problem before, solved it with checking the output from the ftp command. Still, found it quite weird, so i decided to use PERL for it.
#!/usr/bin/perl
use strict;
use warnings;
use Net::FTP;
# open connection
my $ftp = Net::FTP->new("127.0.0.1");
if (! $ftp) {
print "connection failed!";
exit 1;
}
# in case you would need to test login too
# if (! $ftp->login("username", "password")) {
# print "login failed!";
# exit 2;
#}
$ftp->close();
exit 0;
Upvotes: 0
Reputation: 32398
How about grepping the output from ftp? I'm not sure what your version of ftp returns when it's done a successful upload, but something like:
#!/bin/ksh
(
ftp -nv <<EOF
open xxx.xx.xx.xx
user xxx xxxxxxxxx
bye
EOF
) | grep -i "success"
if [[ "$?" -eq "0" ]]
then
echo "FTP SUCCESS"
else
echo "FTP FAIL"
fi
Should work..
Upvotes: 1