2

I tried searching for a simple login data check for ftp connection in my bash script. I tried using wput and grep to get the output for a progress bar. Is there a way to check the login data first? I think wput doesn't support this...

Is there anybody who could help me with a good simple solution?

Thanks!

user3006625
  • 33
  • 1
  • 7

1 Answers1

3

You can use wget to test the connection:

wget --spider --tries=1 --user=login --password=pass ftp://your-ftp.com/
if [ $? -ne 0 ]; then
    echo "Failed to connect to ftp host"
fi

Or you can use ftp command:

echo 'exit' | ftp ftp://login:pass@your-ftp.com/
if [ $? -ne 0 ]; then
    echo "Failed to connect to ftp host"
fi

Note: sending/piping 'exit' command to FTP to force it exit out of interactive mode.

mhellmeier
  • 1,982
  • 1
  • 22
  • 35
Alex B.
  • 143
  • 8
  • I tried to run the command in background, `echo 'exit' | ftp ftp://login:pass@your-ftp.com/ &` How can I get the return value? – user3006625 Feb 07 '16 at 15:35
  • 1
    @user3006625 - return value (exit code) is stored in $? variable. You can do: `echo $?` – Alex B. Feb 26 '16 at 21:11
  • It is better to use **PIPESTATUS[]** shell variable while checking exit status of the commands run with pipes. for here, in second example, use `[ ${PIPESTATUS[1]} -ne 0 ]` to get exact exit status of the second command. – purushothaman poovai Dec 14 '19 at 04:58