Search code examples
bashtclexpectcisco

Error in Cisco Expect script


I wrote a basic Cisco expect script. After ssh connection, I want to detect Cisco error output when I send a command line from a file, for example:

SPAIN#sow crypto isakmp sa
       ^
% Invalid input detected at '^' marker.

I want to catch "% Invalid", in my loop and stop the programm.

foreach line $data {
    expect "*#"
    send "$line\r"
    expect {
        "% Invalid*" { send_user "\n Command [ $line ] Failed. \n"; exit 252 }
    }

I get these errors in the output:

SPAIN>enable
Password:
SPAIN#sow crypto isakmp sa
   ^
% Invalid input detected at '^' marker.

SPAIN#invalid command name "sow crypto isakmp sa"
while executing
"$line "
invoked from within
"expect {
     "% Invalid*" { send_user "\n Command [ $line ]  Failed. \n"; exit 252}
}"
("foreach" body line 4)
invoked from within
"foreach line $data {
    expect "*#"
    send "$line\r"
    expect {
        "% Invalid*" { send_user "\n Command [ $line ] Failed. \n"; exit 252}
    }
}"

thanks


Solution

  • The problem is that [] has a special meaning in Tcl; to execute a command and return the result.

    Change this:

    expect {
        "% Invalid*" { send_user "\n Command [ $line ] Failed. \n"; exit 252 }
    }
    

    To:

    expect {
        "% Invalid*" { send_user "\n Command \[ $line \] Failed. \n"; exit 252 }
    }
    

    See Evaluation & Substitutions 3: Grouping arguments with [] in the Tcl manual for more information.