This library was built for two types of users: Non-Programmers Programmers Non-Programmers - Python as a Power Shell Non-programmers, such as Network Engineers , can use the native Python shell on their management server (laptop, tablet, phone, and so on) as their point-of-control to remotely manage Junos OS devices. The Python shell is an interactive environment that provides the necessary means to perform common automation tasks, such as conditional testing, for-loops, macros, and templates. These building blocks are similar enough to other "shell" environments, like Bash, to enable the non-programmer to use the Python shell as a power tool, instead of a programming language. From the Python shell, you can manage Junos OS devices using native hash tables, arrays, and so on, instead of using device-specific Junos OS XML or resorting to "screen scraping" the actual Junos OS CLI
- hosts: scg roles: - Juniper.junos connection: local gather facts: no tasks: - name: Set the SCG gateway into service-mode maintenance junos install config: user=myuser passwd=mypwd123 port=830 host=(( inventory hostname )) file=/root/scg-automation/scg-activate-service-mode.conf overwrite=no logfile=/root/scg-automation/scg-junos-config.log - hosts: automation gather facts: no tasks: - name: Clear subscribers from SCG prior to loading new configuration shell: python clear scg subs.py args: chdir: /root/scg-automation - hosts: scg roles: - Juniper.junos connection: local gather facts: no tasks: - name: Overwrite the default configuration for the SCG junos install config: user=myuser passwd=mypwd123 port=830 host=(( inventory hostname )) file=/root/scg-automation/scg-default.conf overwrite=yes logfile=/root/scg-automation/scg-junos-config.log - hosts: automation tasks: - name: Revert IFL subscribers from SCG having restored configuration shell: python clear scg subs.py revert args: chdir: /root/scg-automation - hosts: scg roles: - Juniper.junos connection: local gather facts: no tasks: - name: Reboot SCG to clear all state junos shutdown: user=myuser passwd=mypwd123 port=830 host=(( inventory hostname )) shutdown="shutdown" reboot="yes" The first element uses a configuration exerpt applied in merge mode to activate "maintenance mode"
no search term matches found in comments.
For documentation on the Config.load() method, enter help(Config.load) at the Python shell
But here are the highlights, and you can do " help(Config.load) " at the Python shell for complete documentation
To remotely access a Junos OS device (physical or virtual), you must import the Device type: 1 from jnpr.junos import Device You can obtain help from the Python shell using: help(Device)
If you were using this table in the Python shell, you could do something like the following, assuming dev is a connected Device instance: 1 >>> dev 2 Device(srx210) 3 >>> 4 >>> from jnpr.junos.op.ethport import * 5 >>> eths = EthPortTable(dev) 6 >>> eths.get() 7 EthPortTable:srx210: 8 items 8 >>> eths['ge-0/0/0'].oper 9 'up' At a minimum, you must define fields within your view, as illustrated in lines 5 through 10
For example, using the Python shell: 1 >>> from jnpr.junos.op.xcvr import XcvrTable 2 >>> 3 >>> xcvrs = XcvrTable(path = '/var/tmp/xcvrs/xcvrs CHI-MX480-re0 20131226145947.xml' ) 4 >>> 5 >>> xcvrs.get() 6 XcvrTable: / var / tmp / xcvrs / xcvrs CHI - MX480 - re0 20131226145947.xml: 8 items At this point, you can use the xcvrs tTable as if you had sourced the data directly from the Device
Example The following Python shell output illustrates using the EthPortTable Table and View widgets
On the Python Shell Finally, let's see how to use this YAML file in a Python shell (or your program), and see how the Junos PyEZ works in practice for someone who doesn't know Junos/XML/XPath. Assume that you've stored the above YAML in a file called "mydefs.yml", and you have an established Device instance ( dev ) to a remote Junos OS device. 01 ## At the Python shell. 02 ## import the 'loadyaml' utility and load the definitions into the Python 03 ## global namespace 04 ## 05 >>> from jnpr.junos.factory import loadyaml 06 >>> globals ().update( loadyaml( 'mydefs.yml' ) ) 07 ## 08 ## examine our current namespace, see that we have a "RouteTable" class 09 ## 10 >>> dir () 11 [ 'CurSelView' , 'RouteTable' , 'loadyaml' , 'dev' , (...other stuff ommitted)] 12 ## 13 ## now define a table widget of RouteTable and bind it to a Device object 14 ## 15 >>> tbl = RouteTable(dev) 16 ## 17 ## You can then 'get' a specific route destination or pull in the entire table. 18 ## Let's just get the one for '10.207.64.0/24' - this will only extract this one 19 ## entry from the Junos device; which save considerably on processing. 20 ## 21 >>> tbl.get( '10.207.64.0/24' ) 22 RouteTable:your device name: 1 items 23 ## 24 ## since you only have one time in the table, you could get the item either 25 ## by using index 0, or again using the specific destination, since that 26 ## is the table item key
For example: 1 >>> eths.get() 2 EthPortTable:jnpr - dc - fw: 3 items From the Python shell we can see that there are three Ethernet ports in this table
no search term matches found in comments.