#!/usr/bin/expect
if {$argc != 2} {  #首先注意大话号,彼此之间需要空格
  send_user "USAGE:expect_sshkey.exp file host"
  exit
}

#define var
set file [lindex $argv 0]
set host [lindex $argv 1]
set password "12345"

#spawn
spawn ssh-copy-id -i $file "-p52113 jpinsz@$host" >/dev/null 2>&1

#expect
expect { #也是要注意大话号,与首单词之间需要空格
        "yes/no" {send "yes\r";exp_continue}
        "*password" {send "$password\r";send_user "eof/n"}
}

expect eof

根据这个脚本,当目标主机已经存在sshkey时,将不会提示“yes/no”、“*password”,因此也不会有eof,所以会有
spawn id exp4 not open这个报错!!