Automation

last person joined: yesterday 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  Compare variable to strings

    Posted 05-13-2013 08:43

    I'm trying to determine which interface to use for a multiple platform script, but running into an issue.

     

    Regardless of the $device-model variable, it's matching the first line.

     

    From juise:

    print $device-model
    [string] "SRX210HE-POE"

     

    From SLAX:

    var $adr-interface = {
    		if($device-model == "SRX100B" || "SRX100b"){
    				expr "fe-0/0/0.0";
    			}
    		else if($device-model == "SRX100H" || "SRX100h" ){
    				expr "fe-0/0/0.0";
    			}
    		else if($device-model == "SRX210H" || "SRX210h" || "SRX210HE-POE"){
    				expr "ge-0/0/0.0";
    			}
    		else if ($device-model == "SRX240H" || "SRX240h"){
    				expr "ge-0/0/0.0";
    			}
    		else {
    			expr jcs:syslog( "external.notice", $syslog-tag, "This is only designed for SRX100, SRX210, and SRX240 Devices.  \nAttempting on ", $device-model);
    			<xsl:message terminate="yes">"This is only designed for SRX100, SRX210, and SRX240 Devices";
    		}
    	}

     Any ideas?



  • 2.  RE: Compare variable to strings
    Best Answer

    Posted 05-13-2013 09:00

    You need to specify the full boolean expression on both sides of the OR. A non-empty string always evaluates to true, so right now you are basically saying:

     

    if( $something == "something" || true() )

     

    Instead, say:

     

    if( $something == "something" || $something == "something else" )

     

     



  • 3.  RE: Compare variable to strings

    Posted 05-13-2013 11:03

    As an aside, you can side-step the need to do multiple checks (for character case) by simply normalizing your variable to  an all lower- or uppper-case copy prior to any string comparisons.

    E.g.

     

    /* normalize $dev-model string to all lower-case in $dev-model_lc */
    var $dev-model_lc = translate($device-model,  "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz" );


    var $adr-interface = { if($device-model_lc == "srx100b") { expr "fe-0/0/0.0"; } ....
    etc.




  • 4.  RE: Compare variable to strings

    Posted 05-25-2013 08:27

    Good idea, I actually ended up doing that.  I found that different code versions report device model description with caps / nocaps.