Bulk enable PIM via TCL

Reading Time: 3 minutes

I’ve been working on doing some multicast labs lately and am constantly resetting my lab devices to their default configs and starting from scratch. As many of us know, to enable PIM on all of your interfaces you must go into each interface and enable it manually. There is no default command to enable PIM on all interfaces. We know PIM should be enabled 1 to 1 with interfaces involved in routing making this a boon. With that in mind, and the fact that I am rather comfortable with the concept of needing PIM on the interfaces, and likely speak and type this command in my sleep, I decided to make it easier and modify a previous TCL script I had written to enable PIM on every interface that has an IP address assigned to it. With the great “Send to Chat” feature of SecureCRT I can do this across my entire topology on one fell swoop. In a real world environment, you could use a tool like Solarwinds to push this out to your devices.

The script is outlined below. I will start by going over the variables you will want to change based on your environment and follow with that the script itself it actually doing.

tclsh
set output [exec "show ip int bri | e unas"]
set fd [open "nvram:/interfaces.txt" "w"]
puts $fd $output
close $fd
set fd [open "nvram:/interfaces.txt" r]
while { [gets $fd data] >=0 } {
set interface [string range $data 0 15]
ios_config "interface $interface" "ip pim sparse-mode" }
close $fd
puts [exec "del nvram:/interfaces.txt"]
typeahead "\r"
typeahead "\r"
tclquit
  1. The first variable is theĀ  file path and/or name (lines: 3,6,11)
    1. Change the path to a location available on your router/switch. In my case the version I was using left me only with nvram: In many production systems it may be flash:/
    2. The file name is arbitrary but must be unique to the file path
  2. The second variable is the version of PIM you want to run (line 9)
    1. In my case I was issuing the ip pim sparse-mode command

As far as how the script works: When I was using this script in its previous form for a different task I found an oddity that seemed to work better when I closed and reopened the file. With that in mind, here is the synopsis.

The script starts out by running an executive command which is in this case show ip interface brief | exclude unassigned. This filters to only the interfaces with an IP address assigned and is placed in a variable. Another variable is created containing the text file name and path. At this point, the output of the command is placed into the text file via the two variables and the text file is closed.

We now reopen the file and place the contents into a loop. For reach new line in the file I grab the first 16 characters (indicated by 0 – 15 on line 8) and put those characters into a variable called interface. Now the command to enter interface config mode is issued with the variable $interface defining the interface Module #/#. Under that mode the PIM command is entered. Rinse and repeat for reach line in the file until the loop is complete. At this point I close the file and delete it as it is no longer need.

This specific instance is built of Ethernet#/#.### character length. If your environment is using FastEtherent, Serial, GigabitEthernet, longer sub interfaces, or long #/#/# module you may need to increase the number of characters you grab. Again, this is on line 8 and indicated by 0 (the starting character) and 15 (the last character). This is based on the output of the show ip interface brief command.

 

Share this article:

Permanent link to this article: https://www.packetpilot.com/bulk-enable-pim-via-tcl/