使用带有expect的正则表达式

时间:2013-06-12 11:40:25

标签: regex tcl expect

我要做的是编写一个expect脚本,允许我搜索文件A,如果找到文件A,则发送一系列命令。如果找到文件B或C,则继续执行脚本。我一直卡在正则表达式上。我通过www.myregextester.com单独运行表达式和我正在搜索的数据,它匹配得很好。如果有人对此有任何经验,我将非常感谢您的帮助。

send "term length 0\r"
expect "*#"
send "wr mem\r"
expect "*#"
send "dir flash:\r"
expect 
    if [[ \bc3750-ipservicesk9-mz.122-55.SE7.bin\b ]]; then {
        send "conf t\r"
        expect "*(config)#"
        send "boot system flash:c3750-ipservicesk9-mz.122-55.SE7.bin\r"
        expect "*(config)#"
        send "exit\r"
        expect "*#"
        send "reload at 02:00 8 June\r"
        expect "System configuration has been modified. Save? [yes/no]:"
        send "yes\r"
        expect "Proceed with reload? [confirm]"
        send "\r"
        expect "*#"
        send "wr\r"
        expect "*#"
        send "exit\r
        expect eof
    elif [[ \bc3750-ipservicesk9-mz.122-55.SE5\b | b\c3750-ipservicesk9-mz.122-55.SE5.bin\b} ]]; then
fi
}

1 个答案:

答案 0 :(得分:0)

expect {
    -re {\mc3750-ipservicesk9-mz\.122-55\.SE7\.bin\M} {
        send "conf t\r"
        #...
    }
    -re {\mc3750-ipservicesk9-mz\.122-55\.SE5(?:\.bin)?\M} 
}

Expect是Tcl的扩展:你可以阅读关于Tcl正则表达式here