2

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

1
  • The file "ftp-dummy" does have the Username and Password on two separate lines - this is what I wrote but it changed when I completed posting this question. May 3, 2010 at 19:43

3 Answers 3

4

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:[email protected]/file.txt
1
  • Thank you, this worked. I also got it working using a HERE document: ftp my.ftpsite.com <<TheEnd username password ls quit TheEnd And thanks to Dennis for editing my question to show things correctly. May 3, 2010 at 20:51
1

I've encountered issues redirecting STDIN with UNIX ftp clients before. While I'll often try to work through them, I've found expect to be better suited in many situations.

You might also take a look at nagios-plugins check_ftp, as you could potentially pull that out and use it for your purposes. Often, there's little need to reinvent the wheel.

To specifically answer your question, FTP's netrc file will provide the authentication details on your behalf.

0

There is a flag on ftp that you can use to stop the automatic password request when you send the user command.

man ftp

ftp -n

Then you can pipe your commands (including user and pass) to ftp.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .