JunOS – How to Alias a Command

juniper-junos

I was searching for an easy way to do this over the last few days and found that there wasn't a SIMPLE way to do this. The majority of the answers were:

  • Too complex for the problem at hand.
  • Difficult to understand without prior knowledge of SLAX.

Best Answer

I was working with a customer who was implementing OAM in JunOS, the show commands to display statistics are unwieldy. They wanted a way not to have to type the full command everytime they needed to retrieve statistics.

Here's what I mean:

show oam ethernet connectifity-fault-management sla-iterator-statistics sla-iterator ITERATOR maintenance-domain MD maintenance-association MA local-mep 100 remote-mep 101

I can see why this would get pretty annoying, so how can we turn that into a much shorter one-liner? JunOS doesn't have the ability to alias a command built into the OS. What we can do, is use a SLAX script. It's a bit more obnoxious at first, but it's kind of nice.

The "script":

I'll use a much more basic command to keep everything short, but you can replace the show command you see in the code with any other show command you would want to use. Feel free to also make the variables a more coherent name for your use case.

version 1.0;
ns junos = "http://xml.juniper.net/junos/*/junos";
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
import "../import/junos.xsl";

match / {
    <op-script-results> {

        var $variable1 = <rpc> {
            <command format="text"> "show interfaces terse";
        }

        var $variable2 = jcs:invoke($variable1);

    <output> $variable2;
    }
}

Running the Script:

  1. Your script (filename) must end in .slax
  2. Your script must be located in /var/db/scripts/op/ on the box. If you are using redundant routing engines, it must be present on both RE's in the same directory mentioned above. Here's how you copy a file from one RE to another:

file copy re0:/var/db/scripts/op/filename.slax re1:/var/db/scripts/op/filename.slax file list re1:/var/db/scripts/op/

  1. Once your file(s) are copied, you must enable them. This prevents anyone who can copy files to the device from executing potentially destructive scripts.

R1# set system scripts op file filename.slax R1# show | compare R1# commit and-quit

  1. If you're using multiple RE's:

R1# commit synchronize and-quit

  1. At the operational mode prompt:

R1> op filename.slax

And you're done, I hope this helps.

Related Topic