• All Courses

      Categories

      • Cisco Certifications
      • Juniper Certifications

      Recommended Courses

      Cisco CCDA – 200-310 – Network Design Complete Course
      Juniper JNCIA-Junos – NEW JN0-104 Complete Course
      Juniper JNCIA-DevOps – JN0-222 – Network Automation
      Cisco CCNA – 200-301 Complete Course
  • More
    • Become a Teacher
    • About Us
    • FAQs
  • Blog
  • Contact
  • Have any question?
  • (+1) 410 635 0231
  • help@ciscolessons.com
RegisterLogin

Login with your site account

Lost your password?

Not a member yet? Register now

Register a new account

Are you a member? Login now

CiscoLessons
  • All Courses

      Categories

      • Cisco Certifications
      • Juniper Certifications

      Recommended Courses

      Juniper JNCIA-DevOps – JN0-222 – Network Automation
      Juniper JNCIA-Junos – NEW JN0-104 Complete Course
      Cisco CCNA – 200-301 Complete Course
      Cisco CCDA – 200-310 – Network Design Complete Course
  • More
    • Become a Teacher
    • About Us
    • FAQs
  • Blog
  • Contact

    JNCIA-DevOps

    • Home
    • Blog
    • JNCIA-DevOps
    • Languages Supported by the Junos XML API

    Languages Supported by the Junos XML API

    • Posted by Ben Jacobson
    • Categories JNCIA-DevOps, Lessons, XML and NETCONF
    • Date May 14, 2022
    • Comments 0 comment

    In this lesson we’re going to be talking about the supported XML API languages.

    XSLT

    First up, XSLT.

    XSLT stands for eXtensible Stylesheet Language Transformations. This language is used exclusively for XML to XML transformations. It is a standards-based language, designed by the World Wide Web Consortium. It is used for on-box scripting, so scripts that you write which are stored physically on your device and then executed against your configuration.

    Conceptual flow of XSLT

    XSLT is used for converting one XML document to another. Think of it this way, your device uses XML in the back end, so you’re using XSLT to take that XML and do some transformations to it. Either change it, add to it, check for the existence of something, remove elements from it.

    XSLT has many of the functions of a standard programming language. It gives us if-then type functions, we have the ability to run loops so we can iterate through something. We can find specific nodes in our XML hierarchy.

    At a high level, the way XSLT works is that we have some XML input document. Usually this will be your configuration file. Mostly XSLT scripts are used for commit scripts, which are executed on the configuration file at time of commit. Your candidate configuration will be the input document and the XSLT script that you have is run by the XSLT engine. That engine processes the input document and spits out some alternative output document. We see in the above diagram that the output tree is a little different than the input tree. We ran some stylesheet transformations against that input tree.

    Example XSLT script setting R1 as the hostname or throwing an error when no hostname is set

    Let’s take a little look at an example of an XSLT script. In this script, this changes the host name to R1 if the hostname element is found in the system hierarchy, and if no host name is found then the error Missing [edit system hostname] is displayed. Let’s see how this works. First, we have up top a <xsl:choose> statement, with our XSLT this is used in conjunction with a <xsl:when> and <xsl:otherwise> statement.

    This is done for multiple conditional tests. When a <xsl:when> test must evaluate to a boolean, which is a true / false, value it can only be two values a one or a zero and it’s true or false. A node set is true only if it is not empty. If we have the hostname section of our hierarchy, but nothing in here, then this would evaluate to false. The <xsl:otherwise> here acts as an else statement, which executes if the above <xsl:choose> evaluates to false. If the hostname exists in our configuration hierarchy then we will change it to R1. If it does not exist then we will display an error. The xnm here in <xnm:error> is an error message that is in the xnm namespace and we have the message node in this error element.

    SLAX

    SLAX is the same thing as XSLT, just an alternative syntax. It defines exactly the same information, just writing it in a different manner. It too is only used for on-box scripting and was created by Juniper. I would make sure that you are aware of that for your JNCIA-DevOps exam.

    SLAX is a pre-processor for XSLT, allowing for C-like syntax

    It is a pre-processor to XSLT. The way this works is our SLAX script is processed and converted into an XSLT script before being executed. So SLAX and XSLT are essentially the same thing, it’s just a different way of writing XSLT. It can be much easier to read and to write SLAX, it’s really similar syntax and feel to C or C-like languages if you are used to that. Let’s take a look at an example of SLAX.

    Example of SLAX which performs the same function as the XSLT script above

    This script does exactly the same thing as the XSLT example above, just with a whole lot less of writing. Rather than having to open and close every tag as in XSLT, in our SLAX example we see that our tags are opened and closed with braces. We just specify the tag and open it with an open brace and close it with a close brace. That alone makes things a lot easier! Even beyond that, our choose and when statements are changed, we now use an if statement, a conditional that is a little more natural to those who are familiar with other programming languages.

    Python

    Python is used for both on-box and off-box scripting. That’s something important to know, is that we do have the ability to write python scripts and run them on-box. The PyEZ library is pre-installed in Junos on supported software, so python 2.7 was included for some platforms starting at 18.1 R1.

    You don’t really need to remember the software version for the JNCIA-DevOps exam, but this might be important if you’re trying to test this out on your own devices. You will need to be at 18.1 R1 minimum for some devices. For most of the devices, Python 3 was introduced at 19.4 R1, so i’d really recommend getting up to 19.4 r1 so that will include python 3 on-box. This is included for your SRX, EX, QFX, MX, ACX and PTX platforms. Most of the platforms out there!

    Example python script above and the resulting output below

    A quick example of what a python script looks like. Here I’m just going to describe what this is doing briefly, we’re going to take a much deeper dive into python and PyEZ later in the course and go through some examples.

    First, at the top of the Pythoin script, after the first 2 lines, we’re importing the Device class from the jnpr.junos library. So, jnpr.junos is the PyEZ library. After we import that Device class, first we have some error handling with our try and except statements.

    Next we have some contextual assistance here with the with statement, but really what we’re doing is we’re instantiating the Device class. We are specifying that it is to communicate over serial over the port com4. We are specifying our username and password as well. You would not typically want to specify your password or username in plain text in your scripts, but for this example it gets the point across.

    So we are saying this class with these arguments specified will now be referred to as dev, that we will use the dev variable name to refer to that. Finally we are printing dev.facts. The facts is an attribute of this Device class and when we print dev.facts this is the information that we get below.

    The facts attribute gives us a lot of information here, and spits it out as a python dictionary. We see here some information whether it has two routing engines, what the home directory is, some information about the status what the model this is. This script was run against an SRX210 device.

    Now you might be very eagle-eyed and realize that “wait a minute, 19.4 doesn’t run on an SRX210” and that is absolutely right! This was run as an off-box script, where we connected via serial over com4. If you’re running this as an on-box script, the ‘serial’, ‘port’, ‘username’ and ‘password’ information doesn’t need to be present, you just import the device class and use it with no arguments and it will run on-box. That is because it will execute with the permissions of the user account that has executed the script.

    Other languages

    The following programming languages we’re going to take a very very brief look at. For the exam, I would recommend being aware that they are available for on-box scripting but you just don’t really need to know any details about them, what they look like or how to actually use them, they’re just not covered in the exam.

    Perl is officially supported by Juniper

    First up, Pearl. Pearl is used only for off-box scripting. It executes operational mode commands via the XML API. The module that is available is the Junos device module this is officially supported by Juniper and is downloaded from this link , which also includes some sample scripts.

    Java is also supported for Junos

    Next up, Java.

    As well Java won’t be covered in this course, but it’s used for off-box scripting. The Juniper library for Java makes working with NETCONF in Java a lot easier. We have our Java class and modules available here. There are four main classes included in the Juniper Java library and that is downloaded from github from the official Juniper repository, and it is the netconf-java repository.

    Ruby is another supported language for Junos

    Finally, Ruby.

    Ruby is also used for off-box scripting. This might be a little more helpful for you to be aware of since some automation solutions, like Puppet and Chef use ruby on the back-end to be able to script. Ruby might be very helpful if you’re using these configuration automation engines. It also executes operational mode commands via the XML API and you can retrieve, modify and commit configuration changes. The Ruby Gem for Juniper is also found on the juniper github repository.

    Finally, to take this theory and watch how it applies to a real device, check out the full lesson video below!

    Tag:api, automation, coding, devops, java, jncia, jncia-devops, juniper, junos, languages, perl, programming, python, ruby, slax, xml, xslt

    • Share:
    author avatar
    Ben Jacobson

    Previous post

    Junos XML API Overview
    May 14, 2022

    Next post

    XML XPath Concepts and Overview
    May 14, 2022

    You may also like

    lab.png
    Using Ansible
    4 June, 2022
    example_playbook_run.png
    Ansible Playbooks
    4 June, 2022
    sample_usage.png
    Ansible Inventory
    4 June, 2022

    Leave A Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Popular Courses

    Cisco CCNA – 200-301 Complete Course

    Cisco CCNA – 200-301 Complete Course

    $39.99 $29.99
    Juniper JNCIA-DevOps – JN0-222 – Network Automation

    Juniper JNCIA-DevOps – JN0-222 – Network Automation

    $39.99 $19.99
    Juniper JNCIA-Junos – NEW JN0-104 Complete Course

    Juniper JNCIA-Junos – NEW JN0-104 Complete Course

    $39.99 $19.99

    Latest Posts

    What Businesses Can Expect as Carriers Invest Billions in Broadband Connectivity
    20Sep2022
    Using Ansible
    04Jun2022
    Ansible Playbooks
    04Jun2022
    • 410 635 0231
    • Owings Mills, MD USA
    • contact@ciscolessons.com

    Company

    • About Us
    • Blog
    • Contact
    • Become a Teacher

    Links

    • Courses
    • Events
    • FAQs

    Support

    • Documentation
    • Forums

    All rights reserved CiscoLessons®

    • Privacy
    • Terms
    • Sitemap
    • Purchase