Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ftp command in if bash returns true even with wrong credentials

This is my bash file:

#!/bin/bash

# FTP Vars
FTP_IP=IPv4
FTP_USERNAME=USERNAME
FTP_PASSWORD=PASSWORD
FTP_PATH=/SOME_PATH

# FTP Connection, Upload and Delete
function upload() {
ftp -n -v $FTP_IP << EOF
ascii
user $FTP_USERNAME $FTP_PASSWORD
prompt
cd $FTP_PATH
ls
bye
EOF
}

if upload
then echo 'done'
else echo 'not done'
fi

The issue is, it doesn’t matter if I enter the password correct or wrong, I see done in the output which means upload function worked fine.

Let me show the outputs:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Correct credentials:

Connected to IPv4.
220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 1 of 50 allowed.
220-Local time is now 23:32. Server port: 21.
220-This is a private system - No anonymous login
220-IPv6 connections are also welcome on this server.
220 You will be disconnected after 15 minutes of inactivity.
200 TYPE is now ASCII
331 User group OK. Password required
230-This server supports FXP transfers
230 OK. Current restricted directory is /
Remote system type is UNIX.
Using binary mode to transfer files.
Interactive mode off.
250 OK. Current directory is /SOME_PATH
229 Extended Passive mode OK (|||35332|)
150 Accepted data connection
drwxr-xr-x    7 1047       group                92  May  1 12:49 .
drwx--x---    8 1047       1003                 181 May  1 21:29 ..
drwxr-xr-x    3 1047       group                24  May  1 12:49 dir1
drwxr-xr-x    3 1047       group                24  May  1 12:49 dir2
226-Options: -a -l
226 7 matches total
221-Goodbye. You uploaded 0 and downloaded 0 kbytes.
221 Logout.
done

Incorrect credentials:

Connected to IPv4.
220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 1 of 50 allowed.
220-Local time is now 23:36. Server port: 21.
220-This is a private system - No anonymous login
220-IPv6 connections are also welcome on this server.
220 You will be disconnected after 15 minutes of inactivity.
200 TYPE is now ASCII
331 User group OK. Password required
530 Login authentication failed
Login failed.
Interactive mode off.
530 You aren't logged in
530 You aren't logged in
530 You aren't logged in
530 You aren't logged in
221-Goodbye. You uploaded 0 and downloaded 0 kbytes.
221 Logout.
done

>Solution :

The ftp command doesn’t set an exit status based on the success of the commands.

In my script that automates FTP uploads, I use the lftp command, but you can also use curl or wget.

function upload() {
    lftp "ftp://$FTP_IP" -e "put $filename -o $acbl_directory/$destfile; quit" -u "$FTP_USERNAME,$FTP_PASSWORD" >/dev/null
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading