通过expect,读取配置文件,批量ssh-add

1.配置文件(多台宿主机) id_dsa.txt
id_dsa_192.168.2.112
id_dsa_192.168.2.113
id_dsa_192.168.3.114
id_dsa_192.168.2.115

2.编写批量ssh-add脚本 ssh-addAgent.sh

#!/usr/bin/expect -f
set user "zhangjun"
set passwd "123456"
set file "id_dsa.txt"
set sshhome "/home/$user/.ssh"
set fd [open $file r]
set n 0
while {[gets $fd line] != -1} {
incr n
spawn ssh-add $sshhome/$line
expect  {
"*passphrase" {send "$passwd\r"}
}
interact
puts "$line"
}
close $fd

你可能感兴趣的:(expect)