生产实践:
批量分发公钥
学习技巧:
for循环及expect使用
脚本内容:
#!/usr/bin/expect -f
############################################################
# $Name: tra_pub.exp
# $Version: v1.0
# $Function: transfer id_rsa.pub
# $Author: Zhihe An
# $organization: chegva.com
# $Create Date: 2017-01-11
############################################################
set timeout -1
set user anzhihe
set passwd "anzhihe"
for { set i 19 } { $i < 25 } { incr i } {
spawn ssh-copy-id -i /home/anzhihe/.ssh/id_rsa.pub $user@192.168.64.$i
expect {
"yes/no" { send "yes\r";exp_continue }
"id_rsa" { send "yes\r";exp_continue }
"*assword" { send "$passwd\r" }
}
}
expect eof
exit -onexit {
send_user "Work is done,Good lucky to you!\n"
}