Linux uses Shell script to realize automatic upload and download of ftp

1. FTP automatic login to download files in batches.

#####From /home/data on ftp server to local /home/databackup####
#!/bin/bash
ftp -n<<!
open 192.168.1.171
user guest 123456
binary
cd /home/data
lcd /home/databackup
prompt
mget *
close
bye
!
2. ftp automatically logs in to upload files.

####Local /home/databackup to /home/data on ftp server####
#!/bin/bash
ftp -n<<!
open 192.168.1.171
user guest 123456
binary
hash
cd /home/data
lcd /home/databackup
prompt
mput *
close
bye
!

3. ftp automatically logs in to download a single file.
####Download /home/data/a.sh to local /home/databackup on the ftp server####
#!/bin/bash
ftp -n<<!
open 192.168.1.171
user guest 123456
binary
cd /home/ data
lcd /home/databackup
prompt
get a.sh a.sh 
close
bye
!

4. ftp automatically logs in to upload a single file.
####Put the local /home/databachup/a.sh up ftp /home/databackup ####
#!/bin/bash
ftp -n<<!
open 192.168.1.171
user guest 123456
binary
cd /home/data
lcd /home/databackup
prompt
put a.sh a.sh 
close
bye
!


Summary: Save the above script as a file and add it to crontab to automatically upload and download files by ftp.
annotation:
1. -n is not affected by .netrc files. (The default for ftp is to read the settings in the .netrc file)
2. << is to use the real-time file redirection input.
3. ! is the mark of the instant file and it must appear in pairs to identify the start and end of the instant file.

4. Make sure the script has execute permission chmod +x script name

 

Reprinted from http://liwenge.iteye.com/blog/566515

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325578233&siteId=291194637