cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2079
Views
34
Helpful
17
Replies

TCL Scripting help needed

CCampbell_2
Level 1
Level 1

I'm trying to have my router use Tcl to pull down a webpage from a cable modem.

Here's my code so far.

set sock [socket 192.168.100.1 80]

puts $sock "GET /signaldata.htm HTTP/1.0\n\n"

flush $sock

set result [ gets $sock ]

puts stdout "OUTPUT:\n$result"

set exit_msg "closing socket"

close $sock

It doesn't work. All I get is:

OUTPUT:

I know the URL works because I can pull it up fine from my browser and a telnet to port 80 from the router itself also opens the socket for a brief moment.

Can anyone tell me what I'm doing wrong?

Thanks

17 Replies 17

Joe Clarke
Cisco Employee
Cisco Employee

I think you want:

set sock [socket 192.168.100.1 80]

puts $sock "GET /signaldata.htm HTTP/1.0\n\n"

flush $sock

set result ""

while { [gets $sock line] >= 0 } {

append result $line

append result "\n"

}

puts stdout "OUTPUT:\n$result"

set exit_msg "closing socket"

close $sock

That is, you need to iterate over all the lines in the socket input stream until you get to an EOF.

That did 'er thanks! I had gotten a little further last night with:

set sock [socket 192.168.100.1 80]

set counter 0

puts $sock "GET /signaldata.htm HTTP/1.0\n\n"

flush $sock

set result [ gets $sock ]

puts stdout "OUTPUT:\n$result"

while {$counter < 75} {

set result [ gets $sock ]

puts stdout "$result"

incr counter

}

close $sock

But it was unreliable, frequently dropping half of the page. Your code works perfect everytime!

Thanks again!

Cliff

Does anyone know if there are any TCL programming books that are dedicated to coding for Cisco devices? If so, could you please tell me the titles?

There are not. The documentation on Cisco.com regarding TCL in IOS (http://www.cisco.com/en/US/products/sw/iosswrel/ps5207/products_feature_guide09186a00801a75a7.html) details the differences between host-based TCL 8.3.4 and IOS TCL 8.3.4. From there, all you need is a good general TCL book. I recommend Practical Programming in Tcl and Tk (http://www.amazon.com/Practical-Programming-Tcl-Tk-4th/dp/0130385603/ref=pd_bbs_sr_1/105-1282870-4404422?ie=UTF8&s=books&qid=1181305075&sr=8-1).

Thank you for the suggestion but I am a little worried after reading this person's review on it:

http://www.amazon.com/gp/cdp/member-reviews/A3K9A23UIV25CQ/ref=cm_cr_auth/105-0709698-5602867?ie=UTF8&sort%5Fby=MostRecentReview

I am not an experienced programmer and think perhaps I may have the same issues as this person. Do you have any other suggestions?

That's the only book I've used for my TCL in IOS work. The other references I find useful are the various TCL proc man pages (http://www.tcl.tk/man/tcl8.3/TclCmd/contents.htm).

I think if you have a good knowledge of any other programming language, you will be comfortable with this book.

OK great, thanks for that. By the way, I have put together a small TCL script called proc auto {} but so far the only way I know how to use it is by pasting the config into the router, however, this means each time I want to use it I have to paste the entire script again. So I was just wanting to know how do I go about using the script without having to paste it into the router each time?

I have tried:

puts [eval auto]

But that doesn't work. Any suggestions?

Put the code into a file (e.g. auto.tcl). Upload this file to the router's flash, or place it on a network (e.g. tftp) server. Then, run it using:

Router#tclsh

Router(tcl)#source flash:auto.tcl

Router(tcl)#auto

I use a special syntax in my scripts so that they can be run either using the above syntax, or directly with:

Router#tclsh flash:script.tcl

Attached is a script I wrote to aid in the configuration of SNMP contexts. You can see at the bottom how I handle calling the main proc depending on how the script is executed.

I uploaded the file to flash (automation.tcl) and then I tried:

Router#tclsh

Router(tcl)#source flash:automation.tcl

But I get the following error: "wrong # args: should be "proc name args body""

I then tried:

Router#tclsh flash:automation.tcl

But then I got the following error message:

""proc automation {} "

(file "flash:automation.tcl" line 1)"

I have attached my script.

There were quite a few syntactical problems with this script, but I think I've fixed them while keeping your basic idea in tact. Attached is a script that should do what you want. To run it:

Router#tclsh

Router(tcl)#source flash:automation.tcl

Router(tcl)#automation

Sorry bout the syntactical errors, I'm new to TCL and haven't had much experience with programming in general, however, your alterations and suggestion works exactly the way I was hoping. Thanks a lot, it is much appreciated.

I have run into another problem (see attached code). For some reason when I press 1 on the menu none of the commands are executed. The reason why I have put three "sh run" commands is because I wanted to test them but none of them seem to be working. Can anyone see where Ihave gone wrong?

This script works for me provided interface VLAN 1 exists. You might consider using the catch proc to keep errors from killing your program.

Excellent, thanks for your help.

What do you mean by "catch proc?"

Also, how would I go about storing the output of a show command into a variable and then modifying the text string so that only the information I want is stored in the variable?

For example, from a "show run interface vlan 1" I would like to extract the IP address and put it into a variable.

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: