Ftp linux bash файл

How to upload one file by FTP from command line?

I need to upload a single file to FTP server from Ubuntu. This operation should be done in a script (in non-interactive mode). What is the right syntax for ftp ? I’m trying this, to no avail:

$ ftp -u ftp://user:secret@ftp.example.com my-local-file.txt ftp: Invalid URL `ftp://' 

I don’t know much about the ftp tool in Ubuntu, but it looks like it’s choking on the ftp:// . try taking that out maybe?

12 Answers 12

Alternatively, create (or edit) the ~/.netrc file in the home dir of the user that will run the ftp command, give it appropriate perms ( chmod 0600 ~/.netrc ), and add the following:

# ~/.netrc machine ftp.example.com login user password secret 

Then omit the login information, as in:

$ echo put my-local-file.txt | ftp ftp.example.com 

Also, here’s how you might do the same thing using curl:

$ curl -T my-local-file.txt ftp://ftp.example.com --user user:secret 

FYI: I’ve noticed that I can’t pass a full path of the file using the put raw command but I can with curl .

+1 for the curl. Neat, clean and straight to the point! In Debian/Ubuntu «apt-get install curl», if you don’t have it.

FWIW: I tried this with a large file (162MB). At first I thought curl looked slow (~25mins), but I found that the ftp command took just as long. curl looks good if available, and has nice feedback showing Time Left etc.

For uploads to a specific folder put a slash after the folder name: ftp://ftp.example.com/myFolder/ otherwise youl’ll get «Failed FTP upload: 553»

I can recommend ftp-upload . It’s a neat little tool that you can install under ubuntu through sudo apt-get install ftp-upload .

Hi DragonLord, if you are in Ubuntu and you have installed ftp-upload (using the command I gave before) you can just do man ftp-upload . Hope that helps.

@Xanlantos yes unfortunately it will leave the password in the bash history. I think there are ways around it I found through Google (but I don’t recall exactly).

You need to fix the URL given in your statement. You received the error because the URL was incomplete — it was missing the name of the object you are uploading. Once you add the filename after ‘example.com’ as I have done below, you will see the single command does indeed work as you intended.

perhaps it’s version dependant, like nc -e flag. my ftp command has the -u and following this format, it worked nicely!

Ah I just figured one thing out: this is because my ftp command is, actually, tnftp . You may want to check what FTP client you’re actually using, since they’ll have different capabilities!

lftp -e 'cd folder1/folder2; put /home/path/yourfile.tar; bye' -u user,password ftp.theserver.com 

Refer here for more details and also refer to LFTP Manual

To use it with non unix name I found this: NN=»‘non unix.file'» ; lftp -e «cd folder; put $NN; bye» -u anonymous, ftp.theserver.com

With the example above yes, it will be in the history. You can use the —env-password flag and have the password configured in LFTP_PASSWORD environment variable. Refer to the man pages of LFTP

Читайте также:  Bin and sbin in linux

Install ncftp and use the ncftpput tool that comes along with it, pretty much something like this syntax:

ncftpput -u ftpuser -p ftppass ftphostname /path/where/to/upload localfile.name if [ $? -ne 0 ]; then echo "Upload failed"; fi 

You can even check if the upload status is good or bad. The normal ftp client can also be used along with expect.

Upload a file to a remote location via command line

#!/bin/bash #$1 is the file name #usage:this_script HOST='yourhost' USER="youruser" PASSWD="pass" FILE="abc.php" REMOTEPATH='/html' ftp -n $HOST  

I use BusyBox's ftpput to do this:

# /bin/busybox ftpput BusyBox v1.20.2 (Debian 1:1.20.0-7) multi-call binary. Usage: ftpput [OPTIONS] HOST [REMOTE_FILE] LOCAL_FILE Upload a file to a FTP server -v,--verbose Verbose -u,--username USER Username -p,--password PASS Password -P,--port NUM Port 

Note: busybox ftpget work well too.

Use the put command after connecting to the FTP server with ftp (it is interactive, btw).

put local-file [remote-file] Store a local file on the remote machine. If remote-file is left unspecified, the local file name is used after processing according to any ntrans or nmap settings in naming the remote file. File transfer uses the current settings for type, format, mode, and structure. 
~ $ ftp 127.0.0.1 Connected to 127.0.0.1. 220---------- Welcome to Pure-FTPd [privsep] ---------- 220-You are user number 1 of 25 allowed. 220-Local time is now 10:47. 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. Name (127.0.0.1:user1): user2 331 User user2 OK. Password required Password: 230 OK. Current restricted directory is / Remote system type is UNIX. Using binary mode to transfer files. 

Example copy (login required before it):

ftp> put index.html local: index.html remote: index.html 200 PORT command successful 150 Connecting to port 43791 226-File successfully transferred 226 0.003 seconds (measured here), 3.96 Mbytes per second 10701 bytes sent in 0.00 secs (34.4773 MB/s) 

Example logout (login required before it)

ftp> quit 221-Goodbye. You uploaded 11 and downloaded 0 kbytes. 221 Logout. ~ $ 

Источник

Shell Script to Put File in a FTP Server

The File Transfer Protocol also called FTP is used to transfer files from client to server and vice-versa. It mainly uses port 21 for communication.

Here we can simplify the process of uploading files using FTP. But, before we write a script, let’s look at how to get/put files onto an ftp server directly using commands.

Basic FTP Command:

The commands to start an FTP communication and end it is shown below:

ftp server #Prompts for login details and connects to server #Example: ftp 192.168.0.104 bye #Terminates the ftp connection and exits ftp

Shell Script to Put File in a FTP Server

After typing “FTP hostname”, you will be asked for a username and password. If login is successful after entering the details, we start in the FTP user’s home directory on the server. Any file you upload now gets uploaded to this directory. If you have to upload a file in some other directory on the server, you first have to change to that directory using the “cd” command. Note that, using the cd command in an FTP prompt only changes directory on the server, i.e. we will still be in the same directory on our local computer.

Commands in FTP help us to navigate the server’s directories, fetch and upload files from and to the server. To get single or multiple files, we can use commands “get” and “mget” respectively. Similarly, to put single or multiple files, we can use commands “put” and “mput” respectively.

Some important ftp commands:

ls #Lists files in server cd dir #Change directory in server get file1.c #Downloads file1.c put file.txt #Uploads file.txt mput *.c file.txt #Uploads all c files and file.txt

Example of putting a file in server through FTP prompt:

Shell Script to Put File in a FTP Server

Shell Script to put the file in an FTP server:

#!/bin/bash # The 3 variables below store server and login details HOST="192.168.0.104" USER="user1" PASSWORD="1234" # $1 is the first argument to the script # We are using it as upload directory path # If it is '.', file is uploaded to current directory. DESTINATION=$1 # Rest of the arguments are a list of files to be uploaded. # $ is an array of arguments without first one. ALL_FILES="$" # FTP login and upload is explained in paragraph below ftp -inv $HOST 

Shell Script to Put File in a FTP Server

The above script requires the following data:

  1. Server’s hostname
  2. Server user’s login details
  3. The directory in which to upload files on the server (passed as an argument to the script)
  4. The list of files to be uploaded to the server (passed as an argument to script)

After logging in to the server, we need to enter FTP commands manually, but by using input redirection we can supply the commands directly in the script. “

The “mput” command has been used to upload files as mput can upload either a single file or multiple files.

If login is successful and the files given as input to the script are available, all the files should have been put in the server along with a success message displayed for each file.

The options -inv can also be written as -i -n -v and their functions are explained in the below table:

OptionMeaning
-iDisable interactive mode, so that FTP will not ask for confirmation of each file while using mput command etc. We are using this for convenience while uploading or downloading files
-nDisable auto-login. We have to do this, so we can manually log in using “user” command inside the script
-vEnables verbose mode. This helps us to see the server responses after executing each FTP command

To execute the script supply the upload directory and also a list of files:

./script_name.sh path_to_upload file1 file2 file3

File Uploading Example (Put all .c files and f1.txt in the current directory of server):

Источник

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

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?

11 Answers 11

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:

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:

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

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

ssh username@hostname.example 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 username@hostname.example 

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

sftp -b batchfile.txt ~/.ssh/key_name username@hostname.example 

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.

#!/bin/sh HOST='ftp.example.com' USER='yourid' PASSWD='yourpw' FILE='file.txt' ftp -n $HOST  

I would like to do this! Can you please expand on how I can do this? I need to do some things with ssh after uploading the file. Can this be done in one session?

While it was useful advice for the OP, it shouldn't be the accepted answer. It doesn't answer the original question (that is found via Google)

Some of the other posters have added binary mode (i.e. bin ) to the FTP commands. Since yours is the accepted answer, I recommend you adding this to your answer too.

Can also type hash to show progress of the ftp transmission in a way. Also, you will usually see

@SayanDasgupta as the answer says "The binary command will set it to binary mode which helps if you are transferring something other than a text file." If you are transferring images, pdfs, documents, zips, archives you want binary mode. If you know you are only transferring literal text files (e.g. source code, csvs) then skipping the binary might save you a tiny amount of time.

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

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.

This is totally confusing. How do I get out of this End-Of-Session thing? Why not input directly to the ftp prompt that comes without that?

@erikb85 - this for scripts, not (necessarily) for interactive use. The heredoc will automatically register an act upon your 'End-Of-Session' marker (you'd likely use EOF or similar)

The ftp command isn't designed for scripts, so controlling it is awkward, and getting its exit status is even more awkward.

Curl is made to be scriptable, and also has the merit that you can easily switch to other protocols later by just modifying the URL. If you put your FTP credentials in your .netrc, you can simply do:

# Download file curl --netrc --remote-name ftp://ftp.example.com/file.bin # Upload file curl --netrc --upload-file file.bin ftp://ftp.example.com/ 

If you must, you can specify username and password directly on the command line using --user username:password instead of --netrc .

Источник

Оцените статью
Adblock
detector