如何使用 tcl/expect 捕获 Cisco Nexus 设备上的计数端口?

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

我编写了一个脚本来捕获 Cisco Nexus 设备上连接端口的计数,如下所示:

命令思科:

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

脚本部分:

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"

问题是设备的主机名也有数字,比如 01。当我保存输出时,我看到的只是“01”,而不是我期望的正确输出(本例为 13)。

我这样做了:

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"

现在,输出是正确的,因为我用 \r\n.

换行了