Bash – Run FTP session from bash script

bashftpredirection

I'm trying to write a BASH script to test if an FTP site that I own is running. I therefore want the bash script to connect to the FTP site, log in with a dummy account and redirect the output to a file that I can then grep to confirm that the login succeeded.

(I know that putting user/pass in a file is not recommended, but this dummy account is chrooted to one empty directory and can't escape to the shell, and in any case I'm the only user who can login to a shell prompt.)

I'm using the BASH shell on Ubuntu.

I created a file called "ftp-dummy" which looks like this

username  
password

And I then did this from the prompt:

adam$  ftp my.ftpsite.com  < ftp-dummy

This does not work – I don't see the normal welcome message and the output is:

Password:Name (my.ftpsite.com:adam) :

I tried removing the space between the < and the filename – same result.

If I redirect the output to a testfile, the testfile shows:

Name (my.ftpsite.com:adam): ?Invalid command

And I still get a Password prompt on STDOUT

I also tried using echo and get the same result:

echo -e "username \npassword \n" | ftp my.ftpsite.com 

I don't see why I'm not seeing the normal welcome message or why the input is not being read from the file. Any help would be much appreciated.

Thanks,
Adam

Best Answer

use scriptable ftp client like lftp.

you use it like this:

lftp -u login,pass host.com -e "get file.txt;exit "

or even simpler - use wget:

wget -O file ftp://login:pass@host.com/file.txt