The second ssh key distribution: use sshpass and ssh-kopy-id to write a script to distribute keys in batches:

Use sshpass and ssh-kopy- id to write a script to distribute keys in batches:


First of all, sshpass is an interactive tool for ssh connection. First install it:
yum  install sshpass -y

Next we can use the sshpass tool to distribute the ssh public key in one command:
sshpass -p " ssh login password "  ssh -copy- id -i /root/.ssh /id_dsa.pub -o StrictHostKeyChecking= no [email protected]  
At this point, the public key can be sent to the managed server:
Notice:
sshpass : a non-interactive tool for ssh
- p : Specify the password for client login
 ssh -copy- id : A tool for automatically distributing public keys
 - i : Specify the path of the local public key
 -o : StrictHostKeyChecking= no The first link will be written in the know_hosts file, here Indicates that the information is not written.

To write a script to distribute keys in batches, first of all we need to assume that all clients use the root account and the password of the root account
are all the same.

# !/bin/ bash
 '''
 description: batch distribution of ssh keys
author: fengjunhua
Date :         2018 - 4 - 27 
use, write ip to a file casually, specify the script. /ssh_copy.sh file name
 '''
 password= #client server password

for ip in `cat $1`
do
    echo "Test $IP if is alive"
    ping $ip -c1 &>/dev/null
    if [ $? -gt 0 ];then
        echo "$ip 无法ping通"
        continue
    else 
        echo  " Distributing keys " 
        sshpass -p " $password "  ssh -copy- id -i /root/.ssh /id_dsa.pub -o StrictHostKeyChecking=no root@{$ip} &>/dev/ null 
        echo  " $ip key distribution successful " 
    fi 
done

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324982762&siteId=291194637
Recommended