Saturday, October 5, 2013

Junos commit script

Someone asked if there was a Juniper commit script that could check if a user configures an interface that uses unit 1000. If the interface does not appear in a VRF named VRF1000 it then will then make sure it is configured in there. I didn't write most of the script, but I did fix the original that had some errors. Here's the result.

Interface ge-0/0/0 was already configured and placed in the VRF, but not ge-0/0/10

user@router# show interfaces 
ge-0/0/0 {
    vlan-tagging;
    unit 1000 {
        vlan-id 1000;
        family inet {
            address 100.1.1.1/24;
        }
    }
}
ge-0/0/10 {
    vlan-tagging;
    unit 1000 {
        vlan-id 1000;
        family inet {
            address 200.1.1.1/24;
        }
    }
}
lo0 {
    unit 0 {
        family inet {
            address 1.1.1.1/32;
        }
    }
}


{master:0}[edit]
user@router# show routing-instances VRF1000 
instance-type vrf;
interface ge-0/0/0.1000;
route-distinguisher 100:100;
vrf-target target:1001:100;
vrf-table-label;

Now if a user commits it, the script will automatically correct config by adding the missing interface

{master:0}[edit]
user@router# commit 
warning: warning message - found int in vrf VRF1000 ge-0/0/0.1000
warning: warning message - adding to vrf VRF1000 missing int ge-0/0/10.1000
configuration check succeeds
commit complete


{master:0}[edit]
user@router# show routing-instances VRF1000 
instance-type vrf;
interface ge-0/0/0.1000;
interface ge-0/0/10.1000;
route-distinguisher 100:100;
vrf-target target:1001:100;
vrf-table-label;

{master:0}[edit]
user@router## show system
scripts {
    commit {
        file vrf.slax;
    }
}


Here's the code:

------------------------
{master:0}[edit]
user@router> file show /var/db/scripts/commit/vrf.slax

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 configuration {
   var $top = .;
   var $interfaces = interfaces/interface;
   var $vrf = "VRF1000";

   for-each ($interfaces/unit[name == 1000]) {
       var $ifname = ../name _ "." _ name;

       if ($top/routing-instances/instance[name == $vrf]/interface[name == $ifname]) {
          <xnm:warning> {
            <message> "warning message - found int in vrf VRF1000 " _ $ifname;
          }
       }
       else {
           <xnm:warning> {
             <message> "warning message - adding to vrf VRF1000 missing int " _ $ifname;
           }
           var $dot = $top/routing-instances/instance[name == $vrf];
           var $content = <interface> {
               <name> $ifname;
           }
      call jcs:emit-change($dot, $content);
       }
   }
}

No comments:

Post a Comment