expect--自動批量分發公鑰腳本
阿新 • • 發佈:2018-12-16
word div nbsp 腳本 命令 pass 變量 ini 安裝epel源
1.在使用之前,先安裝epel源,yum install expect -y
2.寫分發腳本,後綴為exp
#!/usr/bin/expect set host_ip [lindex $argv 0] spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $host_ip expect { -timeout 60 "(yes/no)?" { send "yes\n";exp_continue} "password:" { send "Ak46\n"} timeout {puts "Connect timeout!";return} } expect eof exit -onexit { send_user "Job has finished!" }
註:set的作用是設置變量,spawn設置執行命令時,可以引用變量;變量的第一個參數為0
3.執行以下命令開始分發
for ip in `cat /root/ip.txt`;do expect /root/fenfa_sshkey.exp $ip ;done
expect--自動批量分發公鑰腳本