Sending public keys to multiple remote hosts

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.