Use SFTP to batch upload files in shell scripts

Source: Internet
Author: User

How to Use SFTP to batch transfer files in shell scripts


The main steps are as follows:

1. generate key pairs for local users running shell scripts
2. Distribute the public key to the remote server that SFTP wants to log on.
3. Write and run shell scripts for the above local users

1. Generate a key pair

Key pairs (public keys and private keys) must be used when SFTP is used in shell scripts. You can use the following methods to generate a key pair (SSH 2.x ):
Local User name: local_user:

$ Ssh-keygen-d

Screen tip:

Generating public/private DSA key pair.

Enter file in which to save the key (/home/local_user/. Ssh/id_dsa ):
# Press enter to save as:/home/local_user/. Ssh/id_dsa, that is, the private key of the current user local_user

Enter passphrase (empty for no passphrase ):
# Press enter to read the key without the password.

Enter same passphrase again:
# Confirm the password of the key, which must be the same as the above input

Your identification has been saved in/home/local_user/. Ssh/id_dsa.
# Private key storage Information

Your public key has been saved in/home/local_user/. Ssh/id_dsa.pub.
# Public Key saving information

The key fingerprint is:
EC: 41: E8: 08: 38: 0b: F8: 1E: BC: 92: 98: 32: FC: D7: 69: 7d...
# Key fingerprint

Ii. Distribution Public Key

To use the key, you must distribute the public key to the remote server you want to log on to. The remote server is recorded as remote_hos.
T. The remote user to log on is marked as remote_user.

1. Copy the public key to the Home Directory of the remote user who wants to log on to the remote server. For example:

Copy id_dsa.pub to remote_host:/home/remote_user/. Ssh/

If the directory/home/remote_user/. Ssh/does not exist, create it first.

2. Rename the copy public key file to authorized_keys.

3. Modify the access permission of the public key file

Chmod 644 authorized_keys

Iii. Example

Objectives:

Remote Server remote_host:/home/remote_user/data/
Transfer the following files to the current directory of the Local Computer:/home/local_user/data /:

20050201
20050202
20050203
20050204
20050205

Method 1: batch mode

SFTP provides option-B, which is used to store SFTP commands in a centralized manner (this option is mainly used for SFTP in non-interactive mode). Therefore
For the above target, the following command file can be generated:

CD/home/remote_user/data/
LCD/home/local_user/data/
-"Get 20050201.
-"Get 20050202.
-"Get 20050203.
-"Get 20050204.
-"Get 20050205.
Quit

Saved here: sftp_cmds.txt

Note: Add "-" before the GET command to prevent termination of the SFTP execution process when an error occurs.

The following is a script example:

#! /Bin/sh
Sftp-B./sftp_cmds.txt remote_user @ remote_host

Method 2:

#! /Bin/sh
SFTP remote_user @ remote_host <EOF
CD/home/remote_user/data/
LCD/home/local_user/data/
-"Get 20050201.
-"Get 20050202.
-"Get 20050203.
-"Get 20050204.
-"Get 20050205.
Quit
EOF

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.