In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to use sftp to transfer files in batches in shell scripts". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
How to use sftp to transfer files in batch in shell script
The main steps are as follows:
1. Generate key pairs for local users running shell scripts
two。 Distribute the public key to the remote server where sftp wants to log in
3. Write and run the shell script as the local user above
one。 Generate key pair
Key pairs (public and private keys) must be used when using sftp in shell scripts. It can be generated in the following ways (SSH 2.x version), where the local user is marked as: 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's local_user
Enter passphrase (empty for no passphrase):
# Press enter to indicate that the password of the key is not required to read the key
Enter same passphrase again:
# confirm the password of the key, which must be the same as the input above
Your identification has been saved in / home/local_user/.ssh/id_dsa.
# storing information with private key
Your public key has been saved in / home/local_user/.ssh/id_dsa.pub.
# Public key to save information
The key fingerprint is:
Ec:41:e8:08:38:0b:f8:1e:bc:92:98:32:fc:d7:69:7d...
# key fingerprint
two。 Distribute the public key
In order to use the key, the public key must be distributed to the remote server to which you want to log in, where the remote server is recorded as remote_host and the remote user who wants to log in is recorded as remote_user
The 1.copy public key is placed in the home directory of the remote user of the remote server you want to log in to, for example:
Copy id_dsa.pub to remote_host:/home/remote_user/.ssh/
If the directory / home/remote_user/.ssh/ does not exist, please create it first.
two。 Rename the public key file from copy to authorized_keys
3. Modify access to public key files
Chmod 644 authorized_keys
three。 Example
Goal:
Remote_host:/home/remote_user/data/ from a remote server
Transfer the following files to the current directory of the local computer: / home/local_user/data/:
20050201
20050202
20050203
20050204
20050205
Mode 1: batch mode
Sftp provides an option-b for centralized storage of sftp commands (this option is mainly used for sftp in non-interactive mode). So for the above target, you can generate the following command file:
Cd / home/remote_user/data/
Lcd / home/local_user/data/
-get 20050201.
-get 20050202.
-get 20050203.
-get 20050204.
-get 20050205.
Quit
Save here as: sftp_cmds.txt
Note: the get command is preceded by a "-" to prevent the sftp execution process from being terminated when it makes an error.
The following is an example script:
#! / bin/sh
Sftp-b. / sftp_cmds.txt remote_user@remote_host
Method 2:
#! / bin/sh
Sftp remote_user@remote_host
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.