Scene: the customer request, copy the data on the server 96 to 86 on the server, but will automatically copy, not human intervention, 96 server, then I log in using the SSH command, login to 86 server, found that need to enter the password, if you want to write a shell script, use the SCP command copy data, need to manually enter the password.

Based on this scenario, there are two scenarios.

Solution 1: Configure no-password login for servers 96 and 86

Steps:

1. Generate a passwordless key pair

ssh-keygen -t rsa
Copy the code

Enter all the way

2. Add the public key to the local authentication file

cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
Copy the code

3. Set the access permission for authorized_keys

chmod 600 /root/.ssh/authorized_keys
Copy the code

** Perform the above steps on each node first, then perform the following operations

4. Copy the id_rsa.pub generated on each node to all other nodes and add it to their authentication files

For example, copy id_rsa.pub generated on 96 to 86

Pub 86:/root/ (run on 96) cat ~/id_rsa.pub >> ~/. SSH /authorized_keys (run on 86) SCP /root/.ssh/id_rsa.pub 96: /root/cat ~/id_rsa.pub >> ~/. SSH /authorized_keys (on 96)Copy the code

If you run the ssh-keygen -t rsa command, you will overwrite the public key configured before. If you run the SSH -keygen -t rsa command, you will overwrite the public key configured before.

I found id_rsa.pub in the.ssh/ directory.

The second step is to add the id_rsa.pub content to the authorized_keys of the peer machine, and then I add the id_rsa.pub content on the 86 server to the authorized_keys of the 96 machine. If you according to the plan will not solve the problem, then you can use the second scheme, I also use solution 2, because free configuration, configure the number of too much (I of this article blog.csdn.net/Allenzyg/ar… There are also no-secret configuration steps), also want to change the way. Ha ha ha ~ ~ ~

Solution two: Use Expect

Expect is designed for interaction, and can be used for almost any interaction login, but the Expect package needs to be installed.

Steps:

1. Download Expect on server 96

yum -y install expect
Copy the code

2. Configure shell scripts

vim 1.sh
Copy the code

Fill in the following:

#! /usr/bin/expect -f set timeout 30 spawn SCP directory/file 86: / opt/fanRuan/apache tomcat - 8.5.29 / webapps/WebReport/yecai_tctp_income/BL/January/expect "* password:" send "123456 \ r"  expect eofCopy the code

3. Run the 1.sh file

Note: Expect is similar to bash in that you must log in to Expect first, so in the first line you must specify expect file when running the script instead of sh file.

1.sh: line 4: spawn: command not found

couldn’t read file “*password:”: no such file or directory

1.sh: line 6: send: command not found

couldn’t read file “eof”: no such file or directory

The execution is not correct, because Expect is not using bash and therefore reports an error. You can simply execute./1.sh

Sh: Permission denied. -bash:./1.sh: Permission denied

Solution: chmod -r 777 1.sh

The first line specifies the command program to execute the script, in this case /usr/bin/expect

The first statement of the above statement sets the timeout period to 30s, and the spawn statement is expect statement, which must be added before the command is executed

Expect “password: “= “password:” = “password: “= “password:”

Send means to send the password 123456

The EOF character that an Expect Eof child process has ended terminates the Expect script

Interact means stay at the remote console after execution, don’t add this sentence and return to the local console after execution

Reference: knowledge reserves blog.csdn.net/huoyuanshen…