Search code examples
linuxvariablestclexpectcisco

Tcl expects output from tclsh to shell script


I'm searching for last few days for solution to my problem so finally I came here.

I got to write a Tcl script that should be opened from a Linux/Debian system. The problem is, that the script should log into a Cisco router and then go into the special tclsh mode, where it should get the SNMP value which represents txload. After a moment I get the value in tclsh mode (which supports SNMP), but I can't get it back to my script. It still exists only in router tclsh mode. My script should change some other values depending on the value I already have in tclsh, but tclsh is not needed anymore.

So here's my question: How can I get the $ifnum value from tclsh but I could still use in that script for some loops, because I need to make some loops where the $ifnum will be the condition?

As you can see, the script is not finished yet, but as I said, I'm stuck here and trying to figure it out:

#!/usr/bin/expect -f

set gateway [lindex $argv 0]
set serial [lindex $argv 1]
set timeout 5

spawn telnet $gateway
expect "Password:"
send "cisco\r"
expect "*>"
send "en\r"
expect "Password:"
send "cisco\r"
expect "*#"
send "set ifnumstr \[snmp_getone odczyt 1.3.6.1.4.1.9.2.2.1.1.24.6\]\r"
expect "*}"
send "regexp \{val='(\[0-9\]*)'\} \$ifnumstr \{\} ifnum \r"
expect "*#"
send "puts \$ifnum \r"
expect "*#"

Solution

  • I'd make it print out the thing you're looking for between markers, and then use expect in its RE matching mode to grab the value.

    send "puts \"if\\>\$ifnum<if\"\r"
    expect -re {if>(.*?)<if}
    # Save the value in a variable in the *local* script, not the remote one
    set ifnum $expect_out(1,string)