如何返回到期望

时间:2016-06-09 21:14:41

标签: tcl expect

我正在尝试使用expect重启系统。这是我的代码:

#!/usr/bin/expect
set timeout 20
set name [lindex $arg0]
set user [lindex $arg1]
set password [lindex $arg2]
spawn telnet $name
expect "login:"
send "$user\n"
expect "Password:"
send "$password\n"
expect "<system prompt>$"
send "<some command>\n"
expect "<system prompt>$"
send "./my_script\n" --> code is below
send "<back to the telnet system prompt and running a builtin command>"
expect "g>"
send "some built in command"

这是my_script的代码(我在这里重新启动系统,之后连接丢失,上面的脚本(主脚本)在行结束)

#!/usr/bin/expect
set name [lindex <some ip>]
set user [lindex <name>]
set password [lindex <password>]
spawn telnet $name
expect "Login:"
send "$user\n"
expect "Password:"
send "$password\n"
expect "<system prompt>"
send "reboot\n"
expect "Are you sure you want to reboot (y/n)?"
send "y"

这里它失去了连接,永远不会返回主脚本并继续使用脚本的其余部分。

注意:如果我在两个脚本的末尾放置了交互,但是我不想进行交互,因为我们不想要用户提供任何内容。

1 个答案:

答案 0 :(得分:0)

你的内心剧本可能应该这样做:

expect eof
close
exit

最后。但是,更常见的是你应该添加:

exp_internal 1

到你的脚本(通常在spawn之前)进行调试时,你可以弄清楚出了什么问题。否则你可能遇到意想不到的问题而不知道。