ssh免秘钥登录脚本
1、yum -y install expect
2、cat auto_ssh.sh
#!/usr/bin/expect
set timeout 10
set username [lindex $argv 0]
set password [lindex $argv 1]
set hostname [lindex $argv 2]
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $username@$hostname
expect {
#first connect, no public key in ~/.ssh/known_hosts
"Are you sure you want to continue connecting (yes/no)?" {
send "yes\r"
expect "password:"
send "$password\r"
}
#already has public key in ~/.ssh/known_hosts
"password:" {
send "$password\r"
}
"Now try logging into the machine" {
#it has authorized, do nothing!
}
}
expect eof
=========================================
3、cat ssh_key.sh
#!/bin/bash
user="root"
password="123456"
ip="cat /root/ip.txt"
for i in `$ip`
do
/root/auto_ssh.sh $user $password $i
done
==========================================
4、cat /root/ip.txt #要同步免秘钥的ip地址
192.168.0.201
192.168.0.202
5、chmod 777 ssh_key.sh
6、chmod 777 auto_ssh.sh
7、./ ssh_key.sh 执行完同步成功