Sunday, October 13, 2013

Juniper Op script to convert Hex to Decimal


Created an op script that would take a string of hex pairs and convert them into decimal. Actually I took some xslt script I found on the internet and made the conversion to slax.

user@router> op hex 
07 d9 08 1c 04 32 13 00 2d 07 00
7
217
8
28
4
50
19
0
45
7
0

Could be useful in other scripts if you need to make a conversion.

Code:
---------------

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 $string = 07 d9 08 1c 04 32 13 00 2d 07 00;
     var $hexval = translate($string,' ','');
     <output> $string;
     var $len = string-length($hexval);
     var $loopCounter := { call create-loop-counter( $counter = 11); }
     for-each( $loopCounter/counter ) {
       call hexpairs-to-dec($pair = position(), $hexval);
     }

    }
   
}
template create-loop-counter( $counter ) {
if( $counter > 0 ) {
<counter>;
call create-loop-counter( $counter = $counter -1 );
}
}

template hexpairs-to-dec ($pair = 1, $hexval) {
    var $hexpair = substring($hexval, $pair * 2 - 1, 2);
   
    if ($hexpair) {
        var $hex = 0123456789abcdef;

        <output> (string-length(substring-before($hex, substring($hexpair, 1, 1)))) * 16 + string-length(substring-before($hex, subs
tring($hexpair, 2, 1)));
    }
}

No comments:

Post a Comment