How to connect to a ftp server via bash script? - bash

I wrote a bash script for uploading backup files from a server to a ftp server. But I always get an error.
Name (myftpserver:root): Permission denied.
Login failed.
Login with USER first.
Please login with USER and PASS.
Local directory now /backup01
Please login with USER and PASS.
Passive mode refused.
That's my script:
#!/bin/bash
DATE=`date +%Y-%m-%d_%H%M`
LOCAL_BACKUP_DIR="/backup01"
DB_NAME="databasename"
DB_USER="root"
FTP_SERVER="randomIP"
FTP_USERNAME="myname"
FTP_PASSWORT="supersecret"
FTP_UPLOAD_DIR="/home/mydirectory/ftp/upload"
LOG_FILE=/backup01/backup-$DATE.log
mysqldump -u $DB_USER $DB_NAME | gzip > $LOCAL_BACKUP_DIR/$DATE-$DB_NAME.sql.gz
ftp $FTP_SERVER << END_FTP
quote USER $FTP_USERNAME
quote PASS $FTP_PASSWORD
cd $FTP_UPLOAD_DIR
lcd $LOCAL_BACKUP_DIR
put "$DATE-$DB_NAME.sql.gz"
bye
END_FTP
if test $? = 0
then
echo "Database successfully uploaded to the FTP Server!"
echo "Database successfully created and uploaded to the FTP Server!" | mail -s "Backup from $DATE" my.email#whereever.com
else
echo "Error in database upload to Ftp Server" > $LOG_FILE
fi
Maybe someone can help me, because I've tried everything I've found on the internet.
I've made a .netrc file. I configured the vsftpd.conf, enabled passiv mode, enabled user list and I've made a lot of other stuff...
But now I'm having no idea what else I have to do to make this script working the way it should. And I have no idea why it's trying to connect via root...
Maybe there is someone out there who can help.
Thanks in advance.

I use:
ftp -v -n >> /tmp/ftpb.log <<EOF
open $URL
user $USER $PASS
binary
put $FILE
quit
EOF
and works

It's a common staple to use something like this:
$: ftp -vn <<!
> open localhost
> user foo
> put someFile
> quit
> !
> ftp: connect :Connection refused
Not connected.
Not connected.
Not connected.
$: echo $?
0
Unfortunately ftp considers that it successfully reported all problems, so it exits with a happy zero.
Use scp:
if scp "$lcldir/$filename" "$usr/$pw#$svr:$dir/"
then echo "file delivered"
else echo "delivery failed"
fi
If you can't use scp try something like expect, or write something in Perl - some way you can interactively test and confirm each step.
As a last resort, make the here-doc send the file and then pull it back to a tempfile that doesn't already exist locally. If you can successfully cmp file1 file2 afterwards, the send must have worked ok.

Related

change ftp to sftp in a shell script

I have this shell script which transfers CSV files to another server using the FTP service, and I need to change this service to SFTP. Can anyone help me?
ftp -inv >$FTP_LOG_FILE <<EOF
open $FTP_HOST
user $FTP_USERNAME $FTP_PASSWORD
lcd $REPORT_LOCAL_SOURCE
cd $DESTINATION_DIRECTORY
mput *$FILE_TYPE
exit
EOF
Can you use public key authentication? That makes it pretty easy - no password required.
Also, personal preference - ftp gives you no way to reasonably interact with the file transfers and react to misbehavior. Try scp.
Assuming automatic public-key authentication and the same vars you used above -
scp $REPORT_LOCAL_SOURCE/*$FILE_TYPE $FTP_USERNAME#$FTP_HOST:$DESTINATION_DIRECTORY/
or, with shorter names...
if scp ldir/*$ext $me#$host:$dir/
then echo "No errors"
else echo "There were errors"
fi
Generally, try to never use all cap vars.

FTPing inside of a KSH script from AIX to Windows to GET a file

#!/bin/ksh
. $HOME/bin/init.ksh
# log_start
exit_if_not_dgftp
# EXPIRE_DAYS=30
# create_arc_dir
# handle_error abort $? "Command create_arc_dir failed" $USER
# purge_archive $EXPIRE_DAYS
WORK_FILE=RetriesExceeded.csv
USER=LawsonIT-FinMM#test.org
HOST=lawsonfax.test.org
# Ftp the file
# Ftp username and password is in .netrc
$FTP -v $HOST
lcd $WORK_FILE
get RetriesExceeded.csv
quit
# archive_file $WORK_FILE
# /law/bin/mpack -s "Fax Retries Exceeded" $WORK_FILE_OUTPUT $USER
# log_stop
exit 0
[dgftp#lawapp2]/lawif/bin$ get_lawson_fax.ksh
Connected to lawsonfax
220 Microsoft FTP Service
331 Password required for dgftp.
230 User logged in.
ftp> quit
221 Goodbye.
/lawif/bin/get_lawson_fax.ksh[33]: lcd: not found
/lawif/bin/get_lawson_fax.ksh[34]: get: not found
/lawif/bin/get_lawson_fax.ksh[35]: quit: not found
[dgftp#lawapp2]/lawif/bin$
Explanation: The script connects fine to the lawson fax server, but stops at a FTP prompt. I can type in 'Get' interactively and it works, but in a KSH script it just stops at ftp prompt and then when I quit it gives three not found errors. If I am on the AIX server and manually FTP, open lawsonfax, get retriesexceed.csv there is no issue pulling the file.
Try using shell-redirection:
ftp -v "$HOST" <<DONE
lcd $WORK_FILE
get RetriesExceeded.csv
quit
DONE

Bash - uploading multiple files to FTP using real path?

I have this script to upload files to FTP (I know FTP is not secure, though client insists of using FTP..). It works fine, but the problem with it is does not recognize path provided when doing upload, even though message says it uploaded successfully, but nothing is uploaded.
So the script looks like this:
#!/bin/bash
HOST=host
USER=user
PASS=pass
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ftp -inv $HOST << EOF
user $USER $PASS
get
cd /path/in/ftp/
prompt
mput $DIR/*.csv
# End FTP Connection
bye
EOF
rm $DIR/*.csv
Here what is outputted:
Connected to host.
220 You have connected to Client's FTP server.
?Invalid command
331 User name okay, need password.
230-Welcome user from ip. You are now logged in to the server.
230 User logged in, proceed.
Remote system type is UNIX.
Using binary mode to transfer files.
?Invalid command
250 Directory changed to "/path/in/ftp/"
?Invalid command
Interactive mode on.
mput /path/inv_numbers_2016-11-21_12_09.csv? 200 PORT command successful.
150 File status okay; about to open data connection.
226 Closing data connection. Transferred 140 bytes in 1 seconds. 0KB/second.
140 bytes sent in 0.00 secs (1395.1 kB/s)
?Invalid command
221 Session Ended. Downloaded 0KB, Uploaded 1KB. Goodbye user from ip.
Now if I change mput $DIR/*.csv to mput *.csv, then it works (I get same log output like with previous one, except it shows path as being directly in directory where script is). But this only works if I would run script directly from directory it is placed in.
Any ideas?
Replace
ftp -inv $HOST << EOF
by
cd "$DIR" && ftp -inv $HOST << EOF
or by
cd "$DIR" || exit 1
ftp -inv $HOST << EOF

UNIX - FTP Using Parameterized Values

I have been stuck in this problem in a few days now and I really need help. My goal is to FTP a certain file into a bridge server. But before I can FTP, I need to enter some login credentials first. I want the login part to be automated that's why I created a separated parameter file. That parameter file has the login details.
So when I run the script, first it will create a txt file. Then the text file will be passed into the bridge server. Now, the script will also pass the login details from the parameter file to access the bridge server and finally a successful FTP. Any way to do this?
FTPFILE="File to be ftped"
Lets say the parameterised file has the details in the format
HostName username password.
Read the file contents using a loop statement like or however you like
I am using a while loop here
while read hostname username password
do
HOST=${hostname}
LOGIN=${username}
PWD=${password}
done
write the details - hostname,login, password to the $HOME/.netrc file
echo "machine ${HOST} login ${LOGIN} password ${PWD}" > /$HOME/.netrc
echo "macdef init" >> /$HOME/.netrc
echo "put ${FTPFILE} " >> /$HOME/.netrc
echo "bye" >> /$HOME/.netrc
echo >> /$HOME/.netrc
Ftp statement (Ftp first looks for .netrc file in $HOME directory to initiate the login process. If the file is not found then the username and password will be prompted)
ftp -i $HOST
This code will do the job:
#!/bin/sh
FTP_USERNAME=username
FTP_PASSWORD=password
FTP_SERVER=server_domaine
touch /directory/textfile.txt
run_access_server()
{
lftp <<STOP
#automatically access the server
open -u $FTP_USERNAME,$FTP_PASSWORD $FTP_SERVER
#changing directory
cd /directory/on/server
lcd /from/where/you/fetch/
#upload the file using get
mget textfile.txt
bye
STOP
}
run_access_server
Tell me how it works out with you.Regards

Download files from specific folder in AIX box

I have an AIX box. I wanted to connect to remote FTP server and download a specified folder "abc".
I have created a script; but it isn't working.
Here is my Code:
#!/bin/sh HOST='ftp.abc.xysz.net' USER='ftp' PASSWD='password' FILE='ababababababababababababab.abab';
ftp $HOST user $USER $PASSWD mget $FILE
quit END_SCRIPT
exit 0
Here is the error I receive when I execute Script..
Anyone has any idea to download the files from remote FTP server. Is there any single command available
Your script doesn't look properly formatted... it would be something like this:
#!/bin/sh
HOST='10.129.151.41'
USER='technicolor'
PASSWD='blueray'
FILE='ababababababababababababab.abab'
ftp -n <<END_SCRIPT
open $HOST
user $USER $PASSWD
get $FILE
quit
END_SCRIPT

Resources