How to use FTP in shell scripts

Source: Internet
Author: User
Tags ftp client how to use ftp

1. FTP automatically logs in to batch download files.

##### From/home/data on the FTP server to the 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 on to upload files.

#### Local/home/databackup to/home/data on the 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 on to upload a single file.
#### Put 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
!

"! "Must be in the first line
Summary: Save the preceding script as a file and add it to crontab to automatically upload and download files through FTP.
Note:
1.-N is not affected by the. netrc file. (FTP reads the settings in the. netrc file by default)
2. <uses the instant file redirection input.
3 .! It must appear in pairs to mark the beginning and end of an instant file.

Bytes -----------------------------------------------------------------------------------------------

In Windows, it is very simple. In Windows, the FTP client has a-S parameter that can contain a file. All the content to be input under the FTP command line is input to this file as is, run FTP-s. For example:
Open 10.60.56.90
WHB
123456
Get Test
Close
Bye
Save the file as autologin.txt.
E:/> FTP-S: autologin.txt
Ftp> open 10.60.56.90
Connected to 10.60.56.90.
220 (vsftpd 2.0.5)
User (10.60.56.90 :( none )):
331 please specify the password.

230 login successful.
Ftp> get test
200 PORT command successful. Consider using PASV.
150 opening binary mode data connection for test (5 bytes ).
226 file send OK.
FTP: received 5 bytes, with 0.00 seconds 5000.00 Kbytes/sec.
Ftp> close
221 goodbye.
Ftp> bye

E:/>
If you log on anonymously, leave the password blank.

In Linux, the above method is invalid. Bruce ediger. Summary:
Method 1:
#! /Bin/bash
Server = Server
Username = Username
Password = Password
FTP-N $ server <script
Quote user $ username
Quote pass $ Password
CD remote_path
LCD local_path
Binary
Verbose
Get something
Put something
Close
Quit
Script
Method 2:
#! /Bin/bash
Server = Server
Username = Username
Password = Password
FTP-N $ server <script
User $ username $ Password
CD remote_path
LCD local_path
Binary
Verbose
Get something
Put something
Close
Quit
Script
Method 3: Use. netrc
You can use man netrc to view the configuration parameters of this file. For example, write the following. netrc file:
Machine WHB
Login WHB
Password 123456
 
Machine node6
Login Globus
Password 123456
Note that if. if the FTP logon password is set in the netrc file using the password, other users except the file owner cannot read the file; otherwise, automatic logon will fail.
[[Email protected] ~] $ Chmod 600. netrc
[[Email protected] ~] $ FTP node6
Connected to node6.jlu.edu.cn.
220 (vsftpd 1.1.3)
530 please login with user and pass.
530 please login with user and pass.
Kerberos_v4 rejected as an authentication type
331 please specify the password.
230 login successful. Have fun.
Remote system type is UNIX.
Using binary mode to transfer files.
Ftp>

In addition, there are a lot of methods on the Internet to implement the use of secondary CT, You can Google.

How to use FTP in shell scripts

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.