添加互信
安装 expect
sudo apt-get install tcl tk expect
创建脚本和主机列表文件,所有服务器密码需一样,根据实际环境,需要更改脚本里的密码
cat << EOF > ~/auto_ssh.exp
! /usr/bin/expect
执行ssh-keygen命令,如果需要就解注释
spawn ssh-keygen
expect “id_rsa”
send “\r”
expect “phrase”
send “\r”
expect “again”
send “\r”
interact
set f [open auto_ssh_host_ip r]
while { [gets $f ip]>=0} {
spawn ssh-copy-id $ip
expect {
“yes/no” {send “yes\r”;exp_continue}
“password:” {send “123123\r”;exp_continue}
}
}
close $f
EOF
创建主机列表文件
cat << EOF > ~/auto_ssh_host_ip server-31 server-32 server-33 EOF
执行脚本
expect ~/auto_ssh.exp