expect script regex not working

492 views Asked by At

This script is failing to work as expected:

#!/usr/bin/expect
set timeout 2
set server [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
set mac [lindex $argv 3]
set interface "po1"
spawn ssh $user@$server
expect "Password:"
send -- "$password\n"
expect "*>"
send "show mac address-table address $mac\n"
# 100      1cc1.de65.441c   dynamic ip,ipx,assigned,other Port-channel43
expect -re { (\d+) *($mac) *(dynamic|static) *(.*) *(.*)} {
        set interface $expect_out(5,string)
        expect "*>"
        send "show interface $interface status\n"
}
send "exit\n"
interact

After issuing the show mac command above, the output contains one line that looks like the one commented below it. but the following expect -re block is never hit, making it time out and send the exit command.

Sample output: spawn ssh user@host Password:

================= Host login banner  =================
host>show mac address-table address 1cc1.de65.441c
Unicast Entries
 vlan     mac address     type        protocols               port
---------+---------------+--------+---------------------+-------------------------
 100      1cc1.de65.441c   dynamic ip,ipx,assigned,other Port-channel43


host>exit
Connection to host closed.
1

There are 1 answers

1
Dinesh On BEST ANSWER

Your regex is not correct and inside braces substitutions won't happen. i.e. $mac won't be substituted as it is inside braces.

expect {
    -re "\\d+\\s+$mac\\s+(dynamic|static)\\s+\\S+\\s+(\\S+)" { set interface $expect_out(2,string); # Add your further code}
}