push.ksh – A Tool for Large Juniper Networks

04/07/2009

Over the years I have written a few scripts to help me manage the networks I have been responsible for.  None of it is earth-shattering I am sure but I think at least some of these tools could be useful to others as well.  Not all IP/Network Engineers (router jockeys) are well versed in scripting languages and all but the smallest networks can benefit from them.  So without further adieu, here is the first of what I plan to make a series of posts on Network Tools, enjoy!

push.ksh:

I wrote push.ksh to “push” commands out to several Juniper/JUNOS routers at once.  It is a Korn shell script which leverages jlogin (an Expect script distributed with RANCID).  It can basically do one thing, two and a half ways.  It can execute the same command on a list of routers or it can run a tailored command on a list of routers.  I added the half because when running the same command on all routers, the script can pull the data from a pair of files or from the command line.  Push.ksh can be used to do many repetitive tasks across a given group of routers – including configuration changes.  This is a simple script (not unlike most of my scripts) but I have found it invaluable in many situations (again, not unusual – else why share them).

Let’s first check the dependencies, found in the variables section:

#-----------Variables--------------#
#** enter the correct paths here **#
PASTE=<path>/paste_input.pl
#paste_input.pl can be found at http://odin.chrisgrundemann.com/files/scripts/perl/
JLOGIN=<path>/jlogin
#jlogin is an expect script that comes with RANCID (http://www.shrubbery.net/rancid/)
#----------------------------------#

As you can see, this script is dependent on two others.  We touched on jlogin above; if you have RANCID then you probably have jlogin.  Try “> whereis jlogin” to find the full path.  If not, you can grab it from my server here.  The second script referenced is a perl script that I adapted from some other perl scripts that my long time colleague Ryan Privette has written.  You can also grab that one from my server, right here.

Next, we take a look at the help output and basic usage:

> push.ksh --help

Chris wrote this script for personal use.
If it messes something up that is your fault for
using it in the first place.  By using this script
you agree to this.

Usage:
push.ksh -f <file with list of routers> <file with commands for every router>
push.ksh -d <directory where command files are located - named by router host name>
push.ksh -p [you then paste the list of routers and list of commands into the cli]

>

There is the disclaimer to keep co-workers from blaming me with there ill advised attempts and then the usage guide.  As you can see, there are three options which are all hopefully fairly self explanatory.  Let’s look at each in a bit more detail just in case.

 push.ksh -f <file with list of routers> <file with commands for every router>

This option should be utilized when you want to execute the exact same command on every router in a given list. Save your list of host names or IPs to act on (whatever you would use to log into the device) in one file and the list of commands to execute in another.  Push.ksh will go through the list of routers in the first file one by one and run the commands in the second file on each; displaying the entire output so that you can monitor or capture the results.

push.sh -d <directory where command files are located - named by router host name>

This option is used when the commands must be individualized for each router.  Generate your commands and save them in files that are named with the host name (or IP) of the router in question.  Push.ksh will “ls” the directory supplied, use the contained files names as host names to (attempt to) log into and then execute the commands in that file on that router.  Again, all output is displayed.  For example:  If you want to run “show interfaces xe-1/0/0″ on rtr1 and “show interfaces xe-2/1/0″ on rtr2; you would create a fresh directory (mkdir) and create two files inside of it, one called rtr1 (assuming your DNS works) containing “show interface xe-1/0/0″ and another named rtr2 with the line “show interface xe-2/1/0″ as its contents.  Then run “> push.ksh -d <directory you created>” to have the commands executed on the respective routers.

 push.ksh -p [you then paste the list of routers and list of commands into the cli]

This final option should be used when you have a command or set of commands to execute on a list of routers but don’t want to take the time to write files beforehand.  This option works just like the first, -f, option but instead of referencing files, the lists are pasted right onto the cli.

As mentioned, all options display all output to standard out which of course means that you can monitor the action live, write it to a file or use the pipe to run other shell commands (grep is one obvious example) and better parse the data.

I hope this can be of use to you; if it is – please drop me a comment or an email and let me know.  Thanks!

Similar Posts:

Leave a Comment

Previous post:

Next post: