Help with TCL scripts to do show commands

Unanswered Question
Jun 2nd, 2011
User Badges:

I need to log into about 60 routers, run the same show commands on each router, and copy each router's outputs as a file on my workstation. Can someone provide a sample TCP script to do those things? I'm not good at programming.

Greatly appreciated.

  • 1
  • 2
  • 3
  • 4
  • 5
Overall Rating: 5 (3 ratings)
Joe Clarke Thu, 06/02/2011 - 07:46
User Badges:
  • Cisco Employee,
  • Hall of Fame,

    Founding Member

Here's a simple example using TFTP.  Save this to cmd.tcl.

set output [exec "show ip int brief"]

set fd [open "flash:/ip_int.txt" "w"]

puts $fd $output

close $fd

ios_config "file prompt quiet" "end"

copy flash:/ip_int.txt tftp://

ios_config "no file prompt quiet" "end"

file delete -force "flash:/ip_int.txt"

This is executed with the command:

tclsh flash:/cmd.tcl

Joe Clarke Thu, 06/02/2011 - 07:47
User Badges:
  • Cisco Employee,
  • Hall of Fame,

    Founding Member

I should say that this is a Tcl script on IOS.  I assumed that's what you meant since you said Tcl.  However, if you're looking to automate the collection from the central workstation, you really need an Expect script on that workstation.  I wasn't clear exactly what you were looking for.

gwhuang5398 Thu, 06/02/2011 - 08:00
User Badges:

Thanks a lot Joseph. I was referring to TCL. Didn't know anything about Expect script. What I'm trying to do is to simplify my manual work to collect show outputs from about 60 routers. I need to store each router's outputs as an individual file on my computer. The show commands are mostly show version, show ip int brief, show ip protocol, show ip bgp summary, etc.

When using TCL, if to write output of multiple show commands to the same file on router flash, is it keyword "append" to do it?

To use TCL, I need to log into routers first, can that step be automated too? I only have putty, not Secure CRT.

Thanks a lot


scottdware Thu, 06/02/2011 - 12:16
User Badges:

Expect will make your life a lot easier in cases like this. Expect utilizes the TCL language, and all of its commands (as well as some of its own). So what you know in TCL, you essentially can port over to Expect scripts.

Here's some documentation on Expect (Windows platform version, but its mostly the same on *nix platforms as well):

And here is more info on it that pertains mostly to *nix versions:

You said you are using PuTTY, so I'm assuming that you're running Windows? If so, if you don't already have TCL, download it from: and select the 8.5.9 version (or, 8.6 beta if you want bleeding edge).

Expect doesn't come bundled with TCL anymore, so you have to manually install it. This can be done by running:

teacup install Expect

from the command prompt once you have installed TCL.

I've attached a couple of expect (TCL) scripts outlining what you want to do. One is for running on Windows, and the other for Linux/Unix plaforms. Check them out, and if you have any questions, give a shout!

Hope this helps!

spamspambaconspam Thu, 06/02/2011 - 17:54
User Badges:


I'm interning with a large company which has me doing a bunch of show commands at level 1 access (so no configs )

I'm on an XP Pro workstation.

I'm using PuTTY to telnet in (And I'm noting that I can telnet in, so we can fix that...)

Then I do (usually):

sh ver

sh cdp ne

sh cdp ne detail

sh module

sh ip int b

And as a last hurrah, I do an "en" just to make sure enable mode is password protected...

I'm copying the output to a .txt file, named with the device name, saved right on my desktop.

I'm using the info to make Visio diagrams.

I have no idea how many devices there are...but I'm guessing there are at least 400+ Cisco ...

There are routers and switches and ASAs, oh my!

(Okay...that was supposed to be funny... )

Anyways...I've never heard of TCL, nor Expect.

So I know what I will be digging into, this weekend... hehehhe...

Anyway, thanks for your awesome reply.

It was "plain language" enough to be understood by someone unfamiliar with the topic (ie: me!)

It gave explicit steps to take without "withholding important facts"...

It was not condescending and in fact, your tone cheerfully made me believe I may actually be able to pull off making some scriptie thingie to pull IOS info while I'm eating bon-bons, watching Oprah...


Kidding about the Oprah part.

And the bon-bons.

Wish me luck!!


scottdware Thu, 06/02/2011 - 18:17
User Badges:

I'm glad that I was able to provide some help...that's what I was aiming for.

So, since you don't have enable access, or access to any configs, that would kind of change the response that the scritpts I provided were "expecting" (pun intended)

What Joseph said is spot-on as well. Using EEM (Embedded Event Manager) makes it easy to kind of "fire and forget" in some cases when it comes to repetitive tasks similar to what you're doing. Here's a link to some documentation on EEM, and there's a really good book that came out a few months ago as well:

TCL is a very nice language/toolset to learn in this field. It has great tie-in's with IOS and can really save some time. Good luck on your internship, and sorry that Oprah is off the air now

If you need any help or have any other questions about TCL or Expect, feel free to drop me a line at:

scottdware at gmail dot com


spamspambaconspam Fri, 06/03/2011 - 10:02
User Badges:

OMG.  Oprah is off the air?

Do they still manufacture bon-bons?

I need to turn off my routers and get out more.


You are my hero.

You have earned your cape.

And since I'm a n00b, you can plan on hearing from me...

like 42 hundred and three times


Joe Clarke Thu, 06/02/2011 - 17:25
User Badges:
  • Cisco Employee,
  • Hall of Fame,

    Founding Member

Scott's suggestions are good.  Expect is a good way for you to automate the data collection for all 60 devices from one central location.  There is another alternative, though.  If you want to schedule the data collection to happen automatically you can deploy Embedded Event Manager policies on your devices, then they can periodically export the data to your workstation without any further interaction from you.  Is this something you want to pursue?

gwhuang5398 Thu, 06/02/2011 - 21:18
User Badges:

Thanks so much Scott and Joseph. The example TCL file is really helpful to me. I'm running Windows XP. With Active TCL and Expect, I should be able to run the script from my PC and have all the log files generated on my PC automatically without having to manually log into each router. I'm not doing this periodically, so may not get into Embedded Event Manager.

A few more questions after reading Scott's cmd-win32.tcl file:

1) If I store the TCL file in the folder C:\Network Management\Router Configs\June 2011, is this how to run the file from Windows command prompt?

C:\Network Management\Router Configs\June 2011\cmd-win32.tcl

2) If the hosts.txt file is in the same folder, should I change the command in the file as following?

set device_list [read [open "C:\Network Management\Router Configs\June 2011\hosts.txt"]]

3) Should I download plink.exe in addition to putty.exe? Does it matter where I save the plink.exe file?

4) How do I change command "exp_spawn plink.exe -l $user -pw $pass $device" to initiate telnet session?

5) I also need to get into router Enable mode to show running-config, how do I add "enable" and enable password after telnet/SSH login on each router?

I'll try the script next Monday. If I run into problems, will give you a shout.

Thanks again


scottdware Fri, 06/03/2011 - 03:38
User Badges:

Hi, Gary!

Here are the answers to your questions:

1) Yes. However, you will most likely need to start with "tclsh" on the command line (which invokes the TCL interpreter), followed by the path to your script:

     tclsh "C:\Network Management\Router Configs\June 2011\cmd-win32.tcl"

2) By default, it will look to see if the hosts.txt file is in the same directory as the script. So if it is, you won't need to change it in the script. Its good practice to though, and if you are going to add the full path, I'd do it like such:

     set device_list [read [open [file join C:/ NetworkManagement "Router Configs" "June 2011" cmd-win32.tcl]]]

     or, to split it up and make it easier to read:

    set device_file [file join C:/ NetworkManagement "Router Configs" "June 2011" cmd-win32.tcl]

  set device_list [read [open $device_file]]

3) You can place plink.exe in the same directory as PuTTY, and make sure that you add the location to your %PATH% environment variable. If you installed PuTTY using the PuTTY installer, then plink.exe is included.

4) For using Telnet, you just want to add a "-telnet" flag, like so:

     exp_spawn plink.exe -telnet $device

5) For this question, I've attached another file as an example. Same one as before, just with the modifications from your questions.

Hope this helps

P.S. If you have more questions related to TCL or Expect in general, feel free to drop me a line at:

scottdware at gmail dot com

That way, we don't get too off-topic here on the forums

gwhuang5398 Sun, 06/05/2011 - 21:17
User Badges:

Thanks again Scott. I'll try the script tomorrow (Monday). May email you if I run into problems.

You mentioned %PATH% environment variable for defining plink.exe location. Where do I set the %PATH" variable?

I downloaded a seperate plink.exe and saved it to the same directory as the putty.exe I'm using. I didn't have a Putty installer when it was installed.

Thanks again. Will talk to you later.

Gary ([email protected])

scottdware Mon, 06/06/2011 - 04:52
User Badges:

You can set it by right-clicking on "My Computer" and under "Advanced," there's a button called "Environment Variables." That is where you set it.

But if putty.exe is already in your path, and you placed plink.exe in the same directory, then you might not need to do more.

spamspambaconspam Fri, 07/22/2011 - 13:40
User Badges:



What am I doing wrong?

I made some very basic changes, but after that, the rest of this is all Martian to me...

I included the script you supplied originally, with my changes, so you can see...

package require Expect

# Here, we specify all our commands in a list, that we will issue one

# by one at a later time.

set commands {

"show version"

"show ip int brief"


# This variable is for a file called hosts.txt that has the hostname/IP

# of all of the routers you are collecting information from.

set device_list [read [open "hosts.txt"]]

# Specify the username and password, as well as what we expect the routers'

# prompt to be.

set user "redacted"

set pass "redacted"

set prompt ">"

# This command tells expect not to echo the output to the console.

exp_log_user 0

# We loop through each device in our list, one by one...

foreach device $device_list {

# Set each device's log file to be the name of the device...

# (i.e.

set file_name "$device-log.txt"

# Assuming you are using PuTTY, and have plink.exe, we initiate the SSH

# connection

exp_spawn plink.exe -telnet $device

# If we see a message asking about the device's host key, accept it.

expect -re ".*ogin:" {

exp_send "$pass\r"


# We log our output from each router to its specified file.

exp_log_file -a $file_name

# Loop through each command that we specified earlier.

foreach cmd $commands {

expect -re $prompt {

exp_send "$cmd\r"

exp_sleep 1



# Now we enter enable mode for the running-config

expect -re $prompt {

send "en\r"


expect -re ".*assword:" {

send "$enable_pass\r"


expect -re $prompt {

send "term len 0\r"


expect -re $prompt {

send "show running-config\r"


expect -re $prompt {

exp_send "quit\r"


# Turn off logging.



Joe Clarke Fri, 07/22/2011 - 16:13
User Badges:
  • Cisco Employee,
  • Hall of Fame,

    Founding Member

It looks like you're using telnet now instead of SSH.  With telnet, you're not going to get the "login:" prompt that you get with SSH.  Instead, you'll get either a Username: or Password: prompt.  Assuming you're not using AAA, try changing this:

# If we see a message asking about the device's host key, accept it.

expect -re ".*ogin:" {

exp_send "$pass\r"



# If we see a message asking about the device's host key, accept it.

expect -re ".*assword:" {

exp_send "$pass\r"


spamspambaconspam Fri, 07/22/2011 - 20:32
User Badges:

Rut ro  

I AM using AAA.

I'm assuming I should change this:

expect -re ".*assword:" {<br/>exp_send "$pass\r"<br/>}<br/>

to this:

expect -re ".*sername:" {<br/>exp_send "$user\r"<br/>}

expect -re ".*assword:" {<br/>exp_send "$pass\r"<br/>}<br/>

How far off am I?

PS - TY for your help!  The engineers at work tell me I don't need to know Tcl [except to take the CCIE lab (bwhahahaha!)] but I've never learned scripting/programming/coding at all... and it's always intrigued me...

I always said I'd marry a man JUST because he knows how to write scripts


Joe Clarke Sat, 07/23/2011 - 13:46
User Badges:
  • Cisco Employee,
  • Hall of Fame,

    Founding Member

Assuming you have a $user variable with the correct username, then yes your proposal should work.  That said, things will fail when you move to enable mode unless you set your $prompt variable to:


That is:

set prompt "\[>#]"

spamspambaconspam Sat, 07/23/2011 - 14:06
User Badges:

Enable mode?



No, I'm the intern.

I have level 1 "n00b" access.

I had level 15 access the second day, for a special project. (I wrote a VACL!  Wooot!)

It scared the CRAP out of me.

I did a show mac-address yada yada and it just kept going and going and going...

All those poor employees! 

...they had *NO IDEA* how close they were to having their desktops instanteously combust

And Cisco is going to be supplying POWER to the cube, now?

With people like *ME* responsible for getting it there?


TY for the heads up.

I'm gonna try it tomorrow...

(crosses fingers...)


spamspambaconspam Tue, 06/14/2011 - 18:07
User Badges:


Being a temporary intern, I have been busy doing this n' that....

...but I've been looking forward to playing around with Tcl...

That is....


...I opened your

I read it.

I read it again.

I'm not sure, but I think I need medical attention.

I mean...that literally HURT my brain...

I never learned any programming languages...

The last time I tinkered with anything was (wait for it...) about 5 or 10 lines of BASIC (which was stored on a cassette tape hooked to a Trash-80...)

I CAN do HTML and played with CSS, which Tcl kinda reminds me of, but man oh man.... am I gonna need a stiff bottle of scotch and a computer I don't mind mistakenly formatting c:\ after 1 shot of scotch (I'm small...)

(I did "copy bs: flash:" very late one night [at 1:30am]... ask me how THAT went...)

Anyhoo...I'm gonna modify your script as per Gary's questions (b/c I literally am running the same environment, 'cept his DIR path ain't the same ) and see how it goes...

If I need you to help me XMODEM, we know things didn't go so goog....



This Discussion