bash script to pass password to ssh and scp command method detail

  • 2021-01-18 06:47:08
  • OfStack

Install SSHPASS

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

On the Debian:

[

$ sudo apt install sshpass

]

On the CentOS:

[

$ yum --enablerepo = epel -y install sshpass

]

Using SSHPASS

sshspass uses sshspass environment variables to store user passwords. You need to use the sshspass variable and password first, 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 run the ssh command using sshpass, as shown below

[

sshpass -e ssh user@example.com

]

shell script to solve the SCP command need to enter a password problem

Use key files.

It is assumed that host A (192.168.100.3) is used to obtain files from host B (192.168.100.4).

Execute the following command on host ES63en to generate the paired key:

ssh-keygen -t rsa

The public key is stored in the user directory.ssh directory. For example, root is stored in:

/root/.ssh/id_rsa.pub

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

To host A performed in order to build trust and host B, cases (assuming that host B IP: 192.168.100.4) :

scp ~/.ssh/id_rsa.pub 192.168.100.4:/root/.ssh/authorized_keys

Can use the below scp, ssh command does not need a password to get the document on host B ssh 192.168.100.4 enter don't need a password.

Note: actually id_rsa) pub content added to the other machine authorized_keys


Related articles: