Junos
Reply
New User
miguknom
Posts: 2
Registered: ‎09-25-2009
0

set variable conditionally from if {}

I am writing a script to automate the show arp->show ethernet table->show interface sequence that we all do when people say some host is having problems.

 

I am allowing the user to pass in either an IP address or a MAC address when running the script.  I need to refine my results based on that user input.  What I am trying to do is this:

 

------------------------------------------ 

 

var $arguments =

{

 

    <argument>

    {

        <name> "ip";

        <description> "IP address, e.g., [op checkport ip A.B.C.D]";

    }

    <argument>

    {

        <name> "mac";

        <description> "MAC address, e.g., [op checkport mac AA:BB:CC:smileyvery-happy:D:EE:FF]";

    }

}

 

 

 

/* Command-line arguments */

 

param $ip;

param $mac;

 

 

 

match /

  {

  <op-script-results>

  {

 

    /* Get the ARP information for this host */

 

    var $arpQuery = <get-arp-table-information> { <no-resolve>; <expiration-time>;}

    var $arpResult = jcs:invoke( $arpQuery );

    var $arpChosen =

    {

      if ($ip)

      {

        copy-of $arpResult/arp-table-entry[ip-address==$ip];

      }

      else if ($mac)

      {

        copy-of $arpResult/arp-table-entry[mac-address==$mac];

      }

    }

 

    <output> $arpChosen/ip-address;

 

  }

 

-------------------------------------

 

If I just do "<output> $arpChosen" I see the results that I expect--the contents of that ARP table entry.  However, as soon as I try to do "<output> $arpChosen/ip-address" I get nothing. 

 

If I get rid of the if {} statement and just assume that only IP addresses will be used (like this:  var $arp-Chosen = $arpResult/arp-table-entry[ip-address==$ip]:smileywink: then the rest works fine.

 

Any help?

 

Thanks. 

Recognized Expert
ccall
Posts: 230
Registered: ‎06-18-2008
0

Re: set variable conditionally from if {}

[ Edited ]

When you assign a variable value conditionally like this:

 

var $arpChosen =

    {

      if ($ip)

      {

        copy-of $arpResult/arp-table-entry[ip-address==$ip];

      }

      else if ($mac)

      {

        copy-of $arpResult/arp-table-entry[mac-address==$mac];

      }

    }

 

It will be a result tree fragment data type.  This isn't a problem if what you want is a number or a string, but if you want to retrieve the information from the XML structure using location paths then you need to convert it to a node-set first.

 

If you are using JUNOS 9.2 or later then you can use the := operator in place of the = to cause  $arpChosen to be a node-set rather then a result tree fragment, if earlier you'll have to use the node-set() function instead.  You can then do location paths to extract the information that you want.  But, keep in mind that the resultant XML structure might be a little different than you expect.  The $arpChosen variable will be a node-set with a single node - the root node of your XML structure.

 

For example, I'm guessing that the correct location path you'd want would be:

$arpChosen/arp-table-entry/ip-address

 

For more details, you should find the "Applying JUNOS Automation" Day One Guide useful.  The PDF can be downloaded for free here:

Day One: Applying JUNOS Automation

 

 

 

 

Message Edited by ccall on 09-25-2009 10:07 PM
New User
miguknom
Posts: 2
Registered: ‎09-25-2009
0

Re: set variable conditionally from if {}

Perfect.  Thanks.  the := did the trick.  And yes, I did mean to do $arpChosen//ip-address.  Good catch.

 

-Matt 

New User
JRHelgeson
Posts: 1
Registered: ‎09-28-2009
0

Re: set variable conditionally from if {}

Hey, I recognize that UserID/name... How are ya doin, Matt?

Its been a while...

 

Joel Helgeson

Copyright© 1999-2013 Juniper Networks, Inc. All rights reserved.