The use of automation software to manage multiple hosts, the first thing to do is to put their own public key on the remote host, and sometimes, need to manage the host too much, is also a difficult thing, so the following is I write the automatic upload of the public key script, hoping to help the needs of friends
Note :
Because the upload public key needs the other's IP address, so this script will read a ip.txt file, please note
Ip= ' Cat ~/ip.txt '
#!/bin/bash# #1. Add the user and password of the remote host to the script ############################################################################# read -p "Please enter the host User:" userread -p "Please enter the host password:" mimaread -p "Please enter your password again:" querenif [ $mima == $queren ]thenecho "The password you entered is: $queren" && sleep 1echo "is creating a key pair for you, please note that" && sleep 1elseecho "the password you entered two times does not match, Please re-enter "read -p " Please enter the host User: " userread -p " Please enter the host password: " mimaread -p " Please enter your password again: " querenif [ $mima == $queren ]thenecho " The password you entered is: $queren " echo "Creating key pair for you, please note" && sleep 1elseecho "You have entered a different password two times, please re-execute the script" fifi###################################################################### ####### #2. Check the secret key, create the key if [ -f /root/.ssh/id_rsa.pub ]thenecho "Your host has a key pair, you don't need to create it again" elsessh-keygen -t rsa -p ‘‘ firpm -q sshpass &> /dev/nullif [ $? -ne 0 ]thenyum - Y install sshpass &> /dev/null echo "Stricthostkeychecking no" > /root/.ssh/config echo "Userknownhostsfile=/dev/null" >> /root/.ssh/configelseecho " Stricthostkeychecking no " > /root/.ssh/config echo "Userknownhostsfile=/dev/null" >> /root/.ssh/configfi#3. Batch add key pair ip= ' cat ~ /ip.txt ' for i in $ipdosshpass -p $queren ssh-copy-id [email protected] $i &> /dev/nullssh [email protected] $i "Cat /root/.ssh/authorized_keys | uniq > 1.txt | cat 1.txt > /root/.ssh/authorized_keys " &> /dev/nullif [ $? -eq 0 ] thenecho "$i is add successful" Elseecho "$i is failure" Fidone
This article is from the "Rslinux" blog, make sure to keep this source http://readshlinux.blog.51cto.com/9322509/1789574
Sending public keys to multiple remote hosts