TCL Scripting help needed

Unanswered Question
Jun 6th, 2007

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 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:


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?


I have this problem too.
0 votes
  • 1
  • 2
  • 3
  • 4
  • 5
Overall Rating: 4.8 (7 ratings)
Joe Clarke Wed, 06/06/2007 - 20:52

I think you want:

set sock [socket 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.

CCampbell_2 Thu, 06/07/2007 - 08:48

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

set sock [socket 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!


voiper_99 Thu, 06/07/2007 - 22:11

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?

Joe Clarke Fri, 06/08/2007 - 04:18

There are not. The documentation on regarding TCL in IOS ( 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 (

voiper_99 Sat, 06/09/2007 - 16:33

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?

Joe Clarke Sat, 06/09/2007 - 17:42

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(tcl)#source flash:auto.tcl


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.

voiper_99 Sat, 06/09/2007 - 20:01

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


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.

Joe Clarke Sat, 06/09/2007 - 20:09

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(tcl)#source flash:automation.tcl


voiper_99 Sat, 06/09/2007 - 20:22

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.

voiper_99 Mon, 06/11/2007 - 19:09

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?

Joe Clarke Mon, 06/11/2007 - 19:23

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

voiper_99 Tue, 06/12/2007 - 01:04

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.

Joe Clarke Tue, 06/12/2007 - 04:55

See my earlier snmp-context.tcl on how to use catch effectively. The catch proc allows you to intercept an error before it terminates your script's execution.

To extract a certain element from a string, you will need to use regular expressions. There are examples in my snmp-context.tcl script as well as in my other post to you on your other TCL scripting thread.

voiper_99 Tue, 06/12/2007 - 17:05

I thought regular expressions would be the answer, however, I am not sure how to put the output of a show command into a variable. I am having trouble using your script as an example because it is very in depth. I looked at all cases where you have used "regex" but none of it really makes sense to me.

In regards to my other TCL script, I cannot find the URL, are you able to paste it so I can look at it? (I am somewhat new to these forums).

Thanks a lot for your help.

Joe Clarke Tue, 06/12/2007 - 17:06

The testprog.tcl script I redid for you includes three very simple examples of storing the output of a show command into a variable. The previous Forum thread to which I referred is . I included a simple regexp example based on your initial script.


This Discussion