Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of the method of passing password to ssh/scp command in bash script

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Install SSHPASS

For most of the latest operating systems, the sshpass package is available in the default package repository. You can install it on the system using the following command.

On Debian:

$sudo apt install sshpass

On CentOS:

$yum-- enablerepo = epel-y install sshpass

Use SSHPASS

Sshspass uses the sshspass environment variable to store user passwords. You need to first use the sshspass variable and password, and then use this command. Here is a simple shell script that uses sshspass.

Export SSHPASS=ry3AvbWO0oupYX9HCMzp0Axx

Sshpass-e scp testfile.csv user@example.com:/uploads/

You can also use sshpass to run the ssh command, as follows

Sshpass-e ssh user@example.com

Solving the problem of entering password for SCP command in shell script

Use the key file.

It is assumed that host A (192.168.100.3) is used to obtain the file of host B (192.168.100.4).

Execute the following command on host A to generate the pairing key:

Ssh-keygen-t rsa

When prompted, press enter by default, and the public key is stored in the .ssh directory under the user directory. For example, root is stored in:

/ root/.ssh/id_rsa.pub

Copy the id_rsa.pub file in the .ssh directory to the ~ / .ssh/ directory of host B and rename it authorized_keys

Execute the command in host An and establish trust with host B, for example (assuming that the IP of host B is: 192.168.100.4):

Scp / .ssh/id_rsa.pub 192.168.100.4:/root/.ssh/authorized_keys

Next, you can use the scp, ssh commands to get the file of host B without a password. Ssh 192.168.100.4, enter does not need a password.

Note: in fact, the id_rsa.pub content can be added to the authorized_keys of the other machine.

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report