Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I upload (FTP) files to server in a Bash script?

Tags:

bash

ftp

I'm trying to write a Bash script that uploads a file to a server. How can I achieve this? Is a Bash script the right thing to use for this?

like image 859
Andrew Avatar asked Dec 12 '09 18:12

Andrew


People also ask

How do I upload to an FTP server using terminal?

Upload Single File to FTP Server To upload file on FTP server use put command from FTP prompt. First, navigate to the desired directory on the FTP server where to upload a file and use the following command. It will upload local system file c:\files\file1. txt to uploads directory on FTP server.


2 Answers

Below are two answers. First is a suggestion to use a more secure/flexible solution like ssh/scp/sftp. Second is an explanation of how to run ftp in batch mode.

A secure solution:

You really should use SSH/SCP/SFTP for this rather than FTP. SSH/SCP have the benefits of being more secure and working with public/private keys which allows it to run without a username or password.

You can send a single file:

scp <file to upload> <username>@<hostname>:<destination path> 

Or a whole directory:

scp -r <directory to upload> <username>@<hostname>:<destination path> 

For more details on setting up keys and moving files to the server with RSYNC, which is useful if you have a lot of files to move, or if you sometimes get just one new file among a set of random files, take a look at:

http://troy.jdmz.net/rsync/index.html

You can also execute a single command after sshing into a server:

From man ssh

ssh [...snipped...] hostname [command] If command is specified, it is executed on the remote host instead of a login shell.

So, an example command is:

ssh [email protected] bunzip file_just_sent.bz2 

If you can use SFTP with keys to gain the benefit of a secured connection, there are two tricks I've used to execute commands.

First, you can pass commands using echo and pipe

echo "put files*.xml" | sftp -p -i ~/.ssh/key_name [email protected] 

You can also use a batchfile with the -b parameter:

sftp -b batchfile.txt ~/.ssh/key_name [email protected] 

An FTP solution, if you really need it:

If you understand that FTP is insecure and more limited and you really really want to script it...

There's a great article on this at http://www.stratigery.com/scripting.ftp.html

#!/bin/sh HOST='ftp.example.com' USER='yourid' PASSWD='yourpw' FILE='file.txt'  ftp -n $HOST <<END_SCRIPT quote USER $USER quote PASS $PASSWD binary put $FILE quit END_SCRIPT exit 0 

The -n to ftp ensures that the command won't try to get the password from the current terminal. The other fancy part is the use of a heredoc: the <<END_SCRIPT starts the heredoc and then that exact same END_SCRIPT on the beginning of the line by itself ends the heredoc. The binary command will set it to binary mode which helps if you are transferring something other than a text file.

like image 141
greggles Avatar answered Sep 30 '22 07:09

greggles


You can use a heredoc to do this, e.g.

ftp -n $Server <<End-Of-Session # -n option disables auto-logon  user anonymous "$Password" binary cd $Directory put "$Filename.lsm" put "$Filename.tar.gz" bye End-Of-Session 

so the ftp process is fed on standard input with everything up to End-Of-Session. It is a useful tip for spawning any process, not just ftp! Note that this saves spawning a separate process (echo, cat, etc.). It is not a major resource saving, but it is worth bearing in mind.

like image 37
Brian Agnew Avatar answered Sep 30 '22 07:09

Brian Agnew