It is some time necessary to automatically download a file from a ftp server daily.We can automate it by writing a shell script and putting it as a cron.A sample script is show below.When it got run it will login and download the file “1.txt” in to the current working folder from the directory “ftp” in the ftp server.
#!/bin/sh
rm 1.txt
HOST=’213.104.257.88′
PORT=’21′
USER=’bipin’
PASSWD=’bipin’
FILE=’1.txt’
ftp -n $HOST $PORT <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
cd ftp
bin
get $FILE
quit
END_SCRIPT
exit 0

February 20th, 2008 6:06 pm
Hello bibin,
In that script, i found a cd ftp command (Change Directory to FTP). is that correct.
From where to ftp. i did nt see any slash or root path there. If i am in home directory cd ftp will show the error , no such directory . Am i right ?
February 20th, 2008 8:31 pm
hello sajith,
you are right if u dont have any directory in your home the cd ftp is not needed.
The ftp here in my script is only a directory i created in my home directory.
It is not necessary.You can avoid it.
February 20th, 2008 8:35 pm
I mean try to make a script 100% independent by avoiding locally created directories and changes..
Anyway go on with technical updates. Good start.
February 20th, 2008 8:38 pm
You can use the script as follows, if you don’t have any directory like that..thanks for the comment…
#!/bin/sh
rm 1.txt
HOST=’213.104.257.88′
PORT=’21′
USER=’bipin’
PASSWD=’bipin’
FILE=’1.txt’
ftp -n $HOST $PORT <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
bin
get $FILE
quit
END_SCRIPT
exit 0
January 29th, 2009 2:09 am
I came across your posting on FTP automation as well as another titled “Shell script for checking a system alive or not”. I appreciate your sharing these. I am actually looking for a method to combine these. Here is my issue:
I have remote call center software that periodically sends recordings to a second remote ftp server. On occasion the FTP server would go down (although it would still ping) and we’d have weeks with no recordings until someone happened to notice and a problem occurred.
I am looking for a script, whether shell (sh or bash), perl, or php, that can be loaded into our webserver / email machine and check a remote FTP (don’t need to download anything) for connectivity and then email our administrators if it’s down.
Any advice or assistance would be greatly appreciated. Thank you.
January 29th, 2009 1:38 pm
hello jfrazier,
You can use nmap command for your requirement which show the status of the port running.
bipin@bipin-laptop:~/Desktop$ nmap -p 21 122.166.40.72
Starting Nmap 4.62 ( http://nmap.org ) at 2009-01-29 13:34 IST
Interesting ports on ABTS-KK-Static-072.40.166.122.airtelbroadband.in (122.166.40.72):
PORT STATE SERVICE
21/tcp open ftp
Nmap done: 1 IP address (1 host up) scanned in 0.572 seconds
will show like this.
you can cut the necessary things you want and can make as a shell script and put it as cron to check your ftp server in each 30 sec.
nmap -p 21 122.166.40.72 | grep ftp | cut -d ‘ ‘ -f 2
store the result in a variable and check the status whether its open or not.if not open trigger mail,sms what ever you like. Hope this may help you.
Thanks,
Bipin
January 30th, 2009 8:11 pm
Thank you very much. Works nicely.
#!/usr/local/bin/bash
HOST=’xxx.xxx.xxx.xxx’
status=`/usr/local/bin/nmap -p 21 $HOST | grep ftp | cut -d ‘ ‘ -f 2`
if [ $status != "open" ]
then
SUBJECT=”Automated Alert”
TO=”support@companydomain.com”
/usr/bin/mail -s “$SUBJECT” “$TO” <<EOF
FTP at $HOST is DOWN!
Time: `date`
EOF
fi
exit 0
January 30th, 2009 8:17 pm
Very happy to hear from you!!!!
March 31st, 2010 3:02 pm
It seems that you’ve put a solid amount of effort into your article and I want to see a lot more of these on the Internet these days. I sincerely got a kick out of your post. I do not have a bunch to to say in reply, I only wanted to register to say exceptional work.