Need Script to check the FTP connection

Hi,

I need a script which check the FTP connections, if its working fine then no issue. Incase not working fine then Trigger the mail.

Please help on this.

Thanks

Welcome Keshav Kalra,

I have a few to questions pose in response first:-

  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • What do you describe as a failure? Is is failure to open the connection, failure to get a file, failure to put a file or something else?
  • What OS and version are you using?
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
  • How would you test this manually?

Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.

We're all here to learn and getting the relevant information will help us all.

Kind regards,
Robin

Hi Robin,

Thanks for your reply..

#!/bin/bash 

root_ftp ( )
{
ftp -nvi server name <<EOF 
user ** **
pwd
!date
bye
EOF

RC=`echo "$?"`
}
echo -n "Checking FTP Service status  `date `"
root_ftp
echo $RC
if [[ $RC != 0 ]]
then 
mail -s "$(echo -e "FTP Alert\n content -Type:") "Mail ID"
else 
echo -n "No issues found `date `"
fi 

Above one I have tried...But mail is not getting triggered.

Connection are getting prepared..but In case of failure I am not getting mail triggered.
In both the cases successful or failure I am getting "No issues found "

Your problem here is that the ftp command will probably return code zero in most cases unless the connection fails to open at all.

You might do well to trap the output into a file and then read it afterwards. With the -v flag set as you already have, your should get status messages in the output. Those starting of value 400-999 followed by a space are errors. You have to exclude any messages that say bytes transferred though, as you may genuinely move a file of between 400 & 999 bytes and my simple check would generate an error.

I hope that this helps. Let me know if it's not clear,
Robin

If your goal is just to see if the FTP service is running on the target machine you could try this script (also useful for testing other socket listening services like sftp, httpd, mySQL, etc).

Note: this is a simple test but less comprehensive that your attempt, as it won't pickup issues with user or password changes which could also cause any FTP transfer to still fail even though a server is listening.

#!/bin/bash
if timeout 10 bash -c 'cat < /dev/null > /dev/tcp/server_name/21'  2>/dev/null
then
  printf "%s" "No issues found `date `"
else
  mail -s "$(echo -e "FTP Alert\n content -Type:") "Mail ID"
fi