cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
26419
Views
15
Helpful
18
Replies

Help with TCL scripts to do show commands

gwhuang5398
Level 2
Level 2

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.

18 Replies 18

Joe Clarke
Cisco Employee
Cisco Employee

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://10.1.1.1/ip_int.txt

ios_config "no file prompt quiet" "end"

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

This is executed with the command:

tclsh flash:/cmd.tcl

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.

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

Gary

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

http://docs.activestate.com/activetcl/8.5/expect4win/

And here is more info on it that pertains mostly to *nix versions: http://expect.sourceforge.net/

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: http://www.activestate.com/activetcl/downloads 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!

Scott...

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

Okay.

Kidding about the Oprah part.

And the bon-bons.

Wish me luck!!

~Kathy

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:

http://www.cisco.com/en/US/customer/products/ps6815/products_ios_protocol_group_home.html

http://www.cisco.com/en/US/customer/docs/ios/netmgmt/configuration/guide/nm_eem_policy_cli.html

http://www.cisco.com/en/US/customer/docs/ios/netmgmt/configuration/guide/nm_eem_policy_tcl.html

http://www.amazon.com/TcL-Scripting-Cisco-Networking-Technology/dp/1587059452/ref=sr_1_6?s=books&ie=UTF8&qid=1307063587&sr=1-6

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

Later!

OMG.  Oprah is off the air?

Do they still manufacture bon-bons?

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

Dude.

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

~Kathy

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?

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

Gary

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

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 (gary5398@yahoo.com)

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.

Scott....

Halp?

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. router1.location.com-log.txt)

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.

exp_log_file

}

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"

}

to:

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

expect -re ".*assword:" {

exp_send "$pass\r"

}

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community:

Innovations in Cisco Full Stack Observability - A new webinar from Cisco