当前位置:网站首页>Shell script configures root to login to other hosts without secret

Shell script configures root to login to other hosts without secret

2022-07-19 05:18:00 RobertTeacher


This article will guide you to use shell Script to configure the secret free operation between hosts .
Scripting depends on expect Package implements interactive operation .
About expect Package installation method , Please refer to the appendix to complete the installation .

One 、shell Script code

Here is shell Script code

There are three steps
One 、 To configure hosts file ,host Add the information of the secret free host to the file .
Two 、 Determine whether the local public key exists , If it does not exist, you need to generate a public key .
3、 ... and 、 Copy the public key to the secret free host , Realize secret free operation .
The code is as follows ( Example )

#!/bin/bash
#  To configure root Login to other hosts without password 
#  Pass three parameters  1、 Password free host  2、 Secret free host IP 3、 The host name of the secret free host 
# @Author Cym
########################
#  One 、 To configure hosts file 
sed -i '/'"$3"'/d' /etc/hosts eval `echo $2 $3 >> /etc/hosts` filepath=~/.ssh/id_rsa.pub #  Two 、 Determine whether the local public key exists , If it does not exist, you need to generate a public key  [ ! -f $filepath ] && { ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
}
#  3、 ... and 、 Realize secret free operation 
expect -c "set timeout -1; spawn ssh-copy-id $3; expect { *(yes/no)* {send -- yes\r;exp_continue;} *password:* {send -- $1\r;exp_continue;} eof { exit 0;} }";

Two 、 Demonstration of script execution results

The script results run as shown in the following figure ,expect It will automatically help us complete the interactive operation .
shell Script configuration root Users log in to other hosts without password

appendix

install expect

Offline installation

Offline installation expect Two offline packages are required ,expect_5.45-2_amd64.deb and tcl8.5_8.5.11-1ubuntu1_amd64.deb, I have sorted it out for you and put it in my personal space resources , Please download .

Online installation

ubuntu The system is installed online with the following commands

The code is as follows

apt-get install expect

centOS The system is installed online with the following commands

The code is as follows

yum install expect

Conclusion

That's all Shell Script configuration root All contents of password free login to other hosts , If you like, pay attention , I will update technical articles from time to time .

原网站

版权声明
本文为[RobertTeacher]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207170506002181.html