【发布时间】:2015-07-15 22:05:33
【问题描述】:
我正在创建一个期望脚本,让我在管理 cisco 设备时使用宏。
所有连接完成后,我的脚本等待“#”,因此我知道用户已准备好输入。如果我在交互之后定义我的过程,脚本可以正常工作,但显然它还没有被声明,所以脚本失败了。如果我在交互之前定义它,我会超时,就像它实际上是“期待”一样
为什么这个过程会在没有被调用的情况下“运行”?
proc portSec {port} {
send "show port interface $int\r"
expect {
-nocase -re "(invalid)|(ambig}" {
puts "\nInvalid Interface\n"
return
}
-nocase -re "(\[0-9\]+.\[^ \]+.\[^ \]+):\[^ \]" {
set mac $expect_out(1,string)
}
}
~~~~expect "#" ~~~~~~
send "show port address \| i $mac\r"
expect "#"
}
interact {
"!p" {
send_user "\nWelcome to macro mode!\nWhich interface would you like to clear port security?: "
stty echo
expect_user -re "(.*)\r" {
set port $expect_out(1,string)
}
stty -echo
portSec $port
send "\r"
}
}
这里是调试
expect: does " \r\nYour password will expire in 1 week, 5 days, 3 hours,
44 minutes and 56 seconds
\r\r\n\r\nHOSTNAME line 1 \r\n\r\nHOSTNAME#" (spawn_id exp4)
match glob pattern "#"? yes
expect: set expect_out(0,string) "#"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) " \r\nYour password will expire in 1 week,
5 days, 3 hours, 44 minutes and 56 seconds\r\r\n\r\nHOSTNAME line 1
n\r\nHOSTNAME#"
expect: does "" (spawn_id exp4) match glob pattern "#"? no
expect: timed out
can't read "mac": no such variable
while executing
"send "show port address \| i $mac\r""
(file "./ios.exp" line 78)
单步调试调试器,我在它期望的那一行加了波浪号“~~~”
【问题讨论】:
-
你能发布整个代码吗?你确定程序
portSec之前没有调用过吗? -
这真是令人惊讶。其实并不是proc
portSec自动调用的。似乎expect以某种方式将这些行视为 proc 外部,而不是内部,这就是发生此问题的原因。这是由于代码expect { -nocase -re "(invalid)|(ambig}" { puts "\nInvalid Interface\n" return } -nocase -re "(\[0-9\]+.\[^ \]+.\[^ \]+):\[^ \]" { set mac $expect_out(1,string) } }。 -
没有这个
expect,没关系。想知道并试图找出它为什么会这样。我怀疑方括号[。为了匹配文字[,它应该被放置为\\\[。那也解决不了。让我试试看。
标签: stored-procedures tcl expect procedure