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
use scriptable ftp client like lftp.
you use it like this:
or even simpler - use wget:
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.
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.