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.



<?php

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]+)/';

    
preg_match($regex$v1$c1);
    
preg_match($regex$v2$c2);

    
// 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;
}

?>


Comments 
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:
Anonymous
what I was looking for, thanks
This page was loaded Aug 23rd 2014, 1:22 pm GMT.