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

Agile Methodology in Project Management

0 153 0

In this video, you will gain an understanding of Agile and Scrum Master Certification terminologies and concepts to help you make better decisions in your Project Management capabilities. Whether you’re a developer looking to obtain an Agile or Scrum Master Certification, or you’re a Project Manager/Product Owner who is attempting to get your product or … Continue reading Agile Methodology in Project Management

Creating Users and Managing Passwords in Microsoft Office 365

0 642 3

In this Office 365 training video, instructor Spike Xavier demonstrates how to create users and manage passwords in Office 365.   For instructor-led Office 365 training classes, see our course schedulle: Spike Xavier SharePoint Instructor – Interface Technical Training Phoenix, AZ 20347: Enabling and Managing Office 365    

Detailed Forensic Investigation of Malware Infections – April 21, 2015

4 608 5

How does an investigator hunt down and identify unknown malware? In this recording of our IT Security training webinar on April 21, 2015, Security expert Mike Danseglio (CISSP / CEH) performed several malware investigations on infected computers and identify symptoms, find root cause, and follow the leads to determine what’s happening. He demonstrated his preferred … Continue reading Detailed Forensic Investigation of Malware Infections – April 21, 2015

Write a Comment

See what people are saying...

  1. Avatar Aravinth

    cisco router only we enable tcl ????
    we apply tcl for all cisco series routers???

Share your thoughts...

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