bjforesthowell
bjforesthowell

Reputation: 67

Using regular expressions with expect

What I'm trying to do is write an expect script that will allow me to search for file A, and if file A is found then send a series of commands. If files B, or C are found then continue on through the script. I keep getting stuck on the regular expression. I run the expression through www.myregextester.com alone with the data I'm searching for and it matches up just fine. If anyone has any experience with this I would really appreciate the help.

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
}

Upvotes: 1

Views: 3554

Answers (1)

glenn jackman
glenn jackman

Reputation: 247162

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

Expect is an extension of Tcl: you can read about Tcl regular expressions here.

Upvotes: 2

Related Questions