vim shell.exp
#!/usr/bin/expect
set timeout 10
set hostname [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
spawn ssh-copy-id $username@$hostname
expect {
"Are you sure you want to continue connecting (yes/no)?" {
send "yes\r"
expect "*password:"
send "$password\r"
}
"*password:" {
send "$password\r"
}
"Now try logging into the machine" {
}
}
expect eof
一个bash搞定,完犊子
在bash中,用expect -c "" 把expect语句包起来,将expect -c "" 中的双引号加上反斜杠 ---------------------------------------------------------------------
#!/bin/bash
USER=root
PASSWD=1233
# yum install -y expect
for HOST in 192.168.1.{1..10}
do
echo "------------------>" $HOST "-----------------------------"
/usr/bin/expect -c "
spawn ssh-copy-id $USER@$HOST;
expect {
\"Are you sure you want to continue connecting (yes/no)?\" {
send \"yes\r\"
expect \"*password:\"
send \"$PASSWD\r\"
}
\"*password:\" {
send \"$PASSWD\r\"
}
\"Now try logging into the machine\" {
}
}
expect eof
"
done