shell实现免密

#!/bin/bash

createLocalKey () {
    rm -rf ~/.ssh/id_rsa
    rm -rf ~/.ssh/id_rsa.pub
    /usr/bin/expect <<_oo_

        spawn ssh-keygen -t rsa
        expect {
            "*.ssh/id_rsa*" { send "\r";exp_continue }
            "*no passphrase*" { send "\r";exp_continue }
            "*passphrase again*" { send "\r";exp_continue }
            "Overwrite*" { send "y\r";exp_continue }
        }
        expect eof

_oo_

}


copyToRemote () {
    hosts=(master slave1 slave2)
    password="root"
    for host in ${hosts[@]};
    do
      /usr/bin/expect <<_oo_

        spawn ssh-copy-id $host
        expect {
            "*connecting (yes/no)*" { send "yes\r";exp_continue }
            "*password*" { send "$password\r";exp_continue }
        }

        expect eof

_oo_
    done
}


createLocalKey
copyToRemote

  

原文地址:https://www.cnblogs.com/navysummer/p/15779737.html