Automation

last person joined: 4 days ago 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  Template for verifying IP cli input

    Posted 02-06-2015 11:59

    Hi 

     

    I have a SLAX op script, which is taking user input IP as an argument. I am trying to add the follwoing template to it:

    If argument was not specified, request one. If IP format is incorrect terminate script execution. I have done the below, the first works ok. I am trying to verify IP by using jcs: parse-ip function, but it does not interrupt the script.
    I also struggle to convert the code below to a template.

     

     

    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";

     

     

    /* Request IP input */

    param $ip ;

     

    var $arguments = {

        <argument> {

          <name> "ip" ;

          <description> "Enter IP <ip-address> " ;

        }

    }

     

     

    match / {

          <op-script-results> {

     

       

            var $ip_address = { if(jcs:empty($ip)) {

               expr jcs:get-input("Enter IP address: ") ;

              }

               else {

                 expr $ip;

              }

            }

            var $test_ip = jcs: parse-ip($ip_address) ;

            if ($test_ip/xnm:error) {

                <xsl:message terminate="yes"> "Incorrect IP";

            }

            

    /*Script....*/

     

         <output> "Script complete"  ;

        }

    }



  • 2.  RE: Template for verifying IP cli input
    Best Answer

     
    Posted 02-08-2015 05:47

    Umm... it does seem somewhat tricky to parse xnm:error in relation to this...not sure why.

     

    As an alternative you could try the following approach.   Since parse-ip() will return a number of parse-ip nodes back based on it's success, you could assume that if  $test_ip[1] is empty then the address supplied is invalid.

    if (jcs:empty($test_ip[1])) {
        <xsl:message terminate="yes"> "Incorrect IP";
    }

     

     Something along the lines of:

     

    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";
    
    /* Request IP input */
    
    param $myIp;
    
    var $arguments = {
        <argument> {
            <name> "myIp";
            <description> "Enter IP <ip-address>";
        }
    }
    
    match / {
        <op-script-results> {
            <output> {
                call test_ip($ipAddr = $myIp);
                expr "Script complete\n";
            }
        }
    }
    
    template test_ip ($ipAddr) {
        var $ip_address = {
            if (jcs:empty($ipAddr)) {
                expr jcs:get-input("Enter IP address: ");
            } else {
                expr $ipAddr;
            }
        }
        var $test_ip = jcs:parse-ip($ip_address);
        if (jcs:empty($test_ip[1])) {
            <xsl:message terminate="yes"> "Incorrect IP";
        } else {
            expr "Results...\n";
            expr "host address: " _ $test_ip[1] _ "\n";
            expr "address family: " _ $test_ip[2] _ "\n";
            if ($test_ip[3]) {
                expr "prefix length: " _ $test_ip[3] _ "\n";
            }
            if ($test_ip[4]) {
                expr "network address: " _ $test_ip[4] _ "\n";
            }
            if ($test_ip[5]) {
                expr "ipv4 network mask: " _ $test_ip[5] _ "\n";
            }
        }
    }

    Although, as far as I can tell parse-ip() already returns an error message if an invalid ip address is supplied.

     

    However by the same token, if you supply juset a value of say "192", then parse-ip() assumes that this is the address 192.0.0.0 and doesn't consider the address to be invalid.

     

    I hope that this helps in someway.


    Regards,



  • 3.  RE: Template for verifying IP cli input

    Posted 02-08-2015 09:10

    Thank you so much for your response. It works quite well. The message is not that importaint, but

    the main thing is that it interrupts script exectution in case of no ip entered. I can live with 192 being treated as 192.0.0.0 IP. I guess a better way to do is to match the input against regular expression. 

     

    This template will be verify usefull in the scripts I am working on. I modified your version to the below, so after checking input, the template returns the IP, which can be used further in the script body:

     

     

    param $myIp;
    
    var $arguments = {
        <argument> {
            <name> "myIp";
            <description> "Enter IP <ip-address>";
        }
    }
    
    match / {
        <op-script-results> {
            <output> {
                var $ipResult := {call test_ip($ipAddr = $myIp);}
                expr "IP Entered: " _$ipResult ;
                expr "Script complete\n";
    
            }
        }
    }
    
    template test_ip ($ipAddr) 
        var $ip_address = {
            if (jcs:empty($ipAddr)) {
                expr jcs:get-input("Enter IP address: ");
            } else {
                expr $ipAddr;
            }
        }
        var $test_ip = jcs:parse-ip($ip_address);
        if (jcs:empty($test_ip[1])) {
            <xsl:message terminate="yes"> "Incorrect IP";
        } else {
            expr $test_ip[1];
            ;
    
        }
    }
    

     



  • 4.  RE: Template for verifying IP cli input

     
    Posted 02-08-2015 09:24
    Yes. Using a regex would make a lot of sense in the validation process.

    If you're running with 12.2 or above then you could also make use of a while() statement. Which could be used to really force the user to enter a valid ip address and prevent them from going any further.

    But the while() statement isn't supported in earlier versions of junos iirc.

    Regards,