Detailed explanation of how to pass password to ssh/scp command in bash script

Detailed explanation of how to pass password to ssh/scp command in bash script

Install SSHPASS

For most recent operating systems, the sshpass package is available in the default package repositories. It can be installed on your system using the following command.

On Debian:

$ sudo apt install sshpass

On CentOS:

$ yum --enablerepo=epel -y install sshpass

Using SSHPASS

sshspass uses the sshspass environment variable to store the user password. You need to first use the sshspass variable and password and then use this command. Below is a simple shell script using sshspass.

export SSHPASS=ry3AvbWO0oupYX9HCMzp0Axx

sshpass -e scp testfile.csv [email protected]:/uploads/

You can also run ssh commands using sshpass as shown below

sshpass -e ssh [email protected]

Solve the problem of SCP command requiring password in shell script

Use a key file.

Here 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 a pairing key:

ssh-keygen -t rsa

Press Enter when prompted. The public key is saved 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 to authorized_keys.

Execute commands on host A to establish trust with host B, for example (assuming 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 and ssh commands to obtain files from host B without a password. ssh 192.168.100.4 Press Enter and no password is required.

Note: Actually, just add the content of id_rsa.pub to the authorized_keys of the other machine.

You may also be interested in:
  • Vue-CLI 3 scp2 automatic deployment of projects to the server method
  • Python calls scp to upload files to the server example
  • How to use ssh and scp through secret keys in Windows
  • Python executes scp command to copy files and folders to the directory of the remote host
  • Linux uses the scp command to copy files to the local computer and copy local files to the remote server
  • Python Pexpect implements the method of scp copying with password input
  • Detailed explanation of using scp command to copy files remotely in Linux
  • scp copies data from one linux server to another linux server
  • How to implement scp operation in Android

<<:  MySQL batch removes spaces in a certain field

>>:  A brief discussion on the efficiency of MySQL subquery union and in

Recommend

Using front-end HTML+CSS+JS to develop a simple TODOLIST function (notepad)

Table of contents 1. Brief Introduction 2. Run sc...

Introduction to TypeScript interfaces

Table of contents 1. Interface definition 2. Attr...

Vue implements the full selection function

This article example shares the specific code of ...

Bootstrap FileInput implements image upload function

This article example shares the specific code of ...

Detailed explanation of Django+Vue+Docker to build an interface testing platform

1. Two words at the beginning Hello everyone, my ...

How to implement import and export mysql database commands under linux

1. Export the database using the mysqldump comman...

How to implement remote access control in Centos 7.4

1. SSH remote management SSH is a secure channel ...

Share CSS writing standards and order [recommended for everyone to use]

CSS writing order 1. Position attributes (positio...

How to use Baidu Map API in vue project

Table of contents 1. Register an account on Baidu...

How to import Tomcat source code into idea

Table of contents 1. Download the tomcat code 2. ...

Sample code of uniapp vue and nvue carousel components

The vue part is as follows: <template> <...

js memory leak scenarios, how to monitor and analyze them in detail

Table of contents Preface What situations can cau...

Linux Basic Tutorial: Special Permissions SUID, SGID and SBIT

Preface For file or directory permissions in Linu...