06-06-2007 04:25 PM
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
06-06-2007 08:52 PM
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.
06-07-2007 08:48 AM
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
06-07-2007 10:11 PM
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?
06-08-2007 04:18 AM
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).
06-08-2007 03:56 PM
Thank you for the suggestion but I am a little worried after reading this person's review on it:
I am not an experienced programmer and think perhaps I may have the same issues as this person. Do you have any other suggestions?
06-08-2007 04:09 PM
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.
06-09-2007 04:33 PM
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?
06-09-2007 05:42 PM
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.
06-09-2007 08:01 PM
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.
06-09-2007 08:09 PM
06-09-2007 08:22 PM
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.
06-11-2007 07:09 PM
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?
06-11-2007 07:23 PM
06-12-2007 01:04 AM
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.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide