Try using Junos On-box Python # 1 Op Script

Overview

This is the article on the 11th day of NetOpsCoding Advent Calendar 2016.

This time, I would like to introduce how to use On-box Python installed from Junos 16.1.

About Junoscript

Junoscript is a script environment that runs on the Junos router. Depending on when it runs, Op Script (runs at any time), Commit Script (runs when the settings are committed), and Event Script (runs in response to some event) There is one. Until now, in order to use Junosscript, it was necessary to write a slightly unique script by XSLT or SLAX, but from Junos 16.1, these can be written in Python. On-box Python builds on the pre-existing simple PyEZ-based operations and extends the functionality previously possible with Junoscript.

This time it's a touch, so I'd like to write an Op Script and try it.

For more information, please refer to the Official Documentation. I will.

environment

This time, I am trying in the following environment.

Setting

On-box Python is not enabled by default. To enable it, you need to enter the following settings.

set system scripts language python

This allows you to specify a script written in Python in op / event / commit script.

Also, in Junoscript, for security reasons, the script you want to execute must be specified in the config, so even if it is an op script that can be executed at any time, the following settings are required.

set system scripts op file test.py



 The script file specified here must be saved in `` `/ var / db / scripts / op /` ``.

 Hello, world!

 Now that the preparations are complete, I would like to write a suitable script immediately.
 First of all, it's a classic, so it doesn't mean anything, but let's write Hello world and move it.


#### **`/var/db/scripts/op/To test.Place the following script with the name py.`**
```Place the following script with the name py.



#### **`test.py`**
```py

def main():
    print "Hello, world!"

if __name__ == "__main__":
    main()

As mentioned above, in order to execute the script, it is necessary to define the file name of the script in the config, so specify it as in the above example and commit it.

If the settings and script contents are correct, you should be able to execute the script in the following form.

root@vmx1> op test.py
Hello, world!

Try to get information

Now, running Hello world doesn't mean anything, so let's try something a little more practical. In On-box Python, PyEZ (py-junos-eznc) can be used, so I will try to get appropriate information and display it.

For example, let's run get-interface-information RPC and write code that enumerates the interface names.

test.py


from jnpr.junos import Device

def main():
    dev = Device()
    dev.open()

    interfaces = dev.rpc.get_interface_information(terse=True)

    for ifd in interfaces.getiterator("physical-interface"):
        print ifd.find("name").text.strip()

if __name__ == "__main__":
    main()

It's a very simple code. The result is as follows.

root@vmx1> op test.py
ge-0/0/0
lc-0/0/0
pfe-0/0/0
pfh-0/0/0
ge-0/0/1
ge-0/0/2
ge-0/0/3
ge-0/0/4
ge-0/0/5
ge-0/0/6
ge-0/0/7
ge-0/0/8
ge-0/0/9
cbp0
demux0
dsc
em1
esi
fxp0
gre
ipip
irb
jsrv
lo0
lsi
mtun
pimd
pime
pip0
pp0
rbeb
tap
vtep

Try changing the settings

Next, let's try changing the settings. This is also very easy because it is based on PyEZ.

Here, as an example, let's write a script that disables the interface ge-0 / 0/0.

test.py


from jnpr.junos import Device
from jnpr.junos.utils.config import Config

def main():
    dev = Device()
    dev.open()
    dev.bind(cu=Config)

    config = "set interfaces ge-0/0/0 disable"

    dev.cu.lock()

    dev.cu.load(config, format="set", merge=True)

    dev.cu.commit()
    dev.cu.unlock()

    dev.close()


if __name__ == "__main__":
    main()

The result looks like this:

root@vmx1> show configuration interfaces ge-0/0/0 | display set
set interfaces ge-0/0/0 vlan-tagging
set interfaces ge-0/0/0 unit 1001 vlan-id 1001
set interfaces ge-0/0/0 unit 1001 family inet address 10.0.1.1/24

root@vmx1> op test.py

root@vmx1> show configuration interfaces ge-0/0/0 | display set
set interfaces ge-0/0/0 disable
set interfaces ge-0/0/0 vlan-tagging
set interfaces ge-0/0/0 unit 1001 vlan-id 1001
set interfaces ge-0/0/0 unit 1001 family inet address 10.0.1.1/24

You can see that the invalidation setting of ge-0 / 0/0 has been input.

Finally

In this way, one example of using On-box Python is that scripts that were previously written in PyEZ on the server can now be run on the device.

However, as mentioned above, On-box Python can replace the existing Junoscript, so its true power can be demonstrated by using it as a language for writing Commit Script and Event Script.

In Next time, I would like to introduce examples of Commit Script and Event Script.

Recommended Posts

Try using Junos On-box Python # 1 Op Script
Try using Junos On-box Python # 2 Commit Script
Try using Tweepy [Python2.7]
Try using Kubernetes Client -Python-
Try using Junos' On-box Python #Bonus 1 Something like ChatOps / Commit and post to Slack
Try using Pleasant's API (python / FastAPI)
Try using LevelDB in Python (plyvel)
Try using Python argparse's action API
Try using the Python Cmd module
Try using Leap Motion in Python
Try using Amazon DynamoDB from Python
Try python
Try using the Wunderlist API in Python
Try mathematical formulas using Σ with python
Try using Dialogflow (formerly API.AI) Python SDK #dialogflow
Try using Python with Google Cloud Functions
Try to operate Excel using Python (Xlwings)
Try using Tkinter
Script python file
Try using docker-py
python script skeleton
Try using cookiecutter
Try using PDFMiner
Try using the BitFlyer Ligntning API in Python
Python script profiling
Python: Try using the UI on Pythonista 3 on iPad
Start using Python
Try using geopandas
Import python script
Try using Selenium
Try using scipy
Python> try: / except:
Try using the Python web framework Tornado Part 1
Try using pandas.DataFrame
Try Juniper JUNOS PyEz (python library) Memo 1 ~ PyEz Overview ~
Try using tensorflow ① Build python environment and introduce tensorflow
Try using django-swiftbrowser
Try using matplotlib
Try using the Python web framework Tornado Part 2
Try using tf.metrics
Try using PyODE
Run a python script from excel (using xlwings)
Scraping using Python
Try using ChatWork API and Qiita API in Python
Try using the DropBox Core API in Python
[Unity (C #), Python] Try running Python code in Unity using IronPython
Try creating a compressed file using Python and zlib
(Python) Try to develop a web application using Django
Try projective transformation of images using OpenCV with Python
Try to extract high frequency words using NLTK (python)
Try scraping with Python.
Operate Redmine using Python Redmine
Fibonacci sequence using Python
python try ~ except ~ else
Data analysis using Python 0
Try using virtualenv (virtualenvwrapper)
Data cleaning using Python
[Azure] Try using Azure Functions
Using Python #external packages
DynamoDB Script Memo (Python)
WiringPi-SPI communication using Python