It’s the little things that get ya…the other day I was reviewing some changes to be made by a co-worker for a Juniper switch.  And everything looked fine, except there was one small gotcha.  When disabling an interface, the command used was “disable interfaces xe-0/0/1” and so forth for about 15 interfaces.  So, if you aren’t familiar with JUNOS, you may be asking yourself, so what’s the problem?  Those of you that are JUNOS fans, certainly know that JUNOS is a bit backwards when disabling interfaces and in order to “disable” an interface, you must set the disable attribute on the interface, which means that in order to disable interface xe-0/0/1, the command would look like this:

set interfaces xe-0/0/1 disable

I know…not so intuitive, but that wasn’t the underlying issue at hand.  The problem was that my co-worker had only messed up that one command across all of the 15 or so interfaces he was working with…such that it looked like an expanded version of this:

delete interfaces xe-0/0/1
disable interfaces xe-0/0/1
set interfaces xe-0/0/1 description “Unprovisioned: FREE”
set interfaces xe-0/0/1 apply-groups-except LAG-settings

delete interfaces xe-0/0/2
disable interfaces xe-0/0/2
set interfaces xe-0/0/2 description “Unprovisioned: FREE”
set interfaces xe-0/0/2 apply-groups-except LAG-settings

delete interfaces xe-0/0/3
disable interfaces xe-0/0/3
set interfaces xe-0/0/3 description “Unprovisioned: FREE”
set interfaces xe-0/0/3 apply-groups-except LAG-settings

delete interfaces xe-1/0/1
disable interfaces xe-1/0/1
set interfaces xe-1/0/1 description “Unprovisioned: FREE”
set interfaces xe-1/0/1 apply-groups-except LAG-settings

delete interfaces xe-1/0/2
disable interfaces xe-1/0/2
set interfaces xe-1/0/2 description “Unprovisioned: FREE”
set interfaces xe-1/0/2 apply-groups-except LAG-settings

delete interfaces xe-1/0/3
disable interfaces xe-1/0/3
set interfaces xe-1/0/3 description “Unprovisioned: FREE”
set interfaces xe-1/0/3 apply-groups-except LAG-settings

delete interfaces xe-2/0/1
disable interfaces xe-2/0/1
set interfaces xe-2/0/1 description “Unprovisioned: FREE”
set interfaces xe-2/0/1 apply-groups-except LAG-settings

delete interfaces xe-2/0/2
disable interfaces xe-2/0/2
set interfaces xe-2/0/2 description “Unprovisioned: FREE”
set interfaces xe-2/0/2 apply-groups-except LAG-settings

delete interfaces xe-2/0/3
disable interfaces xe-2/0/3
set interfaces xe-2/0/3 description “Unprovisioned: FREE”
set interfaces xe-2/0/3 apply-groups-except LAG-settings

So…the question was…how can we easily transform those incorrect disable lines to the proper syntax…the answer isn’t as simple as “search and replace”, since you don’t really have anything to “replace” per se…and I cringe at those that would mention excel for a task like this, so I decided to make my own answer to this problem with a clever script.

So I put the above output in a text file (let’s call it pre-change.txt), and came up with a way to change this via a Mac OS X box (but any Linux box should be the same or similar):

cat pre-change.txt |  sed “s/^\(disable\ interfaces\)/set\ interfaces/g ; s/^\(set\ interfaces\ xe-.*\)/\1 disable/g” (Thanks to John’s catch to keep me honest)

cat pre-change.txt |  sed “s/^\(disable interfaces\ xe-.*\)/\1 disable/g; s/^\(disable\ interfaces\)/set\ interfaces/g”

and now I get this:

delete interfaces xe-0/0/1
set interfaces xe-0/0/1 disable
set interfaces xe-0/0/1 description “Unprovisioned: FREE”
set interfaces xe-0/0/1 apply-groups-except LAG-settings

delete interfaces xe-0/0/2
set interfaces xe-0/0/2 disable
set interfaces xe-0/0/2 description “Unprovisioned: FREE”
set interfaces xe-0/0/2 apply-groups-except LAG-settings

delete interfaces xe-0/0/3
set interfaces xe-0/0/3 disable
set interfaces xe-0/0/3 description “Unprovisioned: FREE”
set interfaces xe-0/0/3 apply-groups-except LAG-settings

delete interfaces xe-1/0/1
set interfaces xe-1/0/1 disable
set interfaces xe-1/0/1 description “Unprovisioned: FREE”
set interfaces xe-1/0/1 apply-groups-except LAG-settings

delete interfaces xe-1/0/2
set interfaces xe-1/0/2 disable
set interfaces xe-1/0/2 description “Unprovisioned: FREE”
set interfaces xe-1/0/2 apply-groups-except LAG-settings

delete interfaces xe-1/0/3
set interfaces xe-1/0/3 disable
set interfaces xe-1/0/3 description “Unprovisioned: FREE”
set interfaces xe-1/0/3 apply-groups-except LAG-settings

delete interfaces xe-2/0/1
set interfaces xe-2/0/1 disable
set interfaces xe-2/0/1 description “Unprovisioned: FREE”
set interfaces xe-2/0/1 apply-groups-except LAG-settings

delete interfaces xe-2/0/2
set interfaces xe-2/0/2 disable
set interfaces xe-2/0/2 description “Unprovisioned: FREE”
set interfaces xe-2/0/2 apply-groups-except LAG-settings

delete interfaces xe-2/0/3
set interfaces xe-2/0/3 disable
set interfaces xe-2/0/3 description “Unprovisioned: FREE”
set interfaces xe-2/0/3 apply-groups-except LAG-settings

Pretty cool, eh…So, two things here…get the logic straight of what you are trying to do (replace the ‘disable xe-#/#/#’ with ‘set xe-#/#/# disable’) and know the utilities that can help (sed in this case).  Hope this helps in your time of need.

-=]NSG[=-

3 thoughts on “Correcting bad syntax with da quickness

  1. Deep down though, you know you’re a little bit uncomfortable that every set interface line now has a disable at the end. Nothing /wrong/ with that (syntactically), but my OCD says “waahhhh”.

    Your challenge – make it match ONLY on the line to disable the interface, and on none of the others? You’re welcome 🙂

  2. Dude…I totally missed that. I was editing this from the original syntax to protect the innocent, and I missed that the disable added to the end. The correct syntax should not be:

    cat pre-change.txt | sed “s/^\(disable\ interfaces\)/set\ interfaces/g ; s/^\(set\ interfaces\ xe-.*\)/\1 disable/g”

    but rather switched around like this:

    cat pre-change.txt | sed “s/^\(disable interfaces\ xe-.*\)/\1 disable/g; s/^\(disable\ interfaces\)/set\ interfaces/g”

    so take that! 🙂 Thanks for keeping me honest.

Leave a Reply