Cisco Live 2013 and How To Place a TCL Script on Your Cisco Router Without Using a Server

Home > Blogs > Cisco > Cisco Live 2013 and How To Place a TCL Script on Your Cisco Router Without Using a Server

Cisco Live 2013 and How To Place a TCL Script on Your Cisco Router Without Using a Server

Like This Blog 1 Mark Jacob
Added by July 10, 2013

I just got back from Orlando and attending Cisco Live 2013. It was a great week! If you are a networking person, I highly recommend attending this annual event. Lots of informative sessions, meeting some of the engineers who work for Cisco, seeing 20,000 people all head and shoulders into Cisco, booths with companies showing off their latest network monitoring solutions, demonstrations of the latest gear, well…you get the idea. Not to mention an evening spent at Universal Studios with only Cisco people allowed to be there – so the lines for the rides were a little (maybe a lot) shorter. I do want to share something that was mentioned in Orlando, although it was also mentioned prior to that at the Cisco Live 2013 held in London. The news is this: EIGRP is no longer proprietary! Cisco is opening it up, similar to how OSPF is open source. The only feature of EIGRP which will remain proprietary is the stub router capability. Otherwise, any vendor can include it on their networking devices. Wow! What a change!

While I was there I went to the Cisco store and bought a couple of books. The one that is the main topic of this blog is entitled Tcl Scripting for Cisco IOS. It is from Cisco Press and is authored by Ray Blair (CCIE # 7050), Arvind Durai (CCIE # 7016), and John Lautman. You have heard of ACLs. You have heard of VACLs. I even had a brand new student some time back who had not heard ACLs being referred to as ackles (to write how it is actually pronounced). The new student left the room so another student convinced the remaining students to all play along with the idea that there was such a thing as PCLs and that they were, for short, called pickles. (Of course, Cisco does have Port Access Control Lists, but that is not nearly as funny.) They had the new guy going for a while, but they put a stop to it before someone got hurt.

On that note, have you heard of Tcl? I have been toying around with TCL (Tool Command Language) for a little while. I certainly would not call myself a guru, but it is a very interesting aspect of Cisco networking to explore. If you are a programmer or a developer in addition to being a networking person, you will find Tcl a cool way to spend your leisure time (yea right, I’m sure we all have leisure time to burn).

Tcl is an interpreted scripting language which can automate some of the more tedious aspects of being a network admin, once you get the hang of it. My area of interest was piqued early in the book when it was mentioned that the real power in Tcl lies in placing the script on your device and running it from there, as opposed to typing each line via the command line (which is not particularly riveting). You probably copy configs and IOS files back and forth with regularity, using something like TFTP or FTP (or SCP if you are in a secure environment). So the suggestion was to create the file and once it was complete, to copy it to the device – into the flash file system, for example. Once it was there, the script could be launched with the following command: Router#tclsh pressing return here gets you Router(tcl)# as long as Tcl is present on your device. So let’s say you created a tcl script and name it file.tcl and placed it in flash.

To verify:

Router#dir
Directory of flash:/
17 -rw-     140          <no date> file.tcl
16777212 bytes total (16773316 bytes free)

Now that we know the file is resident, we can launch it thus:

Router(tcl)# source file.tcl

Another way to launch it is from privileged exec mode:

Router#tclsh file.tcl

Either way, the script launches and runs to completion. However, I still had to get the file onto the device first. (Side note, you can also run the script on your device from a remote source, like from the tftp server without transferring it to the device first, but that’s not what I came to tell ya.) My brain was hung up on the thought: What if I don’t have an external server and I want to save my script on my device in flash anyway? What a quandary, you say? I said the same thing. So here’s what I did. I will use the simple example provided in the book on page 38, since all we need is some practice script to work with. What this script does is parse the config to see if there is an interface with an IP address in the range 10.0.0.0/24. Here is the script that the authors provided:

set mybuffer [exec “show ip interface brief”]
set foundposition [string first “10.0.0.” $mybuffer]
if {$foundposition > -1} {
puts “We found the 10.0.0.* network!”
}

Of course, you must read the previous 37 pages if you want to understand the syntax. What the authors had shown up to this point was that each of the above lines was entered individually into the tclsh prompt. It is a short script in this example, but it could be longer. Nevertheless, entering a script one line at a time won’t win you any speed races as an admin. This is where the book explains how to copy the script to your device. Back to my crazy scenario. What if I don’t have an external server and I need to run this script more than once? I don’t want to type each line every time I want to run it. So here is the magic. Let me preface my solution with a story. Years ago I was taking a programming exam as part of an interview. It was many pages long (yes, real paper) and there was considerable output that had to be analyzed page by page. Questions were asked along the way to verify you could read the output and decipher what the behind-the-scenes code was doing. The last piece of the exam was to challenge you to write code that would generate the output that had been undergoing analysis. I proceeded to write a program which consisted entirely of PRINT statements. Hey, it generated the output exactly as I was asked to do. (Another side note: I think they revised the exam shortly after I took it to prevent such a solution.) Which brings me to my Tcl solution: I wanted to write code which would generate the code above. Sounds a little strange, doesn’t it? Write code that writes code. In Tcl, one way to output to the screen is by using the command puts. Imagine yourself typing each of these lines:

set file [open flash:file.tcl w]
set lb “{”
set ds “$”
set f “foundposition”
puts $file {set mybuffer [exec “sh ip int bri”]}
puts $file {set foundposition [string first “10.0.0.” $mybuffer]}
puts $file “if {$ds$f > -1} $lb”
puts $file {puts “We found the 10.0.0.* network!”}
puts $file “}”
close $file

If you have never used Tcl, this probably won’t make much sense, but suffice it to say that the code above, when entered in Tclsh mode, will create a file named file.tcl and place it in flash. Then it will build the script, line by line, using the puts statements as shown. The tricky line was the line with the comparison ‘>’. I needed the output from the puts command to look like it does in the above code that the authors provided. But if you populate the line with $foundposition the interpreter will place the contents of that variable there instead of the text $foundposition. I did that by breaking that text into two pieces and representing each piece with its own variable, which was declared at the beginning of my solution. (ds stands for dollar sign.) The other tricky piece was to get the line to end with a left bracket (lb). If I just place a left bracket ‘{‘ there, the interpreter would treat that as a continuation onto the next line, which is not what I want.

When you are finished, the complete script exists in flash and you can run it just as if you had copied it from an external source. It works!! Again, I don’t know how often you can use this little trick, because if you have an external source, you would just use that. But if you are stuck without that and you want to save your script on the device on which you are working, this will do the trick!

Until next time, happy tcling.
Mark Jacob
Cisco Instructor – Interface Technical Training
Phoenix, AZ

Videos You May Like

A Simple Introduction to Cisco CML2

0 3901 0

Mark Jacob, Cisco Instructor, presents an introduction to Cisco Modeling Labs 2.0 or CML2.0, an upgrade to Cisco’s VIRL Personal Edition. Mark demonstrates Terminal Emulator access to console, as well as console access from within the CML2.0 product. Hello, I’m Mark Jacob, a Cisco Instructor and Network Instructor at Interface Technical Training. I’ve been using … Continue reading A Simple Introduction to Cisco CML2

Creating Dynamic DNS in Network Environments

0 645 1

This content is from our CompTIA Network + Video Certification Training Course. Start training today! In this video, CompTIA Network + instructor Rick Trader teaches how to create Dynamic DNS zones in Network Environments. Video Transcription: Now that we’ve installed DNS, we’ve created our DNS zones, the next step is now, how do we produce those … Continue reading Creating Dynamic DNS in Network Environments

Cable Testers and How to Use them in Network Environments

0 731 1

This content is from our CompTIA Network + Video Certification Training Course. Start training today! In this video, CompTIA Network + instructor Rick Trader demonstrates how to use cable testers in network environments. Let’s look at some tools that we can use to test our different cables in our environment. Cable Testers Properly Wired Connectivity … Continue reading Cable Testers and How to Use them in Network Environments

Write a Comment

See what people are saying...

    Share your thoughts...

    Please fill out the comment form below to post a reply.