- 安装expect
sudo yum install -y tcl tk expect
- 编写exp文件audt.exp
#!/usr/bin/expect
set timeout 10
set user_hostname [lindex $argv 0]
set password [lindex $argv 1]
spawn ssh-copy-id $user_hostname
expect {
"(yes/no)?"
{
send "yes\n"
expect "*password: " { send "$password\n" }
}
"*password: " { send "$password\n" }
}
expect eof
- 编写批量脚本batch.sh
#!/bin/bash
ip=`echo -n "$(seq -s "," 10 13),30" | xargs -d "," -i echo 192.168.10.{}`
password="password"
#user_host=`awk '{print $3}' /home/vagrant/.ssh/id_rsa.pub`
for i in $ip;do
/home/vagrant/auto.exp root@$i $password &
ssh vagrant@$i "echo $i ok"
done