How to realize one click upload with ftp

brief introduction

ftp is the user interface of Internet standard file transfer protocol, which allows users to transfer files with remote network sites

Using FTP client to upload files requires an FTP server. This paper mainly introduces the commands related to FTP client uploading. By default, the FTP server has been installed

The FTP server is built in How to deploy FTP server under Linux There is a detailed introduction in. Those who are interested can go and have a look

install

Install the FTP client using the following command

yum install ftp

Common options

After entering the ftp command line, you can execute upload and download related commands. Because there are many commands, only some common commands and some common options are listed below

commandexplain
putUpload local files to FTP server
mputBatch upload local files to FTP server
getDownload FTP server files locally
mgetFTP server batch download to local
deleteDelete files on FTP server
mdeleteBatch delete files on FTP server
lsFile details in the current directory of the FTP server
lcdReplace local working directory
cdChange directory on FTP server
cdupThe current directory of the FTP server returns to the previous directory
pwdWhich directory is currently on the FTP server
nlistLists the list of file names in the specified directory of the FTP server
binarySet the transport type to binary transport
openEstablish a connection with FTP on the specified IP and port
byeTerminate the session with the FTP server and exit the FTP client

optionexplain
-iTurn off the interactive prompt when transferring files in batches
-nAutomatic login is prohibited when the client connects to the FTP server
-vPrint all messages returned by the FTP server
!Execute command on local machine


Upload a single file

The put command is mainly used to upload a single file. Its format is put local file [remote file], where local file represents the local file, the default is the current local directory, and remote file is the file on the FTP server. If it is not specified, after uploading to the server, it will have the same name as the local file name by default

Here are the steps to upload the local / root/ftptest/haa.txt to the / files directory of the server

  • Step description
1,Enter the account password and log in FTP The server

2,'!pwd' The command indicates the current directory of the local machine

3,'!ls'  Command means to list files in the current local directory

4,'pwd'  The command is currently in FTP Which directory of the server is relative to the user locked directory. For locked directory, please refer to the previous article

   'cd files' The command is the current directory of the server( '/' ) Switch to '/files' catalogue

   'ls'  Yes list FTP Files in the current directory of the server. It can be seen from the results that there are no files in the current directory of the server

5,'binary' Upload in binary mode, which is generally set before uploading

6,'put haa.txt'  The current directory of the local 'haa.txt' Upload to server '/files' In the directory, after the upload is successful, there will be 'Transfer complete' Tips for

    If you upload files from other local directories to the server, you need to specify the file name to upload to the server,
    
    For example: local existence /root/ftptest/test/test.txt file
    
    have access to 'put ./test/test.txt  test.txt' Command to upload it to the server '/files' catalogue

7,After uploading, execute 'nlist *' Command to view the file list in the current directory of the server. This is used to confirm whether the upload is successful. From the results, it can be seen that there are files in the file list in the current directory of the server 'haa.txt' File, so this upload is successful

8,After the operation is completed, execute 'bye' Command disconnect and exit FTP client

Batch upload

Batch upload mainly uses the mput command, which can be followed by a file name list or a file name with wildcards. The command can correctly obtain the files referred to by wildcards, and the file names are separated by spaces

The following steps are to upload the haa.txt, hab.txt, ta.log and tb.log files in the / root/ftptest directory to the / files directory of the server

The above figure omits the steps of FTP login and entering the account password. The first command executed is ftp -i -v 192.168.70.20 48888

Option - i means to turn off the interactive prompt, which is very useful in batch uploading. When mput batch uploads, it will call the put upload command one by one to upload files. If the interactive prompt is not turned off, you need to manually enter the y character for confirmation every time you upload files. When the interactive prompt is turned off, you can upload continuously until all files are uploaded successfully

  • Step description
1,Local current directory

2,The current directory of the server, and then enter '/files' catalogue

3,Upload in binary mode

4,Batch upload files,'mput' It is a batch upload command, which can be followed by a file name or a file name composed of wildcards. They are separated by spaces, which means batch upload 'haa.txt,hab.txt,ta.log,tb.log '

   implement 'mput haa.txt hab.txt *.log' After the command, it will be uploaded to the server one by one
   
   Each file will be returned with upload result information. As can be seen from the results in the figure, a total of files have been uploaded 'haa.txt hab.txt ta.log  tb.log' All four files were uploaded successfully
   
 5,After the upload operation, click 'nlist *' The command confirms that the upload result is successful
 
 6,Exit client

One click upload

Each time you upload a file, you need to input some repeated FTP commands. In fact, it is a cumbersome operation. These repeated and cumbersome commands can be combined into one script to become a shell script for one click upload. The specific contents are as follows:

#!/bin/sh

#FTP user name
FTP_USER=mytest
#FTP password
FTP_PAWD=12345678
#FTP server IP
FTP_IP=192.168.70.20
#FTP server listening port (command port)
FTP_PORT=48888
#Which directory does the file upload to the FTP server
FTP_PATH=files
#Local to current directory
LOCAL_PATH=`pwd`

#Incoming parameter verification
if [ $# != 1 ]; then 
  echo "param count error..please input file"
  exit 1
fi

echo "begin ftp $1 ..."

#Turn off interactive prompt and prohibit automatic login
ftp -v -i -n<<!
#Establish a connection with the FTP server with the specified IP and port
open ${FTP_IP} ${FTP_PORT}
#Verify FTP user and password
user ${FTP_USER} ${FTP_PAWD}
#Transfer files in binary mode
binary
#Switch the server directory to the specified directory
cd ${FTP_PATH}
#Switch local directory
lcd ${LOCAL_PATH}
#The uploaded file $1 represents the file name passed in when executing the script
put $1
#Disconnect and exit the FTP client
bye
!

There are detailed comments in the script, so there is no additional description. This script only provides a basic one click upload template, and only one file can be uploaded at a time. You can expand or adjust it according to your own needs

Keywords: Linux Operation & Maintenance server

Added by mechamecha on Fri, 12 Nov 2021 01:55:31 +0200