脚本实现免密登入多台设备

#!/bin/bash
#---------------设置ssh免密登入对台设备---------------#
#设置要登入的节点ip地址
HOST_IP_NODE=(ip1 ip2 ip3 ... ipn)
HOST_PASS_NODE=(ps1 ps2 ps3 ... psn)
#安装expect软件包
yum install expect -y
expect << EOF
set timeout 5
spawn ssh-keygen -t rsa
expect "id_rsa):" 
send "\r" 
expect "passphrase):" 
send "\r"
expect "again:"
send "\r" 
expect eof
EOF
 
copy-id(){
expect << EOF
set timeout 5
spawn ssh-copy-id root@$1
expect "(yes/no)?" 
send "yes\r"
expect "password:" 
send "$2\r" 
expect eof
EOF
}
 
#循环遍历
num=${#HOST_IP_NODE[*]}
fun(){
    for i in 0 1 ... $num
      do
        copy-id root@${HOST_IP_NODE[i]} ${HOST_PASS_NODE[i]}
      done
}

你可能感兴趣的:(服务器,linux,运维)