Search code examples
tclexpectcisco

How to catch a count ports on Cisco Nexus Devices using tcl/expect?


I wrote a script to catch a count of connected ports on Cisco Nexus devices as below:

Command Cisco:

example01#sh interface status | i connected | exclude Po | exclude Lo|count
13
example01#

Script Part:

send "sh interface status \| i connected \| exclude Po \| exclude Lo \| count\r"
expect -re {([0-9]+)}
set COUNT "$expect_out(0,string)"
puts "$COUNT\r"

The problem is the hostname of device have numbers too, like 01. When I save the ouput, what I see is just "01", and not the correct output that I expect (this case will be 13).


Solution

  • I did this:

    send "sh interface status \| i connected \| exclude Po \| exclude Lo \| count\r"
    expect -re {([0-9]+)\r\n}
    set COUNT "$expect_out(0,string)"
    puts "$COUNT\r"
    

    Now, the output is correct, because I did a newline with \r\n.