eof在Expect Script中无法识别

时间:2013-12-02 13:59:21

标签: tcl expect

我的期望剧本中遇到了一些问题。 我正在产生一个scp文件上传但是eof无法识别。 在脚本继续运行之前,期望进入超时状态。

我做错了什么?

这是脚本

    function ssh_upload_file () {

test_log INFO "Uploading File $3 to Device $2 with a $1 seconds timeout" "ssh_upload_file"
last_log_entry_to_detail

expect <<- DONE
set outfilename "$DETAIL_LOG"
log_file "$DETAIL_LOG";

set timeout $1
set lantime_ip $2

system "touch  ~/.ssh/known_hosts"
system "ssh-keygen -R $2"
spawn /bin/bash
sleep 3
send_user "Uploading File via SSH...\n"
send "scp $2 $3:$4 || echo FWTEST_SSH_RESULT=\$?\n"
expect "assword:" {
        after 1000
        send "$DUT_PWD\r"
        #set timeout 5
        #expect "y/n"
        #send "n\n"
        #set timeout $1
        #expect "#"
        #send "no_shell_timeout\n"
}
expect eof
send_user "Done."
log_file
exit 0
expect eof
DONE
LAST_ERROR=$?
return $LAST_ERROR
}

1 个答案:

答案 0 :(得分:2)

您生成一个bash shell,并发送一个scp命令。当scp完成时,你正处于bash提示符下。您需要先发送一个exit命令,然后才会看到eof

或者,你没有在那个bash会话中做任何事情,除了捕获你可以这样做的scp命令的输出:

spawn scp $2 $3:$4
expect "assword:" 
send "$DUT_PWD\r"
expect eof
set info [wait]
puts "FWTEST_SSH_RESULT=[lindex [set info] 3]"

解决唐纳德的评论:

spawn scp $2 $3:$4
expect {
    "assword:" {
        send "$DUT_PWD\r"
        exp_continue
    }
    eof
}
set info [wait]
puts "FWTEST_SSH_RESULT=[lindex [set info] 3]"