Contents:
Bases: jnpr.junos.cfg.resource.Resource
[edit interfaces <name>]
Bases: jnpr.junos.cfg.phyport.base.PhyPortBase
Bases: object
Resource or Resource-Manager constructor. All managed resources and resource-managers inherit from this class.
If not None, identifies a specific resource by ‘name’. The format of the name is resource dependent. Most resources take a single string name, while others use tuples for compound names. refer to each resource for the ‘namevar’ definition
If namevar is None, then the instance is a Resource-Manager (RM). The RM is then used to select specific resources by name using the __getitem__ overload.
activate resource in Junos config the same as the Junos config-mode “activate” command
is this resource configuration active on the Junos device?
RuntimeError: | if invoked on a manager object |
---|
activate resource in Junos config the same as the Junos config-mode “deactivate” command
remove configuration from Junos device the same as the Junos config-mode “delete” command
does this resource configuration exist on the Junos device?
RuntimError: | if invoked on a manager |
---|
is this a new resource? that is, it does not exist on the Junos device when it was initally retrieved
RuntimeError: | if invoked on a manager |
---|
a resource may contain sub-managers for hierarchical oriented resources. this method will return a list of manager names attached to this resource, or :None: if there are not any
proptery from :has: to :should:
performs a ‘deepcopy’ of the property; used to make changes to list, dict type properties
rename resource in Junos configuration the same as the Junos config-mode “rename” command
move the configuration within the Junos hierarcy the same as the Junos config-mode “insert” command
Kvargs: | after=”<name>” before=”<name>” |
---|
write resource configuration stored in :should: back to device
Bases: jnpr.junos.cfg.resource.Resource
[edit system login user <name>]
Bases: jnpr.junos.cfg.resource.Resource
[edit system login user <name> authentication <key-type> <key-value> ]
Adds a new ssh-key to the user authentication. You can provide either the path to the ssh-key file, or the contents of they key (useful for loading the same key on many devices)
Path: | (optional) path to public ssh-key file on the local server, |
---|---|
Key_value: | (optional) the contents of the ssh public key |
Bases: jnpr.junos.factory.table.Table
Retrieve configuration data for this table. By default only the keys of the table is loaded. This behavior can be overriden by either requesting a sepcifc table item, or by calling with kvargs[‘values’]=True
Vargs: | [0] identifies a unique item in the table, same as calling with :kvargs[‘key’]: value |
---|
‘values’ - True/False*, when set to True will cause all data item values to be retrieved, not just the name-keys.
‘key’ - used to retrieve the contents of the table record, and not just the list of keys (default) [when namesonly=True]
Fields: | dictionary of fields, structure of which is ~internal~ and should not be defined explicitly. use the RunstatMaker.Fields() mechanism to create theserather than hardcoding the dictionary structures; since they might change over time. |
---|---|
Kvargs: | ‘view_name’ to name the class. this could be useful for debug or eventual callback mechanisms. ‘groups’ is a dict of name/xpath assocaited to fields this technique would be used to extract fields from node-set elements like port <if-device-flags>. ‘extends’ names the base View class to extend. using this technique you can add to existing defined Views. |
This file contains the FactoryLoader class that is used to dynamically create Runstat Table and View objects from a <dict> of data. The <dict> can originate from any kind of source: YAML, JSON, program. For examples of YAML refer to the .yml files in this jnpr.junos.op directory.
Bases: object
Used to load a <dict> of data that contains Table and View definitions.
The primary method is :load(): which will return a <dict> of item-name and item-class definitions.
If you want to import these definitions directly into your namespace, (like a module) you would do the following:
loader = FactoryLoader() catalog = loader.load( <catalog_dict> ) globals().update( catalog )
If you did not want to do this, you can access the items as the catalog. For example, if your <catalog_dict> contained a Table called MyTable, then you could do something like:
MyTable = catalog[‘MyTable’] table = MyTable(dev) table.get() ...
Bases: jnpr.junos.factory.table.Table
Retrieve the XML table data from the Device instance and returns back the Table instance - for call-chaining purposes.
If the Table was created with a :path: rather than a Device, then this method will load the XML from that file. In this case, the *vargs, and **kvargs are not used.
ALIAS: __call__
Vargs: | [0] is the table :arg_key: value. This is used so that the caller can retrieve just one item from the table without having to know the Junos RPC argument. |
---|---|
Kvargs: | these are the name/value pairs relating to the specific Junos XML command attached to the table. For example, if the RPC is ‘get-route-information’, there are parameters such as ‘table’ and ‘destination’. Any valid RPC argument can be passed to :kvargs: to further filter the results of the :get(): operation. neato! |
Bases: object
Dev: | Device instance |
---|---|
Xml: | lxml Element instance |
Path: | file path to XML, to be used rather than :dev: |
True if this table does not have records, but is a container of fields False otherwise
Save a copy of the table XML data to a local file. The name of the output file (:path:) can include the name of the Device host, the timestamp of this action, as well as any user-defined appended value. These ‘add-ons’ will be added to the :path: value prior to the file extension in the order (hostname,timestamp,append), separated by underscore (_).
For example, if both hostname=True and append=’BAZ1’, then when :path: = ‘/var/tmp/foo.xml’ and the Device.hostname is “srx123”, the final file-path will be “/var/tmp/foo_srx123_BAZ1.xml”
Path: | file-path to write the XML file on the local filesystem |
---|---|
Hostname: | if True, will append the hostname to the :path: |
Timestamp: |
|
Append: | any <str> value that you’d like appended to the :path: value preceding the filename extension. |
Bases: object
View is the base-class that makes extracting values from XML data appear as objects with attributes.
Table: | instance of the RunstatTable |
---|---|
View_xml: | this should be an lxml etree Elemenet object. This constructor also accepts a list with a single item/XML |
return the name of view item
~~~ EXPERIMENTAL ~~~ refresh the data from the Junos device. this only works if the table provides an “args_key”, does not update the original table, just this specific view/item
Bases: object
Used to dynamically create a field dictionary used with the RunstatView class
field string value will be passed to function :astype:
This is typically used to do simple type conversions, but also works really well if you set :astype: to a function that does a basic converstion like look at the value and change it to a True/False. For example:
astype=lambda x: True if x == ‘enabled’ else False
Load a YAML file at :path: that contains Table and View definitions. Returns a <dict> of item-name anditem-class definition.
If you want to import these definitions directly into your namespace, (like a module) you would do the following:
globals().update( loadyaml( <path-to-yaml-file> ))
If you did not want to do this, you can access the items as the <dict>. For example, if your YAML file contained a Table called MyTable, then you could do something like:
catalog = loadyaml( <path-to-yaml-file> ) MyTable = catalog[‘MyTable’]
table = MyTable(dev) table.get() ...
Bases: object
Used to load a <dict> of data that contains Table and View definitions.
The primary method is :load(): which will return a <dict> of item-name and item-class definitions.
If you want to import these definitions directly into your namespace, (like a module) you would do the following:
loader = FactoryLoader() catalog = loader.load( <catalog_dict> ) globals().update( catalog )
If you did not want to do this, you can access the items as the catalog. For example, if your <catalog_dict> contained a Table called MyTable, then you could do something like:
MyTable = catalog[‘MyTable’] table = MyTable(dev) table.get() ...
facts[‘HOME’] = login home directory
Pythonifier for ARP Table/View
Pythonifier for BFD Table/View
Pythonifier for EthPort Table/View
Pythonifier for ISIS Table/View
Pythonifier for LACP Table/View
Pythonifier for LDP Table/View
Pythonifier for LLDP Table/View
Pythonifier for PhyPort Table/View
Pythonifier for Route Table/View
Pythonifier for Xcvr Table/View
Bases: jnpr.junos.utils.util.Util
Overivew of Configuration Utilities:
Commit a configuration.
Parameters: |
|
---|---|
Returns: |
|
Raises CommitError: | |
When errors detected in candidate configuration. You can use the Exception errs variable to identify the specific problems |
Warning
If the function does not receive a reply prior to the timeout a RpcTimeoutError will be raised. It is possible the commit was successful. Manual verification may be required.
Perform a commit check. If the commit check passes, this function will return True. If the commit-check results in warnings, they are reported and available in the Exception errs.
Returns: | True if commit-check is successful (no errors) |
---|---|
Raises: |
|
Retrieve a diff (patch-format) report of the candidate config against either the current active config, or a different rollback.
Parameters: | rollback (int) – rollback id [0..49] |
---|---|
Returns: |
|
Loads changes into the candidate configuration. Changes can be in the form of strings (text,set,xml), XML objects, and files. Files can be either static snippets of configuration or Jinja2 templates. When using Jinja2 Templates, this method will render variables into the templates and then load the resulting change; i.e. “template building”.
Parameters: |
|
---|---|
Returns: | RPC-reply as XML object. |
Raises: | ConfigLoadError: When errors detected while loading candidate configuration. You can use the Exception errs variable to identify the specific problems |
Attempts an exclusive lock on the candidate configuration. This is a non-blocking call.
Returns: | True always when successful |
---|---|
Raises LockError: | |
When the lock cannot be obtained |
Helper method that calls print on the diff (patch-format) between the current candidate and the provided rollback.
Parameters: | rb_id (int) – the rollback id value [0-49] |
---|---|
Returns: | None |
Perform action on the “rescue configuration”.
Parameters: |
|
---|---|
Returns: |
Note True regardless if a rescue configuration exists.
Note The rescue configuration is only loaded as the candidate, and not committed. You must commit to make the rescue configuration active. |
Raises ValueError: | |
If action is not one of the above |
Bases: jnpr.junos.utils.util.Util
Filesystem (FS) utilities:
Returns the contents of the file path.
Parameters: | path (str) – File-path |
---|---|
Returns: | contents of the file (str) or None if file does not exist |
Performs the checksum command on the given file path using the required calculation method and returns the string value. If the path is not found on the device, then None is returned.
Parameters: |
|
---|---|
Returns: | checksum value (str) or None if file not found |
Perform a local file copy where from_path and to_path can be any valid Junos path argument. Refer to the Junos “file copy” command documentation for details.
Parameters: |
|
---|
Returns: | True if OK, False if file does not exist. |
---|
Change working directory to path.
Parameters: | path (str) – path to working directory |
---|
File listing, returns a dict of file information. If the path is a symlink, then by default followlink will recursively call this method to obtain the symlink specific information.
Parameters: |
|
---|---|
Returns: | dict collection of file information or None if path is not found |
Executes the ‘mkdir -p’ command on path.
Warning
REQUIRES SHELL PRIVILEGES
Returns: | True if OK, error-message (str) otherwise |
---|
Perform a local file rename function, same as “file rename” Junos CLI.
Returns: | True if OK, False if file does not exist. |
---|
Performs a local file delete action, per Junos CLI command “file delete”.
Returns: | True when successful, False otherwise. |
---|
Executes the ‘rmdir’ command on path.
Warning
REQUIRES SHELL PRIVILEGES
Parameters: | path (str) – file-path to directory |
---|---|
Returns: | True if OK, error-message (str) otherwise |
Returns a dictionary of status information on the path, or None if the path does not exist.
Parameters: | path (str) – file-path on local device |
---|---|
Returns: | status information on the file |
Return type: | dict |
Perform the ‘request system storage cleanup’ command to remove files from the filesystem. Return a dict of file name/info on the files that were removed.
Returns: | dict on files that were removed |
---|
Perform the ‘request system storage cleanup dry-run’ command to return a dict of files/info that would be removed if the cleanup command was executed.
Returns: | dict of files that would be removed (dry-run) |
---|
Returns the storage usage, similar to the unix “df” command.
Returns: | dict of storage usage |
---|
Bases: object
The SCP utility is used to conjunction with jnpr.junos.utils.sw.SW when transferring the Junos image to the device. The SCP can be used for other secure-copy use-cases as well; it is implemented to support the python context-manager pattern. For example:
from jnpr.junos.utils.scp import SCP
with SCP( dev, progress=_scp_progress ) as scp:
scp.put( package, remote_path )
Constructor that wraps paramiko and scp related objects.
Parameters: |
|
---|
Creates an instance of the scp object and return to caller for use.
Note
This method uses the same username/password authentication credentials as used by jnpr.junos.device.Device.
Warning
The jnpr.junos.device.Device ssh_private_key_file option is currently not supported.
Todo
add support for ssh_private_key_file.
Returns: | SCPClient object |
---|
Bases: object
Junos shell execution utility. This utility is written to support the “context manager” design pattern. For example:
def _ssh_exec(self, command):
with StartShell(self._dev) as sh:
got = sh.run(command)
ok = sh.last_ok
return (ok, got)
Open an ssh-client connection and issue the ‘start shell’ command to drop into the Junos shell (csh). This process opens a paramiko.SSHClient instance.
Run a shell command and wait for the response. The return is a tuple. The first item is True/False if exit-code is 0. The second item is the output of the command.
Parameters: |
|
---|---|
Returns: | result of the executed shell command (str) |
Note
as a side-effect this method will set the self.last_ok property. This property is set to True if $? is “0”; indicating the last shell command was successful.
Bases: jnpr.junos.utils.util.Util
Software Utility class, used to perform a software upgrade and associated functions. These methods have been tested on simple deployments. Refer to install for restricted use-cases for software upgrades.
Performs the complete installation of the package that includes the following steps:
Warning
This process has been validated on “simple” deployments.
Tested:
Known Restrictions:
You can get a progress report on this process by providing a progress callback.
Note
You will need to invoke the reboot() method explicitly to reboot the device.
Parameters: |
|
---|
Returns dictionary of file listing information for current and rollback Junos install packages. This information comes from the /packages directory.
Warning
Experimental method; may not work on all platforms. If you find this not working, please report issue.
Computes the MD5 checksum value on the local package file.
Parameters: | package (str) – File-path to the package (*.tgz) file on the local server |
---|---|
Returns: | MD5 checksum (str) |
Raises IOError: | when package file does not exist |
Computes the SHA1 checksum value on the local package file.
Parameters: | package (str) – File-path to the package (*.tgz) file on the local server |
---|---|
Returns: | SHA1 checksum (str) |
Raises IOError: | when package file does not exist |
Computes the SHA-256 value on the package file.
Parameters: | package (str) – File-path to the package (*.tgz) file on the local server |
---|---|
Returns: | SHA-256 checksum (str) |
Raises IOError: | when package file does not exist |
Issue the ‘request system software add’ command on the package. The “no-validate” options is set by default. If you want to validate the image, do that using the specific validate() method. Also, if you want to reboot the device, suggest using the reboot() method rather reboot=True.
Parameters: |
|
---|
Todo
Add way to notify user why installation failed.
Warning
Refer to the restrictions listed in install().
Perform a system shutdown, with optional delay (in minutes) .
If the device is equipped with dual-RE, then both RE will be rebooted. This code also handles EX/QFX VC.
Parameters: | in_min (int) – time (minutes) before rebooting the device. |
---|---|
Returns: |
|
Raises RpcError: | |
when command is not successful. |
Todo
need to better handle the exception event.
SCP ‘put’ the package file from the local server to the remote device.
Parameters: |
|
---|
Perform a system reboot, with optional delay (in minutes) or at a specified date and time.
If the device is equipped with dual-RE, then both RE will be rebooted. This code also handles EX/QFX VC.
Parameters: |
|
---|---|
Returns: |
|
Raises RpcError: | |
when command is not successful. |
Todo
need to better handle the exception event.
Computes the MD5 checksum on the remote device.
Parameters: | remote_package (str) – The file-path on the remote Junos device |
---|---|
Returns: | The MD5 checksum string |
Raises RpcError: | |
when the remote_package is not found. |
Todo
should trap the error and return None instead.
Issues the ‘request’ command to do the rollback and returns the string output of the results.
Returns: | Rollback results (str) |
---|
Copy the install package safely to the remote device. By default this means to clean the filesystem to make space, perform the secure-copy, and then verify the MD5 checksum.
Parameters: |
|
---|---|
Returns: |
|
Bases: object
Junos Device class.
When non-zero the call to open() will probe for NETCONF reachability before proceeding with the NETCONF session establishment. If you want to enable this behavior by default, you could do the following in your code:
from jnpr.junos import Device
# set all device open to auto-probe with timeout of 10 sec
Device.auto_probe = 10
dev = Device( ... )
dev.open() # this will probe before attempting NETCONF connect
Used to return a Jinja2 Template.
Parameters: | filename (str) – file-path to Jinja2 template file on local device |
---|---|
Returns: | Jinja2 Template give filename. |
Device object constructor.
Parameters: |
|
---|
Used to attach things to this Device instance and make them a property of the :class:Device instance. The most common use for bind is attaching Utility instances to a :class:Device. For example:
from jnpr.junos.utils.config import Config
dev.bind( cu=Config )
dev.cu.lock()
# ... load some changes
dev.cu.commit()
dev.cu.unlock()
Parameters: |
|
---|
Executes the CLI command and returns the CLI text output by default.
Parameters: |
|
---|
Note
You can also use this method to obtain the XML RPC command for a given CLI command by using the pipe filter | display xml rpc. When you do this, the return value is the XML RPC command. For example if you provide as the command show version | display xml rpc, you will get back the XML Element <get-software-information>.
Warning
This function is provided for DEBUG purposes only! DO NOT use this method for general automation purposes as that puts you in the realm of “screen-scraping the CLI”. The purpose of the PyEZ framework is to migrate away from that tooling pattern. Interaction with the device should be done via the RPC function.
Warning
You cannot use “pipe” filters with command such as | match or | count, etc. The only value use of the “pipe” is for the | display xml rpc as noted above.
Executes the CLI command and returns the CLI text output by default.
Parameters: |
|
---|
Executes an XML RPC and returns results as either XML or native python
Parameters: |
|
---|---|
Raises: |
|
Returns: | RPC-reply as XML object. If to_py is provided, then that function is called, and return of that function is provided back to the caller; presumably to convert the XML to native python data-types (e.g. dict). |
Returns: | list of Resource Managers/Utilities attached to this instance using the bind() method. |
---|
Opens a connection to the device using existing login/auth information.
Parameters: |
|
---|---|
Returns Device: | Device instance (self). |
Raises: |
|
Probe the device to determine if the Device can accept a remote connection. This method is meant to be called prior to :open(): This method will not work with ssh-jumphost environments.
Parameters: |
|
---|---|
Returns: | True if probe is successful, False otherwise |
Bases: jnpr.junos.exception.RpcError
Generated in response to a commit-check or a commit action.
Bases: jnpr.junos.exception.RpcError
Generated in response to a failure when loading a configuration.
Bases: jnpr.junos.exception.ConnectError
Generated if the user-name, password is invalid
Bases: exceptions.Exception
Parent class for all connection related exceptions
Bases: jnpr.junos.exception.ConnectError
Generated if the connection is made to a non-master routing-engine. This could be a backup RE on an MX device, or a virtual-chassis member (linecard), for example
Bases: jnpr.junos.exception.ConnectError
Generated if the specified host denies the NETCONF; could be that the services is not enabled, or the host has too many connections already.
Bases: jnpr.junos.exception.ConnectError
Generated if the NETCONF session fails to connect, could be due to the fact the device is not ip reachable; bad ipaddr or just due to routing
Bases: jnpr.junos.exception.ConnectError
Generated if the specific hostname does not DNS resolve
Bases: jnpr.junos.exception.RpcError
Generated in response to attempting to take an exclusive lock on the configuration database.
Bases: jnpr.junos.exception.RpcError
Generated in response to invoking an RPC for which the auth user does not have user-class permissions.
PermissionError.message gives you the specific RPC that cause the exceptions
Bases: jnpr.junos.exception.ConnectError
Generated if auto_probe is enabled and the probe action fails
Bases: exceptions.Exception
Parent class for all junos-pyez RPC Exceptions
Bases: jnpr.junos.exception.RpcError
Generated in response to a RPC execution timeout.
Bases: jnpr.junos.exception.RpcError
Generated in response to attempting to unlock the configuration database.
These are Junos XML ‘helper’ definitions use for generic XML processing
.DEL to delete an item .REN to rename an item, requires the use of NAME()
.INSERT(<’before’|’after’>) to reorder an item, requires the use of NAME() .BEFORE to reorder an item before another, requires the use of NAME() .AFTER to reorder an item after another, requires the use of NAME()
.NAME(name) to assign the name attribute
Bases: object
~PRIVATE CLASS~ creates an RPC meta-executor object bound to the provided ez-netconf :junos: object
retrieve configuration from the Junos device
Filter_xml: | is options, defines what to retrieve. if omitted then the entire configuration is returned |
---|---|
Options: | is a dict, creates attributes for the RPC |
loads :contents: onto the Junos device, does not commit the change.
Options: | is a dictionary of XML attributes to set within the <load-configuration> RPC. |
---|
The :contents: are interpreted by the :options: as follows:
format=’text’ and action=’set’, then :contents: is a string containing a series of “set” commands
format=’text’, then :contents: is a string containing Junos configuration in curly-brace/text format
<otherwise> :contents: is XML structure