#!/usr/bin/expect -f
set ip [lindex $argv 0 ]
set user [lindex $argv 1 ]
set password [lindex $argv 2 ]
set timeout 10
spawn ssh-copy-id $user@$ip
expect {
#first connect, no public key in ~/.ssh/known_hosts
"Are you sure you want to continue connecting (yes/no)?" {
send "yes\r"
expect "password:"
send "$password\r"
}
#already has public key in ~/.ssh/known_hosts
"*assword:" {
send "$password\r"
}
"Permission denied, please try again." {
send_user "Wrong password"
return
}
"Now try logging into the machine" {
#it has authorized, do nothing!
}
}
expect eof