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.
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.
This time, I am trying in the following environment.
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!
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
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.
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