How to establish SSH Secret Free between servers in batch by Linux
This article mainly introduces how Linux batch establishment between SSH servers free of secret, has a certain reference value, interested friends can refer to, I hope you read this article after a lot of gains, the following let Xiaobian take you to understand.
First, batch establishment of SSH private key authentication
1. Compile and install expect
Expect depends on tcl, which is not installed on linux systems, so it needs to be installed manually.
(1)Install tcl
cd /opt/tcl8.4.11/unix ./ configure make && make install
(2)Install Expect
cd expect-5.43.0 ./ configure --with-tclinclude=/opt/tcl8.4.11/generic --with-tclconfig=/usr/local/lib/ make && make install
(3)test
2. Master generates public key
Execute ssh-keygen, which will create id_rsa and id_rsa.pub files in ~/.ssh/directory by default, which are public key and private key respectively.
ssh-keygen cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
3. related scripts
The following are all placed in the/root directory
(1) ip.txt
Note that IP is in front, followed by password, separated by colon: , if the password has colon, it is recommended to deal with it separately.
IP: Password
(2) remote_operate.sh
#!/ bin/bash #copyright by hwb if [ ! -d /root/.ssh ];then mkdir /root/.ssh fi cat /tmp/authorized_keys >> /root/.ssh/authorized_keys
(3) batch_sshkey.sh
#!/ bin/bash #copyright by hwb for i in `cat ip.txt` do ip=$(echo "$i"|cut -f1 -d":") password=$(echo "$i"|cut -f2 -d":") expect -c " spawn scp /root/.ssh/authorized_keys /root/remote_operate.sh root@$ip:/tmp/ expect { \"*yes/no*\" {send \"yes\r\"; exp_continue} \"*password*\" {send \"$password\r\"; exp_continue} \"*Password*\" {send \"$password\r\";} } " expect -c " spawn ssh root@$ip "/tmp/remote_operate.sh" expect { \"*yes/no*\" {send \"yes\r\"; exp_continue} \"*password*\" {send \"$password\r\"; exp_continue} \"*Password*\" {send \"$password\r\";} } " done
4. Execute scripts and test
Run batch_sshkey.sh script
Thank you for reading this article carefully. I hope that Xiaobian's article "How to build SSH confidentiality between servers in batches" shared by Linux will help everyone. At the same time, I hope that everyone will support you a lot and pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!