我有一个expect
脚本包装在bash脚本中,用于根据是否从Cisco路由器提取IP地址执行特定命令。我试图编码的算法如下所示:
send -h "ip int br"
expect -re {
if "192.[0-9]{1,3}.{2}[0-9]{1,3}"
{
expect
{
"up/up { do these things here }
"down/down" { do this instead }
}
}
else
{
send -h "<another command>"
expect
{
"up/up" { do this }
"down/down" { or this if down }
}
}
}
我该怎么做?我已经尝试使用expect
的等同于-ex
标志的switch
语句,如回答另一个用户的问题所建议的(根据预期回报做出决定),但没有任何结果。
任何想法?
我将捕获返回的地址,然后对其进行操作。我看到重复的代码,所以尽量减少:
send -h "ip int br"
expect {
timeout {error "can't find an ip address after 'ip int br' command"}
-re {md{1,3}(.d{1,3}){3}M}
}
if { ! [regexp {192.[0-9]{1,3}.{2}[0-9]{1,3}} $expect_out(0,string)]} {
send -h "<another command>"
}
expect {
"up/up" { do these things here }
"down/down" { do this instead }
}
其他笔记:
- 注意,不要将一个块的开始大括号放在它自己的行上,而是与命令依偎在一起。
- 把正则表达式放在{大括号}中,因为这个
"192.[0-9]{1,3}.{2}[0-9]{1,3}"
会给你一个像invalid command name "0-9"
一样的错误——括号是Tcl特有的,需要转义,除非它们在大括号中。