Log in

Julien Goodwin
JunOS version comparuson 
18th-Nov-2010 09:59 pm
southpark cartoon

Recently I've been cleaning up my OS image collections of my Cisco and Juniper kit. Doing version comparison in IOS is a right pain in the a@#, but JunOS is fairly simple, albeit beyond the built-in sorting capabilities of most languages. So here's a simple PHP function, suitable for the usort family of functions that sorts by release version.


function compare_junos_versions($v1$v2) {
// Quoth the manual:
    // The comparison function must return an integer less than, equal to,
    // or greater than zero if the first argument is considered to be 
    // respectively less than, equal to, or greater than the second.
$regex '/([1-9]?[0-9])\.([0-9])([RS])([0-9]+)\.([0-9]+)/';


// Major
if ($c1[1] > $c2[1]) return 1;
    if (
$c1[1] < $c2[1]) return -1;

// Minor
if ($c1[2] > $c2[2]) return 1;
    if (
$c1[2] < $c2[2]) return -1;

// Release / Service
if ( ! ($c1[3] == $c2[3])) { // By far the common case is both are release
if ($c1[3] == 'S') return 1;
        if (
$c2[3] == 'S') return -1;

// Revision
if ($c1[4] > $c2[4]) return 1;
    if (
$c1[4] < $c2[4]) return -1;

// Build
if ($c1[5] > $c2[5]) return 1;
    if (
$c1[5] < $c2[5]) return -1;

// And I guess they're equal
return 0;


22nd-Nov-2010 11:48 pm (UTC)
I tried to write that more succinctly, but my php fails me.

Yay! I'm cured!
30th-Nov-2010 01:25 pm (UTC)
I had tried to use the ternery operator instead of the if's but that was just a ball of fail.
30th-Nov-2010 11:39 pm (UTC)
I tried to use the cmp function, but there's no cmp in php.

Then I tried to use zip to write cmp, but there's no zip in php.

Then I had to use a for loop, but hated myself and stopped.
11th-Jan-2011 06:33 am (UTC) - re:
what I was looking for, thanks
This page was loaded Dec 2nd 2015, 6:51 am GMT.